ScriptableObject初步使用

ScriptableObject 派生自 Unity 对象,但与 MonoBehaviour 不同,不能将 ScriptableObject 附加给游戏对象。需要将它们保存为项目中的资源。
ScriptableObject 将数据保存为项目中的资源,以便在运行时使用,十分方便。
简单创建:

using UnityEngine;
[CreateAssetMenu(fileName ="MyTool",menuName = "MyTool/MyScriptableObject",order =1)]
public class MyScriptableObject :ScriptableObject
{
    
    
    [Range(0,10)]public float Speed;
    public Vector3 MyVector;
    public Color MyColor;
    public GameObject gameObject;
}

注:【CreateAssetMenu(fileName = “t”, menuName = “”, order = ))】为使用自定义资源添加 Assets 菜单按钮,作用在 Assets 文件夹下,鼠标右键,菜单栏中添加一个按钮项,菜单名为 menuName,并执行生成名为 fileName 的脚本,order 为按钮显示顺序
右键Create->MyTool(如图,可自己创建路径)
在这里插入图片描述

using UnityEngine;
public class Test : MonoBehaviour
{
    
    
    [SerializeField] private MyScriptableObject my;
    private void Update()
    {
    
    
        transform.position = my.MyVector;
    }
}

可以在脚本里调用且在运行过程中改变相应参数会保存下来。

猜你喜欢

转载自blog.csdn.net/qq_51978873/article/details/120487478