我尝试在我的Unity游戏中使用代码保存进度,但我遇到了问题。

huangapple go评论73阅读模式
英文:

I try to save progress with code in my Unity Game but I have problems with it

问题

这个错误是因为你的 Settings+Save 结构体没有被标记为可序列化。要解决这个问题,你需要在 Settings+Save 结构体前面加上 [System.Serializable] 属性,就像你在 Settings 类前面所做的一样。这将告诉系统可以对这个结构体进行序列化操作。修改后的代码如下:

[System.Serializable]
public struct Save
{
    public int save_point;
    public int percent;
}

这样修改后,你的 Save 结构体就可以被正确序列化,不再会出现这个错误。

英文:

I try to save progress with code but I have problems with it. I try 5 ways but It always gives an error.Thanks.

I have trouble with this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Settings
{
    public enum Languages
    {
        English,
        Turkce
    }

    public struct Save
    {
        public int save_point;
        public int percent;
    }


    public Languages lang;

    public Save save1;
    public Save save2;
    public Save save3;

    public static Settings data = new Settings();
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class Settings_Issues : MonoBehaviour
{

    public bool _out;
    public int _out_type;
    public int _out_id;
    void Start()
    {
       if(_out)
       {
            if (_out_type == 0)
            {

                if (_out_id == 0) GetComponent<Text>().text = "%" + Settings.data.save1.percent.ToString();
                if (_out_id == 1) GetComponent<Text>().text = "%" + Settings.data.save2.percent.ToString();
                if (_out_id == 2) GetComponent<Text>().text = "%" + Settings.data.save3.percent.ToString();

            }
        } 
    }

    void Update()
    {
        
    }

    public static void Load()
    {
        string path = Application.persistentDataPath + "/Save.gsd";
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(path, FileMode.Open);
        Settings data = formatter.Deserialize(stream) as Settings;
        stream.Close();

        Settings.data= data;

    }


    public static void Save()
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/Save.gsd";
        FileStream stream = new FileStream(path, FileMode.Open);

        formatter.Serialize(stream, Settings.data);
        stream.Close();

        Debug.Log(Application.persistentDataPath + "/Save.gsd");
    }
}

It gives this error when I use Save() with a button:

SerializationException: Type 'Settings+Save' in Assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

The full error is :

SerializationException: Type 'Settings+Save' in Assembly 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.FormatterServices+<>c__DisplayClass9_0.<GetSerializableMembers>b__0 (System.Runtime.Serialization.MemberHolder _) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, System.Runtime.Serialization.StreamingContext context) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo () (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize (System.Object obj, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.Formatters.Binary.ObjectWriter objectWriter, System.Runtime.Serialization.SerializationBinder binder) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize (System.Object obj, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.Formatters.Binary.ObjectWriter objectWriter, System.Runtime.Serialization.SerializationBinder binder) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo typeNameInfo) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize (System.Object graph, System.Runtime.Remoting.Messaging.Header[] inHeaders, System.Runtime.Serialization.Formatters.Binary.__BinaryWriter serWriter, System.Boolean fCheck) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers, System.Boolean fCheck) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
Settings_Issues.Save () (at Assets/Scripts/Settings_Issues.cs:61)
UnityEngine.Events.InvokableCall.Invoke () (at <4a31731933e0419ca5a995305014ad37>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <4a31731933e0419ca5a995305014ad37>:0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

答案1

得分: 0

Settings类被标记为可序列化,但嵌套结构Save没有被标记为可序列化。为了能够序列化Settings,也需要将Save标记为可序列化。例如:

[System.Serializable]
public struct Save
{
    public int save_point;
    public int percent;
}
英文:

Your Settings class is marked as serializable, but the nested struct Save isn't. To be able to serialize Settings, Save also needs to be marked as serializable. i.e:

[System.Serializable]
public struct Save
{
    public int save_point;
    public int percent;
}

huangapple
  • 本文由 发表于 2023年5月22日 01:21:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定