• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:38:52 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

在现场我有这个

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

void OnEable(){
    GameController.Load ();
}

游戏 Controller 是一个静态方法..

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 ("layer Data Saved: " + PlayerLives + ", " + Score);
        print ("layer 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("layer Data Loaded: " + data.PlayerLives + ", " + data.Score);
            print ("layer 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 上手动关闭游戏并将其恢复时,它永远不会将游戏加载回原来的状态。 有什么想法吗?我错过了什么吗?

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

加载时这是堆栈跟踪:

> (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.Debugog(Object)
> UnityEngine.MonoBehaviour:print(Object) GameController:Save()
> GameController:OnDisable() UnityEngine.Objectestroy(Object, Single)
> UnityEngine.Objectestroy(Object) GameController:Awake()   (Filename:
> /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp
> Line: 49)
> 
> ExecutionEngineException: Attempting to JIT compile method
> 'layerData__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.Objectestroy(Object, Single)
> UnityEngine.Objectestroy(Object) GameController:Awake()



Best Answer-推荐答案


将此代码放入场景脚本中的 AwakeStart 函数中:



默认情况下,Mono 二进制序列化器使用 iOS 不支持的 JIT 编译。幸运的是,有一种方法(上面的代码)可以将其切换为使用反射。

证明/参见:

关于ios - 保存和加载游戏数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27806113/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap