C# iOS 8中的隔离存储异常
我被要求修复一款内置于Unity的iPhone游戏,该游戏在iOS 8中导致问题。该游戏在所有以前版本的iOS中都能正常运行,但在iOS 8中,它会加载所有初始屏幕,然后在以下LoadGameData函数中卡住,从而导致Xcode 6中出现此错误:C# iOS 8中的隔离存储异常,c#,objective-c,xcode,unity3d,ios8,C#,Objective C,Xcode,Unity3d,Ios8,我被要求修复一款内置于Unity的iPhone游戏,该游戏在iOS 8中导致问题。该游戏在所有以前版本的iOS中都能正常运行,但在iOS 8中,它会加载所有初始屏幕,然后在以下LoadGameData函数中卡住,从而导致Xcode 6中出现此错误: IsolatedStorageException: Could not find a part of the path "/private/var/mobile/Containers/Bundle/Application/D9F47ED4-40E1-
IsolatedStorageException: Could not find a part of the path "/private/var/mobile/Containers/Bundle/Application/D9F47ED4-40E1-420E-A5A8-836F52BC301C/Documents/GameSave3.dat".
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean isAsync, Boolean anonymous) [0x00000] in <filename unknown>:0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access) [0x00000] in <filename unknown>:0
at LoadSave.LoadGameData () [0x00000] in <filename unknown>:0
at MainLoop.Update () [0x00000] in <filename unknown>:0
(Filename: Line: -1)
IsolatedStorageException:找不到路径“/private/var/mobile/Containers/Bundle/Application/D9F47ED4-40E1-420E-A5A8-836F52BC301C/Documents/GameSave3.dat”的一部分。
在System.IO.FileStream..ctor(System.String路径、FileMode模式、FileAccess访问、FileShare共享、Int32 bufferSize、布尔匿名、FileOptions选项)[0x00000]中:0
在System.IO.FileStream..ctor(System.String路径、FileMode模式、FileAccess访问、FileShare共享、Int32 bufferSize、Boolean isAsync、Boolean anonymous)[0x00000]中:0
在System.IO.FileStream..ctor(System.String路径、FileMode模式、FileAccess访问)[0x00000]中:0
在LoadSave.LoadGameData()[0x00000]中:0
在:0中的MainLoop.Update()[0x00000]处
(文件名:行:-1)
这是LoadSave.cs文件,其中包含LoadGameData()函数:
using UnityEngine;
using System.Collections;
using System.IO;
public class LoadSave : MonoBehaviour {
void Start ()
{
Debug.Log ("Start LoadSave");
Globals.g_loadSave = this;
}
void Update () {
}
void SetGameDataDefaults ()
{
for (int i = 0; i < (int)World.ActivityType.kNum; i++)
{
Globals.g_main.world.notification_day[i] = -1;
Globals.g_main.world.notification_hour[i] = -1;
Globals.g_main.world.notification_minute[i] = -1;
}
}
string pathForDocumentsFile( string filename )
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
string path = Application.dataPath.Substring( 0, Application.dataPath.Length - 5 );
path = path.Substring( 0, path.LastIndexOf( '/' ) );
return Path.Combine( Path.Combine( path, "Documents" ), filename );
}
else if(Application.platform == RuntimePlatform.Android)
{
string path = Application.persistentDataPath;
path = path.Substring(0, path.LastIndexOf( '/' ) );
return Path.Combine (path, filename);
}
else
{
string path = Application.dataPath;
path = path.Substring(0, path.LastIndexOf( '/' ) );
return Path.Combine (path, filename);
}
}
public void SaveGameData()
{
Utilities.Log("Write to GameSave File");
string path = this.pathForDocumentsFile("GameSave3.dat" );
FileStream file = new FileStream (path, FileMode.Open, FileAccess.Write);
this.WriteGameDataToFile(file);
file.Close();
}
public void LoadGameData()
{
string path = pathForDocumentsFile( "GameSave3.dat" );
//if the file has not been made yet then set defaults and create it...
if (!File.Exists(path))
{
Utilities.Log("Create GameSave File");
this.SetGameDataDefaults();
FileStream newFile = new FileStream (path, FileMode.Create, FileAccess.Write);
this.WriteGameDataToFile(newFile);
newFile.Close();
return;
}
//Otherwise just read it
Utilities.Log("Read GameSave File");
FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read);
this.ReadGameDataFromFile(file);
file.Close();
}
void ReadGameDataFromFile(FileStream filestream)
{
BinaryReader reader = new BinaryReader(filestream);
for (int i = 0; i < World.kNumOpenPlayGamesRemembered; i++)
{
Globals.g_main.world.lastOpenPlayGames[i] = (GameType)reader.ReadInt32();
}
Globals.g_main.world.openPlayRememberIndex = reader.ReadInt32();
for (int i = 0; i < (int)World.ActivityType.kNum; i++)
{
Globals.g_main.world.numBadges[i] = reader.ReadInt32();
}
//stored local notification info
for (int i = 0; i < (int)World.ActivityType.kNum; i++)
{
Globals.g_main.world.notification_day[i] = reader.ReadInt32();
Globals.g_main.world.notification_hour[i] = reader.ReadInt32();
Globals.g_main.world.notification_minute[i] = reader.ReadInt32();
}
}
void WriteGameDataToFile(FileStream filestream)
{
BinaryWriter writer = new BinaryWriter(filestream);
for (int i = 0; i < World.kNumOpenPlayGamesRemembered; i++)
{
writer.Write((int)Globals.g_main.world.lastOpenPlayGames[i]);
}
writer.Write(Globals.g_main.world.openPlayRememberIndex);
for (int i = 0; i < (int)World.ActivityType.kNum; i++)
{
writer.Write((int)Globals.g_main.world.numBadges[i]);
}
//stored local notification info
for (int i = 0; i < (int)World.ActivityType.kNum; i++)
{
writer.Write((int)Globals.g_main.world.notification_day[i]);
writer.Write((int)Globals.g_main.world.notification_hour[i]);
writer.Write((int)Globals.g_main.world.notification_minute[i]);
}
}
}
使用UnityEngine;
使用系统集合;
使用System.IO;
公共类LoadSave:MonoBehavior{
无效开始()
{
Log(“启动LoadSave”);
Globals.g_loadSave=此;
}
无效更新(){
}
void SetGameDataDefaults()
{
对于(int i=0;i<(int)World.ActivityType.kNum;i++)
{
Globals.g_main.world.notification_day[i]=-1;
Globals.g_main.world.notification_hour[i]=-1;
Globals.g_main.world.notification_minute[i]=-1;
}
}
字符串路径文档文件(字符串文件名)
{
if(Application.platform==RuntimePlatform.IPhonePlayer)
{
字符串路径=Application.dataPath.Substring(0,Application.dataPath.Length-5);
path=path.Substring(0,path.LastIndexOf('/');
返回Path.Combine(Path.Combine(路径,“文档”),文件名);
}
else if(Application.platform==RuntimePlatform.Android)
{
字符串路径=Application.persistentDataPath;
path=path.Substring(0,path.LastIndexOf('/');
返回Path.Combine(路径、文件名);
}
其他的
{
字符串路径=Application.dataPath;
path=path.Substring(0,path.LastIndexOf('/');
返回Path.Combine(路径、文件名);
}
}
public void SaveGameData()
{
Log(“写入GameSave文件”);
字符串路径=this.pathForDocumentsFile(“GameSave3.dat”);
FileStream file=newfilestream(路径,FileMode.Open,FileAccess.Write);
此.writegamedata文件(文件);
file.Close();
}
公共void LoadGameData()
{
字符串路径=pathForDocumentsFile(“GameSave3.dat”);
//如果文件尚未创建,则设置默认值并创建它。。。
如果(!File.Exists(path))
{
日志(“创建游戏保存文件”);
这个.SetGameDataDefaults();
FileStream newFile=newfilestream(路径,FileMode.Create,FileAccess.Write);
此.WriteGameDataToFile(新文件);
newFile.Close();
返回;
}
//否则就看吧
日志(“读取游戏保存文件”);
FileStream file=newfilestream(路径,FileMode.Open,FileAccess.Read);
此.ReadGameDataFromFile(文件);
file.Close();
}
void ReadGameDataFromFile(FileStream FileStream)
{
BinaryReader=新的BinaryReader(文件流);
for(int i=0;i
让我困惑的是,为什么它能在iOS的所有版本中工作,除了iOS 8。你知道问题出在哪里吗?谢谢你。我终于明白了。问题在于pathForDocumentsFile()函数中使用的dataPath方法。iOS的早期版本不要求路径是持久的,因为我在下面将其用于androids。用persistentDataPath方法替换dataPath方法修复了iOS 8中的问题
if (Application.platform == RuntimePlatform.IPhonePlayer){
string path = Application.persistentDataPath.Substring( 0, Application.persistentDataPath.Length - 5 );
path = path.Substring( 0, path.LastIndexOf( '/' ) );
return Path.Combine( Path.Combine( path, "Documents" ), filename );
}