首页 > 解决方案 > 如何创建一个自定义编辑器脚本,在 Unity 的检查器中显示非本地类?

问题描述

我有一个没有扩展本机类的类,因此该类型的变量不会显示在检查器中。

如果我要通过原生类扩展所述类,则该类型的变量将显示在检查器中,但我实际上无法将它们设置为任何东西,因为它们包含在 ScriptableObject 中,显然这两种类型没有不适合。

我尝试编写 CustomPropertyDrawer,但它仍然没有显示变量。这是我的代码(在 C# 中):

using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(AScriptableObjectClassThatShouldDisplayTheVariableInItsInspector))]
public class ShowVariable : PropertyDrawer
{

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        base.OnGUI(position, property, label);

        EditorGUI.BeginProperty(position, label, property);

        Rect rect = new Rect(position.x, position.y, 50, position.height);

        EditorGUI.PropertyField(rect, property.FindPropertyRelative("nameOfVariable"));

        EditorGUI.EndProperty();
    }

}

这是包含应该显示的类的变量的类:

[System.Serializable]
public class Syntax : ScriptableObject
{
  public GameLevelFunction nameOfVariable;
}

这基本上就是我的类应该在检查器中显示的样子:

[System.Serializable]
public abstract class GameLevelFunction
{
  //literally two methods
}

标签: c#unity3dunity-editor

解决方案


推荐阅读