作爲字典使用我開發一個遊戲,用戶玩遊戲前選擇一個類別。根據不同的類別中選擇所需的詞典在這裏加載是這樣做選擇文本文件在Unity
public Text selectedCategory; //contains the text of the selected category
private Dictionary<String,String> wordList = new Dictionary<String,String>(); //holds the dictionary
private string[] wordListArray; //the array the contents of the text file is first loaded to
private TextAsset textAsset; //the text asset to be used
private string category; // the category selected
void Awake(){
category = selectedCategory.text;
textAsset = Resources.Load ("words", typeof(TextAsset)) as TextAsset;
Debug.Log ("Words dictionary is loaded");
wordListArray = textAsset.text.Split (new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
wordList = wordListArray.ToDictionary (s => s);
Debug.Log ("File loaded as dictionary");
}
此代碼第一塊完美的作品,但是當我將其更改爲
void Awake(){
category = selectedCategory.text;
if (String.Compare(category, "Animals") == 1) {
textAsset = Resources.Load ("animals", typeof(TextAsset)) as TextAsset;
Debug.Log ("Animals dictionary is loaded");
} else {
textAsset = Resources.Load ("words", typeof(TextAsset)) as TextAsset;
Debug.Log ("Words dictionary is loaded");
}
wordListArray = textAsset.text.Split (new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
wordList = wordListArray.ToDictionary (s => s);
Debug.Log ("File loaded as dictionary");
}
我得到的錯誤代碼的代碼
的ArgumentException:具有相同鍵的元素已經存在於詞典中。 System.Collections.Generic.Dictionary
2[System.String,System.String].Add (System.String key, System.String value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:404) System.Linq.Enumerable.ToDictionary[String,String,String] (IEnumerable
1源,System.Func2 keySelector, System.Func
2 elementSelector,的IEqualityComparer1 comparer) System.Linq.Enumerable.ToDictionary[String,String] (IEnumerable
1種源,System.Func2 keySelector, IEqualityComparer
1比較器) System.Linq.Enumerable.ToDictionary [字符串,字符串](IEnumerable的1 source, System.Func
2的KeySelector) GameController.Awake()(在資產/腳本/ GameController.cs:127)
看起來不像你的代碼來通過你的問題? –
@Abdul謝謝我有同樣的問題,你的問題幫助我找到解決方案 –
@Thematkinson我編輯了它...謝謝 – Abdul