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:
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();
}
}
}