扩展UnityEngine.UI.Image并添加Inspector中可用的额外字段

时间:2017-03-10 11:46:00

标签: unity3d unity5 unity3d-gui unity3d-editor

我正在尝试像{/ p>那样扩展UnityEngine.UI.Image

public class MyImage : Image {
   public string Comment;
}

但是我在检查员中看不到额外的文本字段Comment。是否可以添加检查员可用的额外字段?

PS 它触发了Extending Unity UI components with custom Inspector的重复但不是愚蠢的。我对custom Inspector没有任何疑问。它只是default Inspector的常规字段。问题是该领域根本没有出现在检查员身上。

1 个答案:

答案 0 :(得分:5)

不幸的是,Inspector GUI无法自动从基类继承。您需要自己编写,就像Extending Unity UI components with custom Inspector中描述的那样。

MyImage.cs

using UnityEngine;
using UnityEngine.UI;

[ExecuteInEditMode]
public class MyImage : Image
{
    public string Comment;
}

MyImageEditor.cs

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(MyImage))]
public class MyImageEditor : UnityEditor.UI.ImageEditor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();//Draw inspector UI of ImageEditor

        MyImage image = (MyImage)target;
        image.Comment = EditorGUILayout.TextField("Comment", image.Comment);
    }
}

结果: MyImage's Inspector GUI