2014-10-03 43 views
2

我被要求修復在Unity中構建的iPhone遊戲,導致iOS 8出現問題。該遊戲在iOS的所有先前版本中均可用,但在iOS 8中,它會加載所有啓動屏幕,然後卡住以下LoadGameData功能給我這個錯誤在Xcode 6:iOS 8中的IsolatedStorageException異常

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) 

這是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]); 
     }  


    } 
} 

令我百思不解的是,爲什麼它是工作在所有iOS的版本,但iOS 8.任何想法可能是什麼問題?謝謝。

回答

2

我想出了最後。問題出在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); 
}