0
点赞
收藏
分享

微信扫一扫

1.初识ChatGPT:AI聊天机器人的革命(1/10)

火热如冰 2024-09-04 阅读 25

1.CreateInstance方法的非持久化特点

首先,我有一个继承ScriptableObject的脚本,里面有三个变量和一个打印方法

[CreateAssetMenu(fileName ="文件名" ,menuName ="菜单名",order =0)]
public class LearnScripttableObject : ScriptableObject
{
    public int a;
    public string b;
    [SerializeField]
    private GameObject c;
    public void printinfo()
    {
        Debug.Log(a);
        Debug.Log(b);
        Debug.Log(c);
    }
}

CreateInstance这个静态方法(其作用为创建一个 LearnScripttableObject 类型的实例)

 dates = ScriptableObject.CreateInstance<LearnScripttableObject>();
    

修改演示: 

   private void Awake()
   { 
   dates = ScriptableObject.CreateInstance<LearnScripttableObject>();
       dates.a = 10;
       dates.b = "任意字符串";
       dates.printinfo();
   }

 

 

这并不适合我们保存数据的目的,但是因其可以写作代码,所以配合数据持久化知识点更加方便

2.转JSON存取

Unity数据持久化 之 Json序列化与反序列化_unity json 反序列化-CSDN博客

Unity数据持久化 之 LitJson序列化与反序列化_unity litjson-CSDN博客

这里以Utility作演示

public class ScritableObjectPersistence :MonoBehaviour
{
    LearnScripttableObject dates;

    private void Awake()
    { 
    dates = ScriptableObject.CreateInstance<LearnScripttableObject>();
        dates.a = 10;
        dates.b = "任意字符串";
        dates.printinfo();
        string str =JsonUtility.ToJson(dates);
        File.WriteAllText(Application.persistentDataPath+"text.json",str);
        print(Application.persistentDataPath);
    }   
}

取(override原有的数据)

    string newstr = File.ReadAllText(Application.persistentDataPath + "text.json");
    JsonUtility.FromJsonOverwrite(newstr, dates);

 

举报

相关推荐

0 条评论