下面是代码:
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