下载
Scriptable Object Tutorial-Starter
创建 ScriptableObject
-
在
Scripts
目录下,创建SwordData
脚本。using UnityEngine; [CreateAssetMenu(fileName = "New SwordData", menuName = "Sword Data", order = 51)] public class SwordData : ScriptableObject { [SerializeField] private string swordName; [SerializeField] private string description; [SerializeField] private Sprite icon; [SerializeField] private int goldCost; [SerializeField] private int attackDamage; public string SwordName { get => swordName; set => swordName = value; } public string Description { get => description; set => description = value; } public Sprite Icon { get => icon; set => icon = value; } public int GoldCost { get => goldCost; set => goldCost = value; } public int AttackDamage { get => attackDamage; set => attackDamage = value; } }
CreateAssetMenu 的分级菜单的分割线是 50, 上面的 order = 51, 为二级菜单。
增加数据
- 新建目录
Scripts
-Scriptable Objects
-Sword Data
。在新建的目录下面,点击Assets
-Create
-Sword Data
分别创建:
分别点击上面7个新建的对象,在Inspector
面板修改右边的描述属性。
使用 ScriptableObject
- 修改
Scritps
-Sword.cs
文件:using System; using UnityEngine; public class Sword : MonoBehaviour { [SerializeField] private SwordData swordData; private void OnMouseDown() { Debug.Log(swordData.SwordName); Debug.Log(swordData.Description); Debug.Log(swordData.Icon.name); Debug.Log(swordData.GoldCost); Debug.Log(swordData.GoldCost); Debug.Log(swordData.AttackDamage); } }
- 为
1_Longsword
挂载脚本:
基于 ScriptableObjects 的事件
添加事件脚本
- 在
Scripts
目录下新建,GameEvent.cs
和GameEventListener.cs
脚本。
GameEvent.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; [CreateAssetMenu(fileName = "New Game Event", menuName = "Game Event", order = 52)] public class GameEvent : ScriptableObject { private List<GameEventListener> _listeners = new List<GameEventListener>(); public void Raise() { for (int i = _listeners.Count - 1; i >= 0; i--) { _listeners[i].OnEventRaised(); } } public void RegisterListener(GameEventListener listener) { _listeners.Add(listener); } public void UnRegisterListener(GameEventListener listener) { _listeners.Remove(listener); } }
GameEventListener.cs
using UnityEngine; using UnityEngine.Events; public class GameEventListener : MonoBehaviour { [SerializeField] private GameEvent _gameEvent; [SerializeField] private UnityEvent response; private void OnEnable() { _gameEvent.RegisterListener(this); } private void OnDisable() { _gameEvent.UnRegisterListener(this); } public void OnEventRaised() { response.Invoke(); } }
- 点击
Assets
-Create
-Game Event
分别新建:
- 修改
Sword.cs
脚本:
- 修改
1_Longsword
的属性:
事件和UI
-
在
Scripts
添加脚本SwordMerchant.cs
脚本。using UnityEngine; using UnityEngine.UI; namespace RW.Scripts { public class SwordMerchant: MonoBehaviour { [SerializeField] private Text swordName; [SerializeField] private Text description; [SerializeField] private Image icon; [SerializeField] private Text goldCost; [SerializeField] private Text attackDamage; } public void UpdateDisplayUI(SwordData swordData) { swordName.text = swordData.SwordName; description.text = swordData.Description; icon.sprite = swordData.Icon; goldCost.text = swordData.GoldCost.ToString(); attackDamage.text = swordData.AttackDamage.ToString(); } }
-
将脚本挂载到
SwordMerchantCanvas
, 并且关联上面字段和预制。
-
在
SwordMerchantCanvas
上添加事件关联
上面以 Longsword 为例,需要重复执行同级的6个的预制操作。
-
运行