所以我试着在Unity中阅读CSV脚本模块。一切都很好,只是最后一个键值没有添加到列表中,尽管它存在于字典中。
string[] data = fileString.Split(new string[] {";", "n"}, StringSplitOptions.None);
Debug.Log(data.Length);
int rows = data.Length / columns - 1;
List<string> kartenvariablen = new List<string>();
for (int i = 0; i<= columns; i++)
{
kartenvariablen.Add(data[i]);
}
Debug.Log(kartenvariablen.Count);
Debug.Log(kartenvariablen[9]);
List<Dictionary<string, string>> allCards = new List<Dictionary<string, string>>();
for(int i=0; i< rows; i++)
{
Dictionary<string, string> singleCard = new Dictionary<string, string>();
for(int n= 0; n <= columns; n++)
{
singleCard.Add(kartenvariablen[n], data[columns+1+n+(i*(columns+1))]);
}
foreach (KeyValuePair<string, string> s in singleCard)
{
Debug.Log(s); //here it returns Alignment TopLeft
}
allCards.Add(singleCard);
}
Debug.Log(allCards[2]["PrefabName"]); //returns test3
Debug.Log(allCards[2]["Alignment"]); //returns KeyNotFoundException: The given key 'Alignment' was not present in the dictionary.
System.Collections.Generic.Dictionary ' 2 [TKey, TValue]。get_Item (TKey key) (at <6073cf49ed704e958b8a66d540dea948>:0)CSVTemplateReader。ReadCSV () (at Assets/Scripts/CSVTemplateReader.cs:106)CSVTemplateReader。Start()(在Assets/Scripts/CSVTemplateReader.cs:19)在这里输入图像描述
可能是:
int rows = data.Length / columns;
或
for(int i=0; i <= rows; i++)
好了,我修复了代码。背后的逻辑完全没问题。问题是它没有将对齐键保存为"Alignment",但他将其保存为"Alignment"。所以我在一开始就重构了代码:string[] data = fileString。分割(新字符串[]{";";rn"}, StringSplitOptions.None);现在它完全没问题了。感谢所有的答案和帮助!