Unity Web game Dark Light Notes
Chapter five is the realization of knapsack system
1. Develop function buttons
- Notice the placement of Anchors so that the icon remains in relative position when the window changes
2. Event monitoring of function buttons
- Add a script to the FunctionBar to add click method listening events to the button
public void OnStatusButtonClick() {
}
public void OnBagButtonClick() {
}
public void OnEquipButtonClick() {
}
public void OnSkillButtonClick() {
}
public void OnSettingButtonClick() {
}
Copy the code
3. Create a management system for item information
- Use Text files to store information
- Add icon to Atals atlas
- Create a TXT file for managing administrative information OjbectsInfoList
//ID, game name, icon name, type, HP, MP, price, Purchase price is 1001, the small bottles of blood, icon - potion1, Drug, 50,0,50,60 1002, big bottles of blood, icon - potion2, Drug, 100,0,70,100 1003, blue pills, icon - potion3, Drug, 0100,60,80Copy the code
Example Create ObjectInfo script management information
Void Awake() {_instance = this; ReadInfo (); } public enum ObjectType {Drug, Equip, Mat} public class ObjectInfo {public int id; //id public string name; // name public string icon_name; // This is the name stored in the atlas icon name public ObjectType type; // type (drug) public int HP; Public int mp; Public int price_sell; Public int price_buy; / / buy}Copy the code
Read text in the program, read the item information into memory
Using TextAcess class
Get text file
public TextAsset objectsInfoListText;
Copy the code
To save the retrieved character to the dictionary, reference the new namespace
using System.Collections.Generic;
Copy the code
public static ObjectsInfo _instance; private Dictionary<int, ObjectInfo> objectInfoDict = new Dictionary<int, ObjectInfo>(); public TextAsset objectsInfoListText; Void Awake() {_instance = this; ReadInfo(); } public ObjectInfo GetObjectInfoById(int id) {ObjectInfo info=null; objectInfoDict.TryGetValue(id, out info); return info; } void ReadInfo() {string text = objectSinfolisttext.text; String [] strArray = text.split ('\n'); string[] strArray = text.split ('\n'); foreach (string str in strArray) { string[] proArray = str.Split(','); // Instantiate info Store information ObjectInfo info = new ObjectInfo(); Parse(proArray[0]); int ID = int.Parse(proArray[0]); string name = proArray[1]; string icon_name = proArray[2]; string str_type = proArray[3]; ObjectType type = ObjectType.Drug; switch (str_type) { case "Drug": type = ObjectType.Drug; break; case "Equip": type = ObjectType.Equip; break; case "Mat": type = ObjectType.Mat; break; } info.id = ; info.name = name; info.icon_name = icon_name; info.type = type; if (type == ObjectType.Drug) { int hp = int.Parse(proArray[4]); int mp = int.Parse(proArray[5]); int price_sell = int.Parse(proArray[6]); int price_buy = int.Parse(proArray[7]); info.hp = hp; info.mp = mp; info.price_buy = price_buy; info.price_sell = price_sell; } objectInfoDict.Add(id, info); // add to the dictionary, id is key, can be very easy to find the object information according to the id}}Copy the code
- Design backpack interface
Add a Box Collider component so that the character doesn’t move when the mouse moves over the panel and clicks
5. Create scripts to manage backpack items
Create Inventory script Settings Set the singleton mode for easy invocation
// Set the singleton mode to public static Inventory _instance; private TweenPosition tween; Public List<InventoryItemGrid> itemGrids = new List<InventoryItemGrid>(); Private int coinCount = 1000; Public UILabel coinNumberLable; private void Awake() { _instance = this; tween = this.GetComponent<TweenPosition>(); } public void Show() {tween.playForward (); } public void Hide() { tween.PlayReverse(); }Copy the code
To create a InventoryItemGrid script to store information in a single grid, add a Box Collider component to the grid
Create tags to identify items in the grid and assign tags to different grids
Public int id=0; Private ObjectInfo info = null; Public int num = 0; public UILabel numLabel; // Use this for initialization void Start () { numLabel = this.GetComponentInChildren<UILabel>(); Public void SetId(int id, int num = 1) {this.id = id;} public void SetId(int id, int num = 1) {this.id = id; info = ObjectsInfo._instance.GetObjectInfoById(id); / / get InventoryItem child object item = this. GetComponentInChildren < InventoryItem > (); item.SetIconName(info.icon_name); // Update to display numlabel. enabled = true; this.num = num; numLabel.text = num.ToString(); } public void PlusNumber(int num = 1) { this.num += num; numLabel.text = this.num.ToString(); Public void ClearInfo() {id = 0; info = null; num = 0; numLabel.enabled = false; }Copy the code
InventoryItem script
private UISprite sprite; private void Awake() { base.Awake(); sprite = this.GetComponent<UISprite>(); Override void OnDragDropRelease(GameObject surface) {base.onDragdroprelease (surface); if(surface! =null) { } } public void SetId(int id) { ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id); sprite.spriteName = info.icon_name; } public void SetIconName(string icon_name) {sprite.spriteName = icon_name; }Copy the code