Aiden VMR
Aiden VMR

Reputation: 35

The list in the inspector displays the class as standard, although the class has a custom inspector

I need to store a long list of "DataFrame" classes. The "DataFrame" has a lot of fields, so it stretches down a lot. I want to make a custom inspector for the DataFrame so that all its data is displayed in 3 lines.

When I redefine "OnInspectorGUI" for a class, it appears in the list as if "OnInspectorGUI" has not been redefined. At the same time, if I make the class "ScriptableObject" or "MonoBehaviour", it is displayed in the inspector with the redefined "OnInspectorGUI".

The problem is displaying only in the list

Here's what the sheet looks like: enter image description here

This is what a ScriptableObject looks like: enter image description here

Classes:

[System.Serializable]
public class DataFrame 
{
    public const int LINES_NUMBER = 10;

    [Header("Units")]
    public float UnitZombieHealthCoefficient;
    public int UnitMonsterNumber;
    [Space(5)]
    public float UnitEnemyCountCoefficient;

    [Header("Objects")]
    public int ObjTrapNumber;
    public int ObjCoinNumber;

    [Header("Control point Chance")]
    public float CpShopCoefficient;
    public float CpEventCoefficient;
    public float CpAbibityCoefficient;
    public float CpCoinsCoefficient;
}
[CustomEditor(typeof(DataFrame))]
public class DataStageEditor : Editor
{
    #region  SerializedProperties
    SerializedObject _serializedObject;

    SerializedProperty _zombieHealthCoefficient;
    SerializedProperty _monsterNumber;
    SerializedProperty _enemyCountCoefficient;

    SerializedProperty _trapNumber;
    SerializedProperty _coinNumber;

    SerializedProperty _shopCoefficient;
    SerializedProperty _eventCoefficient;
    SerializedProperty _abibityCoefficient;
    SerializedProperty _coinsCoefficient;
    #endregion


    private void OnEnable() {
        SetProperties();
    }

    private void SetProperties() {
        _serializedObject = new SerializedObject(target);

        _zombieHealthCoefficient = _serializedObject.FindProperty(nameof(DataFrame.UnitZombieHealthCoefficient));
        _monsterNumber = _serializedObject.FindProperty(nameof(DataFrame.UnitMonsterNumber));
        _enemyCountCoefficient = _serializedObject.FindProperty(nameof(DataFrame.UnitEnemyCountCoefficient));

        _trapNumber = _serializedObject.FindProperty(nameof(DataFrame.ObjTrapNumber));
        _coinNumber = _serializedObject.FindProperty(nameof(DataFrame.ObjCoinNumber));

        _shopCoefficient = _serializedObject.FindProperty(nameof(DataFrame.CpShopCoefficient));
        _eventCoefficient = _serializedObject.FindProperty(nameof(DataFrame.CpEventCoefficient));
        _abibityCoefficient = _serializedObject.FindProperty(nameof(DataFrame.CpAbibityCoefficient));
        _coinsCoefficient = _serializedObject.FindProperty(nameof(DataFrame.CpCoinsCoefficient));
    }

    public override void OnInspectorGUI() {

        EditorGUILayout.LabelField("TEST");
        EditorGUILayout.PropertyField(_zombieHealthCoefficient, true); 
        EditorGUILayout.PropertyField(_monsterNumber, true);
        EditorGUILayout.PropertyField(_enemyCountCoefficient, true);

        EditorGUILayout.Space(50);
        EditorGUILayout.PropertyField(_trapNumber, true);
        EditorGUILayout.PropertyField(_coinNumber, true);

        EditorGUILayout.PropertyField(_shopCoefficient, true);
        EditorGUILayout.PropertyField(_eventCoefficient, true);
        EditorGUILayout.PropertyField(_abibityCoefficient, true);
        EditorGUILayout.PropertyField(_coinsCoefficient, true);
    }
}

Without "[System.Serializable]" the list just doesn't show up

Upvotes: 3

Views: 37

Answers (1)

ipodtouch0218
ipodtouch0218

Reputation: 3346

CustomEditors are meant to be used for objects that inherit from UnityEngine.Object, which your class does not. This is why the ScriptableObject version of the class worked just fine. (I'm assuming there's some reason why you'd prefer to use a normal class and not a ScriptableObject here...)

You can instead use Property Drawers to have the editor display your serializable class in whatever way you see fit. They're a bit more difficult since you're required to handle / create the rects to draw within manually, rather than the automatic layout that custom editors use.

Source

Upvotes: 1

Related Questions