Unity ScriptableObject

举个例子

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
[CreateAssetMenu(menuName = "ScriptableObject/Weapon")]
public class Weapon : ScriptableObject {
    
    
 
	public string weaponName;
	public Sprite sprite;
	public Color color = Color.white;
	public ColliderType colliderType = ColliderType.None;
 
	public enum ColliderType {
    
    
		None = 0,
		Sprite = 1,
		Grid = 2
	}
}

在这里插入图片描述
在这里插入图片描述
这就是创建出来的对象
查看属性
在这里插入图片描述
然后

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponTest : MonoBehaviour
{
    
    
    public Weapon weapon;
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }
}

你会发现
在这里插入图片描述
可以把那个对象拖进来

你也可以用代码创建对象

Weapon w = ScriptableObject.CreateInstance<Weapon>();
AssetDatabase.CreateAsset(w, "Assets/aaa.asset");
AssetDatabase.SaveAssets();

这个相当于 提供了一种可视化的 unity独特的数据存储方式

猜你喜欢

转载自blog.csdn.net/qq_38913715/article/details/124661710
今日推荐