一.简介
- ScriptableObject是一个特殊的类,它不需要挂载上任何的game object上。它可以不像model behavior需要一个物体才可以使 用,而ScriptableObject可以存在文件夹当中,而且他可以存储我的数据(store data)。
- ScriptableObject包含许多的方法:Destroy,Instantiate,Awake,OnDestroy等。
- ScriptableObject通常与CreateAssetMenuAttribute一起使用。
- model behavior需挂在一个物体上,物体上的数据可以通过代码的运行进行更改,但是当游戏停止运行时物体上的数据将恢复为原始数据。
- 在编写代码是需要将MonoBehaviour更改为ScriptableObjet后,在编写代码。
二. 实例
例如
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/New Item")]
public class Item : ScriptableObject {
public string itemName;
public Sprite itemImage;
public int itemHeld;
[TextArea]
public string itemInfo;
public bool equip;
}