首页 > 解决方案 > Unity:继承的事件属性的自定义编辑器

问题描述

假设我有这个设置

public class SomeClassMadeByUnity
{
    public int someUnneededVariable;
    public UnityEvent someEvent { get; set; }
}

public class MyChild : SomeClassMadeByUnity
{
    
}

SomeClassMadeByUnity在封闭源代码下有一个自定义编辑器,但我想someEvent自己在自​​定义编辑器中公开。

我该怎么办?

标签: c#unity3d

解决方案


这里实际上不需要/使用自定义编辑器。

“问题”是Unity 没有序列化属性。所以自定义编辑器不会有太大帮助。即使您以某种方式设法在 Inspector 中公开这样一个字段,它的任何更改也不会被存储

您实际上需要的只是一个序列化的支持字段,例如

[Serializable]
public class SomeClass
{
    public int someUnneededVariable;

    // You probably wouldn't need the setter anyway 
    public UnityEvent someEvent { get => _someEvent; set => _someEvent = value; }

    [SerializeField] private UnityEvent _someEvent;
}

[Serializable]
public class MyChild : SomeClass
{
    
}

true或者,如果您已经有父类型的自定义编辑器,您可以简单地确保您传入CustomEditor

EditorForChildClasses

如果true是,inspectedType 的子类也将显示此编辑器。默认为false.


推荐阅读