CherepeninDYu CherepeninDYu - 17 days ago 7
C# Question

Show members of a class in Unity3D inspector

I need to edit this structure in object inspector just like on screenshot:
Structure

Edit



I have a script attached to my main object. it has a member named TestData which itself has two other members I and F.

How can I enable the Unity Inspector to edit TestData by its members (I, F) instead of the reference to TestData, When I click my main object?

Answer

Create a script in Unity and attach it to a GameObject. The script must be derived from MonoBehaviour

You can add public variables to the script and see them in the inspector.

c#:

public Vector3 StartPoint;

You can add [System.Serializable] to the class:

[System.Serializable]
public class TestData
{
     public int I;
     public int F;
}

Or if you want the inspector to show something other than the default editor panel you can define a CustomPropertyDrawer for your type.

[CustomPropertyDrawer(typeof(MyScript))]
public class MyScriptDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {

        //...
        EditorGUI.BeginProperty(position, label, property);
        EditorGUI.LabelField(new Rect(x, y, w, h), "title");
        EditorGUI.PropertyField(rect, I, GUIContent.none);
        //...
    }
}
Comments