Tema
Tema

Reputation: 1358

Extending UnityEngine.UI.Image and add extra field available in Inspector

I am trying to extend UnityEngine.UI.Image like that

public class MyImage : Image {
   public string Comment;
}

But I do not see extra text field Comment in inspector. Is it possible to add extra field that would be available in inspector?

PS It triggered as duplicated for Extending Unity UI components with custom Inspector but it is not dupe. I do not ask anything about custom Inspector. It is just regular field with default Inspector. The problem is that field is not appearing in inspector at all.

Upvotes: 2

Views: 2827

Answers (1)

zwcloud
zwcloud

Reputation: 4889

Unfortunately, the Inspector GUI cannot inherit from base class automatically. You need to write that yourself, just like what is described in 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);
    }
}

Result: MyImage's Inspector GUI

Upvotes: 6

Related Questions