- Home /
Question by
mirek_mazur · Nov 24, 2021 at 08:25 PM ·
arrayslistsdisplay
Displaying a List or an Array in Editor file
Hello everyone
I have a question about displaying a List or an Array After creating a List, I can add, remove or assign List items. The problem starts when I create the Custom Editor. I cannot assign a value to the list.
MonoBehaviour: RootTest.cs
using System.Collections.Generic;
using UnityEngine;
public class RootTest : MonoBehaviour
{
[SerializeField] private List<GameObject> card = new List<GameObject>();
}
.. and Editor: RootTestEditor.cs
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(RootTest))]
public class RootTestEditor : Editor
{
private RootTest myTarget;
private SerializedObject soTarget;
private SerializedProperty card;
public override void OnInspectorGUI()
{
myTarget = (RootTest)target;
soTarget = new SerializedObject(target);
card = soTarget.FindProperty("card");
soTarget.Update();
ExecuteMenu();
soTarget.ApplyModifiedProperties();
}
private void ExecuteMenu()
{
EditorGUILayout.PropertyField(card, new GUIContent("Card"));
}
}
I understand the problem is the Editor file which does not refresh the values. And the new item cannot be assigned.
Does anyone know how to do it ?
Comment
Answer by CodesCove · Nov 24, 2021 at 08:50 PM
One thing is that you should always start the OnInspectorGUI with serializedObject.Update().
Other thing is that you are doing unnecessary serializations. This way would be simpler..
private SerializedProperty card;
private void OnEnable()
{
card = serializedObject.FindProperty("card");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(card, new GUIContent("ECard"));
serializedObject.ApplyModifiedProperties();
}
Other that above things I don't see any problems