Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListToPopupDrawer.cs 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Svelto.ECS;
  7. using Svelto.ECS.Extensions.Unity;
  8. using UnityEditor;
  9. using UnityEngine;
  10. [CustomPropertyDrawer(typeof(ListToPopupAttribute))]
  11. public class ListToPopupDrawer : PropertyDrawer
  12. {
  13. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  14. {
  15. ListToPopupAttribute atb = attribute as ListToPopupAttribute;
  16. List<Type> stringList = null;
  17. if (atb.classType.GetField(atb.listName, BindingFlags.Static | BindingFlags.NonPublic) != null)
  18. {
  19. stringList = atb.classType.GetField(atb.listName, BindingFlags.Static | BindingFlags.NonPublic).GetValue(atb.classType) as List<Type>;
  20. }
  21. if (stringList != null && stringList.Count != 0)
  22. {
  23. int selectedIndex = Mathf.Max(0, stringList.FindIndex(t => t.Name == property.stringValue));
  24. selectedIndex = EditorGUI.Popup(position, property.name, selectedIndex, stringList.Select(t => t.Name).ToArray());
  25. property.stringValue = stringList[selectedIndex].Name;
  26. (property.serializedObject.targetObject as EntityDescriptorHolder).type =
  27. Activator.CreateInstance(stringList[selectedIndex]) as IEntityDescriptor;
  28. }
  29. else
  30. {
  31. EditorGUI.TextArea(position, "Error - no valid entity descriptors found");
  32. }
  33. }
  34. }
  35. #endif