ScriptableObject serialize game data

There are often some data that needs to be serialized in the game. The usual method is to [SerializeField] and modify the value in the view panel. In this way, you can adjust the data without modifying the script to facilitate planning and greatly modify the value.
However, there is a disadvantage of doing so, because the serialized data is stored in the Scene scene. If the value is not modified once, the scene file (Scene.unity) will change accordingly. When multiple people cooperate, the scene file is prone to conflicts. Here is a brief introduction to the use of ScriptableObject for serialization. It can create an Asset file in the resource panel, store the data in the resource file, and modify the value through the property panel.

using UnityEngine;
//在资源面板右键Create,创建该类对应的Asset文件
[CreateAssetMenu(fileName = "GameDataAsset", menuName = "Creat GameData Asset")]
public class GameData : ScriptableObject
{
    [Header("测试_字符串")]
    public string testStr = "Test String";
    [Space(10)]
    [Header("测试_数组")]
    public int[] testArr;
}

Define the attributes that need to be serialized in the class, so that's it, is it very simple~

2. Create an Asset file in the resource panel.
Right click => Create => Create an Asset file corresponding to the ScriptableObject class

3. Fill in / modify the value

4. Read

T setting = AssetDatabase.LoadAssetAtPath(assetPath, typeof(T)) as T;

You can use other APIs to read and load.

Guess you like

Origin blog.csdn.net/Momo_Da/article/details/98656297