Raywenderlich: ScriptableObject 教程

阅读: 评论:0

Raywenderlich: ScriptableObject 教程

Raywenderlich: ScriptableObject 教程

文章目录

  • 下载
  • 创建 ScriptableObject
  • 增加数据
  • 使用 ScriptableObject
  • 基于 ScriptableObjects 的事件
  • 添加事件脚本
  • 事件和UI

下载

Scriptable Object Tutorial-Starter

创建 ScriptableObject

  1. 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, 为二级菜单。

增加数据

  1. 新建目录 Scripts - Scriptable Objects - Sword Data。在新建的目录下面,点击 Assets - Create - Sword Data 分别创建:

    分别点击上面7个新建的对象,在 Inspector 面板修改右边的描述属性。

使用 ScriptableObject

  1. 修改 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);}
    }
    
  2. 1_Longsword 挂载脚本:

基于 ScriptableObjects 的事件

添加事件脚本

  1. Scripts 目录下新建, GameEvent.csGameEventListener.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();}
    }
    
  2. 点击 Assets - Create - Game Event 分别新建:
  3. 修改 Sword.cs 脚本:
  4. 修改 1_Longsword 的属性:

事件和UI

  1. 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();}
    }
    
  2. 将脚本挂载到 SwordMerchantCanvas , 并且关联上面字段和预制。

  3. SwordMerchantCanvas 上添加事件关联

    上面以 Longsword 为例,需要重复执行同级的6个的预制操作。

  4. 运行

[1] ScriptableObject Tutorial: Getting Started

本文发布于:2024-02-01 08:33:31,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170674761335279.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23