ScriptableObject data module

ScriptableObject data module is easy to use

The class used by unity to store data, similar to json. It is most useful for resources used only to store data.

creation method

1. Create through CreateAssetMenu

#if UNITY_EDITOR
//创建工具栏按钮,点击按钮创建数据类
    [CreateAssetMenu(menuName = "Templet/Create HeatMapBase")]
    public class HeatMapTemplet : HeatMapBase
    {}
#endif
//创建ScriptableObject类
public class HeatMapBase : ScriptableObject
{
    public float DisRatio;
    public float Resolution;
    public List<HeatMapInfo> HeatMapInfos;
//序列化
    [Serializable]
    public class HeatMapInfo
    {
        public float MaxAmount;
        public Color Color;
    }
}

2. Created by CreateInstance

HeatMapTemplet level = ScriptableObject.CreateInstance<HeatMapTemplet>();

    public class HeatMapTemplet : HeatMapBase
    {}

public class HeatMapBase : ScriptableObject
{
    public float DisRatio;
    public float Resolution;
    public List<HeatMapInfo> HeatMapInfos;
//序列化
    [Serializable]
    public class HeatMapInfo
    {
        public float MaxAmount;
        public Color Color;
    }
}

loading method

1. AssetDatabase.LoadAssetAtPath

Read files with all paths relative to the project folder, eg: "Assets/MyTextures/hello.png".

HeatMapTemplet heatMapTemplet = (HeatMapTemplet)AssetDatabase.LoadAssetAtPath("Assets/FrameWorkSong/Data/" + "Path" + ".asset", typeof(HeatMapTemplet));

2. Resources.Load

Load resources in a folder pathlocated , or load a file located there.

HeatMapTemplet heatMapTemplet = Resources.Load<HeatMapTemplet>("Data/Path");

3. Framework resLoader.LoadAssetSync

ResLoader resLoader = new ResLoader();//资源管理器
HeatMapTemplet heatMapTemplet = resLoader.LoadAssetSync<HeatMapTemplet>("Path", "resources://Data/Path");//资源不需要后缀

Instructions

public class Exampl : MonoBehavior {
       public HeatMapTemplet heatMapTemplet;
 
       void Start()
       {
               Debug.Log(heatMapTemplet.DisRatio.string);
       }
 
}

Guess you like

Origin blog.csdn.net/dxs1990/article/details/127547593