ios - 保存和加载游戏数据不起作用
<p><p>不知道为什么我的代码不起作用,因为我遵循了我找到的示例。试图让我的 iOS 游戏使用 Unity3D 保存和加载数据,但不使用 player.prefs。</p>
<p>在现场我有这个</p>
<pre><code>void OnDisable(){
GameController.Save ();
}
void OnEable(){
GameController.Load ();
}
</code></pre>
<p>游戏 Controller 是一个静态方法.. </p>
<pre><code>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;
}
}
}
</code></pre>
<p>即使我运行了这段代码,但当我在 iOS 上手动关闭游戏并将其恢复时,它永远不会将游戏加载回原来的状态。
有什么想法吗?我错过了什么吗?</p>
<p>更新:
在 iOS XCode 中,我发现它在尝试“加载”时会吐出这个堆栈跟踪,尽管此时文件还不存在。</p>
<p><strong>加载时这是堆栈跟踪:</strong></p>
<pre><code>> (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) in
> <filename unknown>:0 at
> System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject
> (BinaryElement element, System.IO.BinaryReader reader) 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) in
> <filename unknown>:0 at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize
> (System.IO.Stream serializationStream,
> System.Runtime.Remoting.Messaging.HeaderHandler handler) in
> <filename unknown>:0 at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
> (System.IO.Stream serializationStream) in <filename
> unknown>:0 at GameController.Load () in <filename
> unknown>:0 at GameController.OnEnable () in <filename
> unknown>:0 (Filename:Line: -1)
</code></pre>
<p><strong>保存时,这是堆栈跟踪:</strong></p>
<pre><code>> 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)
> in <filename unknown>:0Rethrow 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) in <filename unknown>:0 at
> System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr,
> System.Reflection.Binder binder, System.Object[] parameters,
> System.Globalization.CultureInfo culture) in <filename
> unknown>:0 at System.Reflection.ConstructorInfo.Invoke
> (System.Object[] parameters) in <filename unknown>:0 at
> System.Activator.CreateInstance (System.Type type, Boolean nonPublic)
> in <filename unknown>:0 at
> System.Activator.CreateInstance (System.Type type) in
> <filename unknown>:0 at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata
> (System.Type type) 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) in <filename unknown>:0 at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject
> (System.IO.BinaryWriter writer, Int64 id, System.Object obj)
> in <filename unknown>:0 at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance
> (System.IO.BinaryWriter writer, System.Object obj, Boolean
> isValueObject) in <filename unknown>:0 at
> System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects
> (System.IO.BinaryWriter writer) 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) 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) in
> <filename unknown>:0 at
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize
> (System.IO.Stream serializationStream, System.Object graph)
> in <filename unknown>:0 at GameController.Save () in
> <filename unknown>:0 at GameController.OnDisable () in
> <filename unknown>:0UnityEngine.Object:Destroy(Object, Single)
> UnityEngine.Object:Destroy(Object) GameController:Awake()
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>将此代码放入场景脚本中的 <code>Awake</code> 或 <code>Start</code> 函数中:</p>
<pre class="lang-cs prettyprint-override"><code>// Forces a different code path in the BinaryFormatter that doesn't rely on run-time code generation (which would break on iOS).
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
</code></pre>
<p>默认情况下,Mono 二进制序列化器使用 iOS 不支持的 JIT 编译。幸运的是,有一种方法(上面的代码)可以将其切换为使用反射。</p>
<p>证明/参见:</p>
<ul>
<li><p> <a href="http://answers.unity3d.com/questions/725419/filestream-binaryformatter-from-c-to-ios-doesnt-wo.html" rel="noreferrer noopener nofollow">http://answers.unity3d.com/questions/725419/filestream-binaryformatter-from-c-to-ios-doesnt-wo.html</a> </p></li>
<li><p> <a href="http://answers.unity3d.com/questions/30930/why-did-my-binaryserialzer-stop-working.html" rel="noreferrer noopener nofollow">http://answers.unity3d.com/questions/30930/why-did-my-binaryserialzer-stop-working.html</a> </p></li>
</ul></p>
<p style="font-size: 20px;">关于ios - 保存和加载游戏数据不起作用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/27806113/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/27806113/
</a>
</p>
页:
[1]