Scriptable Object Tutorial-Starter
在 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
分别创建:Inspector
面板修改右边的描述属性。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
挂载脚本: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
的属性:在 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){ = swordData. = swordData.Description;icon.sprite = swordData. = swordData.GoldCost.ToString(); = swordData.AttackDamage.ToString();}
}
将脚本挂载到 SwordMerchantCanvas
, 并且关联上面字段和预制。
在 SwordMerchantCanvas
上添加事件关联
上面以 Longsword 为例,需要重复执行同级的6个的预制操作。
运行
[1] ScriptableObject Tutorial: Getting Started
本文发布于:2024-02-01 08:33:31,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170674761335279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |