【问题标题】:Unity: adding custom function to Scriptable ObjectUnity:向 Scriptable Object 添加自定义功能
【发布时间】:2022-01-22 22:31:25
【问题描述】:

我想将 CardSO 添加到我的项目中 - 一个可编写脚本的对象。我想给它起一个名字、点数和一些卡片的特殊行为。如何向 SO 字段添加功能? 对于大多数卡片,它可以是空的(或者只返回 0),我希望我可以编写一个函数来获取 List 并返回 int。 有什么想法吗?

我当前的代码布局:

using UnityEngine;

[CreateAssetMenu(fileName = "CardSO", menuName = "New CardSO", order = 0)]
public class CardSO : ScriptableObject
{
    public string name;
    public int points;
    public Sprite Sprite;
    
    // public int SpecialBehavior(List<CardSO>);
}

谢谢!

【问题讨论】:

    标签: function unity3d scriptable-object


    【解决方案1】:

    嗯......只是实现它。如果您需要大多数没有它的卡片,为什么不使用基类并创建方法virtual

    // Whatever shall be the default behavior
    public virtual int SpecialBehavior(List<CardSO> cards) => -1;
    

    然后制作一个特殊的卡片子类,可以override这个方法并返回你需要的任何东西

    [CreateAssetMenu]
    public class SpecialCardSO : CardSO
    {
        public override int SpecialBehavior (List<CardSO> cards)
        {
            // or whatever
            return cards.Length;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多