Unity 游戏黑暗之光笔记第五章 背包系统的实现

Posted ananoi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 游戏黑暗之光笔记第五章 背包系统的实现相关的知识,希望对你有一定的参考价值。

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,60
1002,大瓶血药,icon-potion2,Drug,100,0,70,100
1003,蓝药,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查找到这个物品信息
        }
    }
    
4. 设计背包界面

 

 

 

 

  • 为了在鼠标移动到面板点击时角色不移动,添加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;
    }

 

以上是关于Unity 游戏黑暗之光笔记第五章 背包系统的实现的主要内容,如果未能解决你的问题,请参考以下文章

Unity 游戏黑暗之光笔记第一章 完善场景

Unity3D实战RPG黑暗之光:游戏分解及各系统的实现

Unity3D实战RPG黑暗之光Scene3:创建游戏运行场景及角色控制

Unity初探之黑暗之光

unity初探之黑暗之光

Linux读书笔记第五章