文件流媒体崩溃团结

时间:2016-08-17 13:07:20

标签: c# unity3d filestream

我正在尝试为我的应用保存数据我正在使用此代码:

BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/inventorySave.gd"); 
bf.Serialize(file, toSave);
file.Close();

我也试过这段代码:

StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/inventorySave.txt");
sw.WriteLine(toSave);
print("wrote line" + toSave);
sw.Flush();
sw.Close();    

但它们似乎都没有工作(我的团结崩溃,我在控制台中没有任何错误)。在for循环中调用save,我试图保存少于8个字符的字符串,可以找到代码here

1 个答案:

答案 0 :(得分:1)

这是我在你的代码中找到的地方:

for (int i = 0; i <= numbers.Length ; i++)
            {
                print("inside while class");
                switch (int.Parse(numbers[i--].ToString()))
                {
                    case 0:
                        finalString[i--] = "a".ToString(); break;
                    case 1:
                        finalString[i--] = "b".ToString(); break;
                    case 2:
                        finalString[i--] = "c".ToString(); break;
                    case 3:
                        finalString[0] = "d".ToString(); break;
                    case 4:
                        finalString[i--] = "e".ToString(); break;
                    case 5:
                        finalString[i--] = "f".ToString(); break;
                    case 6:
                        finalString[i--] = "g".ToString(); break;
                    case 7:
                        finalString[i--] = "h".ToString(); break;
                    case 8:
                        finalString[i--] = "i".ToString(); break;
                    case 9:
                        finalString[i--] = "j".ToString(); break;
                }

                i++;

在每次迭代中,这都是变量i

  1. int i = 0; // in start of for loop i is "0".

  2. switch (int.Parse(numbers[i--].ToString())) // i = -1 here

  3. finalString[i--] = "a".ToString(); break; // in any of case statement, i = -2

  4. i++; // i = -1 here
  5. i++; // i = 0 here
  6. 因此: i的值始终为0且绝不会大于numbers.Length,因此循环不会终止。

    因此:无限循环 - &gt; Unity崩溃