2,537
社区成员
发帖
与我相关
我的任务
分享
public enum ObjectType{
Drug,//药品
Equip,//装备
Mat//材料
}
//一个物品的对象类(这个物品里所有的属性)
public class ObjectInfo{
public int id;
public string name;
public string icon_name;//图集中的名称
public ObjectType type;
public int hp;
public int mp;
public int price_sell;
public int price_buy;
}
//把text中的内容读取到程序中
public void ReadText(){
//取出文本文件中所有的字符
string text = objectsInfoListText.text;
string[] strArray = text.Split ('\n');//取出一行
ObjectInfo info = new ObjectInfo();
foreach (string str in strArray) {//解析出单独一行的信息
string[] proArray = str.Split (',');
//把属性取出来
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 = id;
info.name = name;
info.icon_name = icon_name;
print ("现在要添加objectInfo 中 的 icon_name 是 "+ info.id +info.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_sell = price_sell;
info.price_buy = price_buy;
}
objectInfoDict.Add (id, info);//把已经提取出的信息存到字典里,方便调用
//测试代码
foreach (KeyValuePair <int ,ObjectInfo> temp in objectInfoDict){
print ("添加过程中对字典的遍历 " + "键是 " + temp.Key +"值部分 " + temp.Value.id+ temp.Value.icon_name );
}
}
}