Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4009.00元
黑暗之光中加入到字典后所有值都变成一样的了
吻风发起了问答2017-12-16
1
回复
453
浏览

下面是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectsInfo : MonoBehaviour
{


    public static ObjectsInfo _instance;
    public TextAsset objectsInfoList;




    private Dictionary<int, ObjectInfo> objectInfoDic = new Dictionary<int, ObjectInfo>();
    private void Awake()
    {
        _instance = this;
        ReadInfo();
        foreach (var item in objectInfoDic.Values)
        {
            print(item.iconName);
        }
    }

    public ObjectInfo GetObjectInfoById(int id){

        ObjectInfo info=null;
        objectInfoDic.TryGetValue(id, out info);
        return info;
    }


    void ReadInfo(){
        ObjectInfo info = new ObjectInfo();
        string text = objectsInfoList.text;
        string[] strArray = text.Split('\n');
        foreach (var item in strArray)
        {
            string[] proArray = item.Split(',');
            info.id= int.Parse(proArray[0]);
            info.name = proArray[1];
            info.iconName = 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.tpye = type;
            if (type==ObjectType.Drug)
            {
                info.addHp = int.Parse(proArray[4]);
                info.addMp = int.Parse(proArray[5]);
                info.sellPrice = int.Parse(proArray[6]);
                info.buyPrice = int.Parse(proArray[7]);
            }
            objectInfoDic.Add(info.id,info);//添加到字典中,id为key,可以很方便的根据id查找到这个物品信息。


        }
       
    }

    //id
    //名字
    //icon
    //类型
    //加血量
    //加魔量
    //出售价格
    //购买价格
}
public enum ObjectType
{
    Drug,
    Equip,
    Mat
}

public class ObjectInfo{
    public int id;
    public string name;
    public string iconName;
    public ObjectType tpye;
    public int addHp;
    public int addMp;
    public int sellPrice;
    public int buyPrice;

}
 

输出的却是3个icon-potion3。再add进字典之前我输出还是potion1、potion2、potion3

所有回复
  • 吻风 2017-12-18

    我自己已经解决。字典存的只是内存地址。这一点我没想到。所以我只开辟了一个内存导致这个错误。

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!