在最新的游戏中,需要把游戏进度保存到文件中,但是在iOS平台上却遇到了Crash,一开始以为是序列化问题,但是已经按照Answer上面所说的,加上一句
System.Environment.SetEnvironmentVariable ("MONO_REFLECTION_SERIALIZER", "yes"); |
通过反射来进行文件序列化操作。添加Debug语句后发现,游戏Crash的地方还没到Serialize那步,反而是在File.Create的地方。
BinaryFormatter bf = new BinaryFormatter ();
file = File.Create(path);
bf.Serialize (file, missionsData);
file.Close ();
于是估计是创建文件时导致出错,最后怀疑是文件路径导致的,原本获取文件路径的方法是这样的
private static void SettingUpFilePath () { if (Application.platform == RuntimePlatform.Android) {//Android Platform path = Application.persistentDataPath; } else if (Application.platform == RuntimePlatform.IPhonePlayer) {//IOS Platform ///Get iPhone Documents Path string dataPath = Application.dataPath; string fileBasePath = dataPath.Substring (0, dataPath.Length - 5); path = fileBasePath.Substring (0, fileBasePath.LastIndexOf ('/')) + "/Documents"; } else {//Others path = Application.dataPath; } path += "/" + fileName; } |
如果使用dataPath,在iPhone真机会Crash,需要换成 Application.persistentDataPath
private static void SettingUpFilePath () { if (Application.platform == RuntimePlatform.Android) {//Android Platform path = Application.persistentDataPath; } else if (Application.platform == RuntimePlatform.IPhonePlayer) {//IOS Platform ///Get iPhone Documents Path path = Application.persistentDataPath; } else {//Others path = Application.dataPath; } path += "/" + fileName; } |
再次运行,游戏终于不Crash了
- 本文固定链接: https://freemanapp.com/zh/?p=80
- 转载请注明: freeman 于 FreemanApp 发表