Unity 游戏光明之光笔记
第五章 背包零碎的实
1.开发性能按钮

留神Anchors的地位,能够让图标在窗口变动时放弃绝对地位不会变动

2.性能按钮的事件的监听
给FunctionBar增加脚本别离给按钮增加点击办法监听事件

 public void OnStatusButtonClick() {    }public void OnBagButtonClick() {    }public void OnEquipButtonClick() {    }public void OnSkillButtonClick() {    }public void OnSettingButtonClick() {    }

3.创立物品信息的管理系统
应用Text文件存储信息
增加icon给Atals图集
创立用来治理治理信息的TXT 文档 OjbectsInfoList

//对应的信息名称//ID,游戏名称,icon名称,类型,hp,mp,出售价,购买价1001,小瓶血药,icon-potion1,Drug,50,0,50,601002,大瓶血药,icon-potion2,Drug,100,0,70,1001003,蓝药,icon-potion3,Drug,0,100,60,80

创立ObjectInfo脚本治理信息

//应用单例模式    void Awake() {        _instance = this;        ReadInfo();    }   //定义各个物品的性能 public enum ObjectType {    Drug,    Equip,    Mat}//示意物品的一条信息public class ObjectInfo {    public int id;    //id     public string name;//名称    public string icon_name;//这个名称是存储在图集中的名称 icon名称    public ObjectType type;//类型(药品drug)    public int hp; //加血量值    public int mp;//加魔法值    public int price_sell;//出售价    public int price_buy;//购买}

在程序中读取文本,将物品信息读取到内存

应用TextAcess类

获取文本文件

public TextAsset objectsInfoListText;

要把获取的字符存到字典要援用新的命名空间

using System.Collections.Generic;
 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');        foreach (string str in strArray) {            string[] proArray = str.Split(',');            //实例化info存储信息            ObjectInfo info = new ObjectInfo();                        //ID转换成字符类型            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);//增加到字典中,id为key,能够很不便的依据id查找到这个物品信息        }    }    
  1. 设计背包界面

为了在鼠标挪动到面板点击时角色不挪动,增加box collider组件
5.创立脚本治理背包物品
创立Inventory脚本设置设置单例模式不便调用

    //设置单例模式    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();    }

单个网格中寄存信息,若要创立InventoryItemGrid脚本进行信息的存储,要给网格增加box Collider组件

创立Tag辨别网格中是否存在物品,给不同的网格制订Tag

 //存储物品信息    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>();    }    //管制网格的显示办法,默认num里为1,依据idmunber显示    public void SetId(int id, int num = 1) {        this.id = id;        info = ObjectsInfo._instance.GetObjectInfoById(id);        //获取子物体        InventoryItem item = this.GetComponentInChildren<InventoryItem>();        item.SetIconName(info.icon_name);        //更新显示        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;    }

InventoryItem脚本

private UISprite sprite;    private void Awake()    {        base.Awake();        sprite = this.GetComponent<UISprite>();    }    //判断鼠标是否 在网格中    protected 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;    }