How can I create a List<CustomClass> through the Editor in the inspector ?

I created a list of custom class I need to use. When I create a property field for this list I cannot edit the elements of the list. What should I change in my code ?

Here’s how it looks like:
9508573--1339798--upload_2023-12-3_12-55-26.png

Here’s the code:

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Rendering;
using UnityEngine;

namespace Interaction
{
    public enum InteractableType
    {
        Bed,
        Searchable
    }

    [Serializable]
    public class Item
    {
        public ItemData itemData;
        [Range(0f, 100f)] public float chance = 100f;
        [HideInInspector] public float weight;
    }

    [Serializable]
    [CreateAssetMenu(fileName = "Interactable", menuName = "Scriptables/Interactable", order = 2)]
    public class InteractionData : ScriptableObject
    {
        public string interactableName;
        public InteractableType interactableType;

        [Header("Bed Properties")] [HideInInspector]
        public float warmthBonus;

        [Header("Searchable Properties")] [HideInInspector]
        public List<Item> items = new();
    }

    [CustomEditor(typeof(InteractionData))]
    public class InteractionDataEditor : Editor
    {
        private SerializedProperty warmthBonus;
        private SerializedProperty items;

        private void OnEnable()
        {
            warmthBonus = serializedObject.FindProperty("warmthBonus");
            items = serializedObject.FindProperty("items");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            InteractionData interactionData = (InteractionData)target;

            DrawDefaultInspector();

            if (interactionData.interactableType == InteractableType.Bed)
            {
                EditorGUILayout.PropertyField(warmthBonus);
            }
            else if (interactionData.interactableType == InteractableType.Searchable)
            {
                EditorGUILayout.PropertyField(items);
            }

            serializedObject.ApplyModifiedProperties();
        }
    }
}

I would not put all the classes in the same script.
I also don’t see how you could possibly add classes to a list.

I put the classes together so everyone could see the whole script.

Of course you can. When I delete [HideInInspector] attribute it works just fine and displays the list with the content of the class, but I don’t know how to specifically tell EditorGUI to display the class content.

You need a PropertyDrawer that is handling the drawing of Item elements.

2 Likes

You’re looking for something like Odin’s [InlineEditor]… as @CodeSmile said you need to create a custom editor for this.

Well, the issue is this:

Attributes attached to any serializable collection (arrays or Lists) are not applied to the collection but to each element. It’s actually not possible to attach an attribute to an array or list unless you wrap it in a separate class or struct. Since you put a HideInInspector on the array it means the default inspector would not draw the elements but still draws the array.

Since you create your own inspector, why do you actually use HideInInspector and “DrawDefaultInspector”? Remove than and just draw the properties you want to see.

Though be careful with such setups. We don’t know what your “ItemData” class / struct contains. However if there are serialized references in there it would mean when you switch your “interactableType” to “Bed” all that data would be hidden but still there. So such cases could result in having hidden references to assets which would be inluded in a build and it would be quite hard to debug.