zhouqijie

存档系统示例

保存游戏进度,新手指引完成状态等信息。

数据的存档系统

public abstract class StandaloneData
{
    //abstract
    public abstract string GetKey();
    public abstract string Serialize();
    public abstract void Deserialize(string json);

    //read
    public static T Read<T>() where T:StandaloneData, new()
    {
        T obj = new T();

        if(PlayerPrefs.HasKey(obj.GetKey()))
        {
            obj.Deserialize(PlayerPrefs.GetString(obj.GetKey()));
            return obj;
        }
        else
        {
            return null;
        }
    }

    //save
    public static void Save<T>(T obj) where T : StandaloneData, new()
    {
        //Debug.LogAssertion(obj.Serialize()); //DBEUG
        PlayerPrefs.SetString(obj.GetKey(), obj.Serialize());
    }
}