r/UnityHelp • u/planetidiot • Jun 08 '23
How to make enum-driven CustomEditor that allows me to drag prefabs to each field, and get them in code?
I've gotten this much:
using UnityEngine;
using UnityEditor;
namespace Creatures
{
[CustomEditor(typeof(CreatureSpawner))]
public class CreatureSpawnerEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
CreatureSpawner creatureSpawner = (CreatureSpawner)target;
foreach (CreatureLookup.CreatureTypes creatureType in System.Enum.GetValues(typeof(CreatureLookup.CreatureTypes)))
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(creatureType.ToString(), GUILayout.Width(100));
EditorGUILayout.ObjectField(null, typeof(GameObject), false);
EditorGUILayout.EndHorizontal();
}
serializedObject.ApplyModifiedProperties();
}
}
}It got me really excited to see the creature types showing up! However, I have absolutely no idea how to get these values OUT. How do I use them from my CreatureSpawner code? I want to be able to call SpawnCreature(creatureType) and have it instantiate the prefab assigned in the editor.
3
Upvotes
1
u/nulldiver Jun 08 '23
At this point, you’re not serializing anything, just drawing UI for the object fields. You need to assign those values back to something serialized in the spawner until then, there is nothing to get out.