我正在开发一款游戏,用户可以在玩游戏前选择一个类别。根据所选的类别,此处加载必要的字典是执行此操作的代码
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: An element with the same key already exists in the dictionary. 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 source, System.Func2 keySelector, System.Func
2 elementSelector, IEqualityComparer1 comparer) System.Linq.Enumerable.ToDictionary[String,String] (IEnumerable
1 source, System.Func2 keySelector, IEqualityComparer
1 comparer) System.Linq.Enumerable.ToDictionary[String,String] (IEnumerable1 source, System.Func
2 keySelector) GameController.Awake () (at Assets/Scripts/GameController.cs:127)
最佳答案
C# 字典具有键值结构,这意味着它看起来像这样:
Dictionary<string, string> dictionary
示例:
(Key => Value)
"greeting" => "Hello"
"animal" => "Dog"
当得到一个值时,你给字典字符串 Key
并返回值。
Debug.Log(dictionary["greeting"]) --> "Hello"
因此,当您在文本文件中尝试添加具有相同值的多个键时,例如:
"greeting" => "Hello"
"animal" => "Dog"
"greeting" => "Hey"
您将得到一个异常,因为您破坏了 Dictionary 的工作方式(一个值的唯一键)。如果您没有得到异常并且您尝试访问键“greeting”的值,您会得到哪个值?
Debug.Log(dictionary["greeting"]) --> ???
这就是编译时出现异常的原因。
您需要确保每个键都是唯一的。在您的文本文件中,您有同一个词的多个实例。我没有看到字典的使用,也许你被“字典”这个词的语义和你想用它做什么所困。我认为 List 就是您所追求的(?),这意味着没有键,只有值。
因为我不知道你想要达到什么目的,所以很难推荐一个解决方案,但你可以在字典上阅读更多信息 here .和 here这是对如何从数组填充它的答案(如果您确实需要它作为字典)。
更新:
I used a dictionary because I want to be able to check if the text file contains a word
好的!我认为字典不是你想要的;这是您想要的列表。将所有单词添加到列表中:
wordList = wordListArray.ToList();
并检查列表是否包含您要检查的单词(也可以在您的数组上完成)
if (wordList.contains(YourString)) {
Debug.Log(YourString + " exist in wordList!");
}
更新2:
I get the structure of the dictionary but from my code I am loading the contents of the text file into the dictionary and before doing that depending on the category selected a particular text file is loaded... say i have text files with names animals, countries, names. if the person selects names the names text file is loaded
给我的印象是您还有另一个问题,即如何选择要加载的文件。那么为此你需要一些不同的东西,我假设你有,我们称之为 playerChoice
.方法如下:
TextAsset textAsset;
switch (playerChoice) {
case "PlayerOption1":
textAsset = Resources.Load<TextAsset>("words1", typeof(TextAsset));
break;
case "PlayerOption2":
textAsset = Resources.Load<TextAsset>("words2", typeof(TextAsset));
break;
}
// and then the rest of your textAsset,
// handling code (splitting on linebreaks and .ToList())
因为我不知道你什么时候想加载一个不同的文件,所以这是我能做到的最低抽象度。
关于c# - 选择文本文件用作 Unity 中的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42643810/