希望高手指點迷津。。。。。沒找到解決的辦法,謝謝了
相關代碼:
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 );
}
}
}
測驗的資訊:
uj5u.com熱心網友回復:
這沒太理解你得意思,是不是說讀取出來的道具id一樣的,只更改資料呢,這樣的話,你最好先檢測這個id是不是存在于字典里,如果存在就增加數量,否則就增加id假設你得道具為id, num
if (!dic.containsKey(id))
dic.add(id, 0);
dic[id] += num
uj5u.com熱心網友回復:
不同的id 下放不同的圖片,現在的錯誤是:從字典里取值時,鍵不同,但值一樣。發現錯誤源頭是:向字典中添加資料時,每添加一條新的就會覆寫已存在的值(兩者id 不同)。。。。。
uj5u.com熱心網友回復:
ObjectInfo info = new ObjectInfo();這句話在 回圈外邊 進行了實體化. 應該 在回圈外宣告變數, 回圈里進行實體化.
在回圈里病沒有 實體化.每次都是對同一個物件進行修改,
這樣改一下.
ObjectInfo info = null;
foreach (string str in strArray) {//決議出單獨一行的資訊
info = new ObjectInfo();
string[] proArray = str.Split (',');
.......
uj5u.com熱心網友回復:
光這些代碼,看不出什么情況uj5u.com熱心網友回復:
你是在遍歷外實體化的ObjectsInfo,所以說從頭到尾你只是實體化了一個ObjectsInfo,字典就像是一個冰箱,你實體化的ObjectsInfo就是一個杯子,ObjectsInfo里面的欄位就是那你往杯子里裝的東西,你不斷對杯子里的東西進行替換,最后放進冰箱里的還是只有一只杯子uj5u.com熱心網友回復:
ObjectInfo info = new ObjectInfo(); 你確定位置放對地方,你回圈操作的有意義?都是一個物件,值肯定一樣的uj5u.com熱心網友回復:
前面已經說對了最后結果是好多個 Id 對應最后一個物體
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/57959.html
標籤:Unity3D
