Dictionary<string, Dictionary<int, int>> tmpDicts;
void Start () {
//TestStringToDictionary();
TestStringToDictionary2();
Dictionary<int, int> tmp = new Dictionary<int,int>();
tmp.Add(888888, 0);
SetKey("40004*0", tmp);
TestDictionaryToString2();
}
private void TestDictionaryToString2()
{
StringBuilder sb = new StringBuilder();
Dictionary<string, Dictionary<int, int>>.Enumerator tmpEnum = tmpDicts.GetEnumerator();
while (tmpEnum.MoveNext())
{
Debug.Log("000key: " + tmpEnum.Current.Key);
sb.Append(tmpEnum.Current.Key);
Dictionary<int, int>.Enumerator tmpSkillEnum = tmpEnum.Current.Value.GetEnumerator();
sb.Append("-");
while (tmpSkillEnum.MoveNext())
{
Debug.Log("111Value: " + tmpSkillEnum.Current.Key + " " + tmpSkillEnum.Current.Value);
sb.Append(tmpSkillEnum.Current.Key);
sb.Append("_");
sb.Append(tmpSkillEnum.Current.Value);
}
sb.Append("#");
}
Debug.Log("Dictionary to string: " + sb.ToString().TrimEnd('#'));
}
private void TestStringToDictionary2()
{
string origionalStr = "40001*0-1001_1#40002*0-2001_1#40003*0-3001_1";
tmpDicts = ParseStringToDictionary(origionalStr);
Dictionary<string, Dictionary<int, int>>.Enumerator tmpEnum = tmpDicts.GetEnumerator();
while (tmpEnum.MoveNext())
{
Debug.Log("111key: " + tmpEnum.Current.Key);
Dictionary<int, int>.Enumerator tmpSkillEnum = tmpEnum.Current.Value.GetEnumerator();
while (tmpSkillEnum.MoveNext())
{
Debug.Log("111Value: " + tmpSkillEnum.Current.Key + " " + tmpSkillEnum.Current.Value);
}
}
Debug.Log("-----------------------------------------------------------------------------------------------------");
}
public void SetKey(string newKey, Dictionary<int, int> newValue)
{
if (tmpDicts.ContainsKey(newKey))
{
tmpDicts[newKey] = newValue;
Dictionary<int, int>.Enumerator tmpSkillEnum = tmpDicts[newKey].GetEnumerator();
while (tmpSkillEnum.MoveNext())
{
Debug.Log("222Value: " + tmpSkillEnum.Current.Key + " " + tmpSkillEnum.Current.Value);
}
}
else
{
tmpDicts.Add(newKey, newValue);
}
Debug.Log("-----------------------------------------------------------------------------------------------------");
Dictionary<string, Dictionary<int, int>>.Enumerator tmpEnum = tmpDicts.GetEnumerator();
while (tmpEnum.MoveNext())
{
Debug.Log("333key: " + tmpEnum.Current.Key);
Dictionary<int, int>.Enumerator tmpSkillEnum = tmpEnum.Current.Value.GetEnumerator();
while (tmpSkillEnum.MoveNext())
{
Debug.Log("333Value: " + tmpSkillEnum.Current.Key + " " + tmpSkillEnum.Current.Value);
}
}
Debug.Log("-----------------------------------------------------------------------------------------------------");
}
public Dictionary<string, Dictionary<int, int>> ParseStringToDictionary(string origionalStr)
{
Dictionary<string, Dictionary<int, int>> tmpDict = new Dictionary<string, Dictionary<int, int>>();
string[] fields = origionalStr.Split('#');
for (int i = 0; i < fields.Length; i++)
{
string[] dictFiled = fields[i].Split('-');
if (!tmpDict.ContainsKey(dictFiled[0]))
{
tmpDict.Add(dictFiled[0], StringToDictionary2(dictFiled[1]));
}
}
return tmpDict;
}
//1001_1
public Dictionary<int, int> StringToDictionary2(string skillStr)
{
Dictionary<int, int> tmpDict = new Dictionary<int, int>();
string[] fileds = skillStr.Split('_');
tmpDict.Add(int.Parse(fileds[0]), int.Parse(fileds[1]));
return tmpDict;
}