Ios 保存和加载游戏数据不起作用

Ios 保存和加载游戏数据不起作用,ios,unity3d,Ios,Unity3d,我不知道为什么我的代码不起作用,因为我遵循了我找到的示例。试图让我的iOS游戏使用Unity3D保存并加载数据,但不使用player.prefs 在现场我有这个 void OnDisable(){ GameController.Save (); } void OnEable(){ GameController.Load (); } 游戏控制器是一种静态方法 static public void Save() { Debug.Log("Saving Pl

我不知道为什么我的代码不起作用,因为我遵循了我找到的示例。试图让我的iOS游戏使用Unity3D保存并加载数据,但不使用player.prefs

在现场我有这个

void OnDisable(){
    GameController.Save ();
}

void OnEable(){
    GameController.Load ();
}
游戏控制器是一种静态方法

static public void Save()
    {
        Debug.Log("Saving Player Data...");
        print ("Saving Player Data...");
        var bf = new BinaryFormatter();
        var file = File.Create(Application.persistentDataPath + "/CatEscapeInfo.dat");

        var data = new PlayerData
        {
            PlayerLives = PlayerLives,
            Score = Score,
            Distance = Distance,
            CurrentLevelNo = CurrentLevelNo,
            HighestLevelCompleted = HighestLevelCompleted
        };

        bf.Serialize(file, data);
        file.Close();
        //PlayerPrefs.SetString (data);
        Debug.Log ("Player Data Saved: " + PlayerLives + ", " + Score);
        print ("Player Data Saved: " + PlayerLives + ", " + Score);
    }

    static public void Load()
    {
        Debug.Log("Loading Player Data...");
        if (File.Exists(Application.persistentDataPath + "/CatEscapeInfo.dat"))
        {
            var bf = new BinaryFormatter();
            var file = File.Open(Application.persistentDataPath + "/CatEscapeInfo.dat", FileMode.Open);
            var data = bf.Deserialize(file) as PlayerData;
            file.Close();

            Debug.Log("Player Data Loaded: " + data.PlayerLives + ", " + data.Score);
            print ("Player Data Loaded: " + data.PlayerLives + ", " + data.Score);

            if (data != null)
            {
                PlayerLives = data.PlayerLives;
                Score = data.Score;
                Distance = data.Distance;
                CurrentLevelNo = data.CurrentLevelNo;
                HighestLevelCompleted = data.HighestLevelCompleted;
            }
        }
    }
即使我运行了这段代码,当我在iOS上手动关闭游戏并将其恢复时,它也不会将游戏加载回它拥有的统计数据。 有什么想法吗?我错过什么了吗

更新: 在iOSXcode中,我发现它在尝试“加载”时会抛出这个堆栈跟踪,尽管此时文件还不存在

加载时这是堆栈跟踪:

> (Filename:
> /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp
> Line: 49)
> 
> SerializationException: Unexpected binary element: 255   at
> System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject
> (BinaryElement element, System.IO.BinaryReader reader, System.Int64&
> objectId, System.Object& value,
> System.Runtime.Serialization.SerializationInfo& info) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject
> (BinaryElement element, System.IO.BinaryReader reader) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph
> (BinaryElement elem, System.IO.BinaryReader reader, Boolean
> readHeaders, System.Object& result,
> System.Runtime.Remoting.Messaging.Header[]& headers) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize
> (System.IO.Stream serializationStream,
> System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
> (System.IO.Stream serializationStream) [0x00000] in <filename
> unknown>:0    at GameController.Load () [0x00000] in <filename
> unknown>:0    at GameController.OnEnable () [0x00000] in <filename
> unknown>:0    (Filename:  Line: -1)
> Saving Player Data... UnityEngine.Debug:Internal_Log(Int32, String,
> Object) UnityEngine.Debug:Log(Object)
> UnityEngine.MonoBehaviour:print(Object) GameController:Save()
> GameController:OnDisable() UnityEngine.Object:Destroy(Object, Single)
> UnityEngine.Object:Destroy(Object) GameController:Awake()   (Filename:
> /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp
> Line: 49)
> 
> ExecutionEngineException: Attempting to JIT compile method
> 'PlayerData__TypeMetadata4:.ctor ()' while running with --aot-only.
> 
>   at System.Reflection.MonoCMethod.Invoke (System.Object obj,
> BindingFlags invokeAttr, System.Reflection.Binder binder,
> System.Object[] parameters, System.Globalization.CultureInfo culture)
> [0x00000] in <filename unknown>:0  Rethrow as
> TargetInvocationException: Exception has been thrown by the target of
> an invocation.   at System.Reflection.MonoCMethod.Invoke
> (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder
> binder, System.Object[] parameters, System.Globalization.CultureInfo
> culture) [0x00000] in <filename unknown>:0    at
> System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr,
> System.Reflection.Binder binder, System.Object[] parameters,
> System.Globalization.CultureInfo culture) [0x00000] in <filename
> unknown>:0    at System.Reflection.ConstructorInfo.Invoke
> (System.Object[] parameters) [0x00000] in <filename unknown>:0    at
> System.Activator.CreateInstance (System.Type type, Boolean nonPublic)
> [0x00000] in <filename unknown>:0    at
> System.Activator.CreateInstance (System.Type type) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata
> (System.Type type) [0x00000] in <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData
> (System.Object obj,
> System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata,
> System.Object& data) [0x00000] in <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject
> (System.IO.BinaryWriter writer, Int64 id, System.Object obj) [0x00000]
> in <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance
> (System.IO.BinaryWriter writer, System.Object obj, Boolean
> isValueObject) [0x00000] in <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects
> (System.IO.BinaryWriter writer) [0x00000] in <filename unknown>:0   
> at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph
> (System.IO.BinaryWriter writer, System.Object obj,
> System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize
> (System.IO.Stream serializationStream, System.Object graph,
> System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize
> (System.IO.Stream serializationStream, System.Object graph) [0x00000]
> in <filename unknown>:0    at GameController.Save () [0x00000] in
> <filename unknown>:0    at GameController.OnDisable () [0x00000] in
> <filename unknown>:0  UnityEngine.Object:Destroy(Object, Single)
> UnityEngine.Object:Destroy(Object) GameController:Awake()
>(文件名:
>/Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp
>第49行)
> 
>SerializationException:意外的二进制元素:255处
>System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject
>(BinaryElement元素,System.IO.BinaryReader阅读器,System.Int64&
>objectId、系统、对象和值,
>中的System.Runtime.Serialization.SerializationInfo&info)[0x00000]
>:0 at
>System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject
>(二进制元素,System.IO.BinaryReader读取器)[0x00000]位于
>:0 at
>System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph
>(BinaryElement元素,System.IO.BinaryReader读取器,布尔值
>ReadHeader、System.Object和result、,
>中的System.Runtime.Remoting.Messaging.Header[]和headers)[0x00000]
>:0 at
>System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize
>(System.IO.Stream序列化流,
>中的System.Runtime.Remoting.Messaging.HeaderHandler处理程序)[0x00000]
>:0 at
>System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
>(System.IO.Stream serializationStream)[0x00000]在未知>:0在GameController.Load()[0x00000]在未知>:0在GameController.oneable()[0x00000]在未知>:0(文件名:行:-1)
保存时,这是堆栈跟踪:

> (Filename:
> /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp
> Line: 49)
> 
> SerializationException: Unexpected binary element: 255   at
> System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject
> (BinaryElement element, System.IO.BinaryReader reader, System.Int64&
> objectId, System.Object& value,
> System.Runtime.Serialization.SerializationInfo& info) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject
> (BinaryElement element, System.IO.BinaryReader reader) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph
> (BinaryElement elem, System.IO.BinaryReader reader, Boolean
> readHeaders, System.Object& result,
> System.Runtime.Remoting.Messaging.Header[]& headers) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize
> (System.IO.Stream serializationStream,
> System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
> (System.IO.Stream serializationStream) [0x00000] in <filename
> unknown>:0    at GameController.Load () [0x00000] in <filename
> unknown>:0    at GameController.OnEnable () [0x00000] in <filename
> unknown>:0    (Filename:  Line: -1)
> Saving Player Data... UnityEngine.Debug:Internal_Log(Int32, String,
> Object) UnityEngine.Debug:Log(Object)
> UnityEngine.MonoBehaviour:print(Object) GameController:Save()
> GameController:OnDisable() UnityEngine.Object:Destroy(Object, Single)
> UnityEngine.Object:Destroy(Object) GameController:Awake()   (Filename:
> /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp
> Line: 49)
> 
> ExecutionEngineException: Attempting to JIT compile method
> 'PlayerData__TypeMetadata4:.ctor ()' while running with --aot-only.
> 
>   at System.Reflection.MonoCMethod.Invoke (System.Object obj,
> BindingFlags invokeAttr, System.Reflection.Binder binder,
> System.Object[] parameters, System.Globalization.CultureInfo culture)
> [0x00000] in <filename unknown>:0  Rethrow as
> TargetInvocationException: Exception has been thrown by the target of
> an invocation.   at System.Reflection.MonoCMethod.Invoke
> (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder
> binder, System.Object[] parameters, System.Globalization.CultureInfo
> culture) [0x00000] in <filename unknown>:0    at
> System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr,
> System.Reflection.Binder binder, System.Object[] parameters,
> System.Globalization.CultureInfo culture) [0x00000] in <filename
> unknown>:0    at System.Reflection.ConstructorInfo.Invoke
> (System.Object[] parameters) [0x00000] in <filename unknown>:0    at
> System.Activator.CreateInstance (System.Type type, Boolean nonPublic)
> [0x00000] in <filename unknown>:0    at
> System.Activator.CreateInstance (System.Type type) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata
> (System.Type type) [0x00000] in <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData
> (System.Object obj,
> System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata,
> System.Object& data) [0x00000] in <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject
> (System.IO.BinaryWriter writer, Int64 id, System.Object obj) [0x00000]
> in <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance
> (System.IO.BinaryWriter writer, System.Object obj, Boolean
> isValueObject) [0x00000] in <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects
> (System.IO.BinaryWriter writer) [0x00000] in <filename unknown>:0   
> at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph
> (System.IO.BinaryWriter writer, System.Object obj,
> System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize
> (System.IO.Stream serializationStream, System.Object graph,
> System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in
> <filename unknown>:0    at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize
> (System.IO.Stream serializationStream, System.Object graph) [0x00000]
> in <filename unknown>:0    at GameController.Save () [0x00000] in
> <filename unknown>:0    at GameController.OnDisable () [0x00000] in
> <filename unknown>:0  UnityEngine.Object:Destroy(Object, Single)
> UnityEngine.Object:Destroy(Object) GameController:Awake()
>正在保存播放器数据。。。调试:内部日志(Int32,字符串,
>调试:日志(对象)
>monobhavior:print(Object)GameController:Save()
>GameController:OnDisable()UnityEngine.Object:Destroy(对象,单个)
>UnityEngine.Object:Destroy(Object)GameController:Awake()(文件名:
>/Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp
>第49行)
> 
>ExecutionEngineeException:正在尝试JIT编译方法
>“PlayerData_u_TypeMetadata4:.ctor()”同时仅使用--aot运行。
> 
>在System.Reflection.Method.Invoke(System.Object obj,
>BindingFlags invokeAttr,System.Reflection.Binder Binder,
>System.Object[]参数,System.Globalization.CultureInfo区域性)
>[0x00000]in:0将重新显示为
>TargetInvocationException:的目标已引发异常
>召唤。在System.Reflection.Method.Invoke
>(System.Object obj、BindingFlags invokeAttr、System.Reflection.Binder
>binder,System.Object[]参数,System.Globalization.CultureInfo
>区域性)[0x00000]位于:0处
>System.Reflection.MonoCMethod.Invoke(BindingFlags invokeAttr,
>System.Reflection.Binder,System.Object[]参数,
>System.Globalization.CultureInfo区域性)[0x00000]在未知>:0位于System.Reflection.ConstructorInfo.Invoke
>(System.Object[]参数)[0x00000]位于:0处
>System.Activator.CreateInstance(System.Type类型,布尔非公共)
>[0x00000]英寸:0英寸
>中的System.Activator.CreateInstance(System.Type类型)[0x00000]
>:0 at
>System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata
>(System.Type类型)[0x00000]位于:0处
>System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData
>(System.Object obj,
>System.Runtime.Serialization.Formatters.Binary.TypeMetadata和metadata,
>系统对象和数据)[0x00000]位于:0处
>System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject
>(System.IO.BinaryWriter编写器,Int64 id,System.Object obj)[0x00000]
>in:0 at
>System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance
>(System.IO.BinaryWriter编写器,System.Object对象,布尔值
>isValueObject)[0x00000]位于:0处
>System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueueObjects
>(System.IO.BinaryWriter写入程序)[0x00000]位于:0
>在
>System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph
>(System.IO.BinaryWriter编写器,System.Object对象,
>中的System.Runtime.Remoting.Messaging.Header[]headers)[0x00000]
>:0 at
>System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serializate
>(System.IO.Stream序列化流、System.Object图、,
>中的System.Runtime.Remoting.Messaging.Header[]headers)[0x00000]
>:0 at
>System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serializate
>(System.IO.Stream序列化流,System.Object图)[0x00000]
>中:0位于GameController.Save()[0x00000]中
>:0位于中的GameController.OnDisable()[0x00000]
>:0 UnityEngine.对象:销毁(对象,单个)
>对象:销毁(对象)游戏控制器:唤醒()

在场景脚本中将此代码置于
唤醒
启动
功能:

//在BinaryFormatter中强制使用不同的代码路径,该路径不依赖于运行时代码生成(在iOS上会中断)。
SetEnvironmentVariable(“MONO_REFLECTION_SERIALIZER”,“yes”);
默认情况下,单二进制序列化程序使用iOS不支持的JIT编译。幸运的是,有一种方法(上面的代码)可以将其切换为使用反射

证明/另见:

在Unity编辑器中创建错误,如:

在命名空间中找不到“环境”

几个小时的睡眠后