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.

43 lines
1.5KB

  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 =
  20. atb.classType.GetField(atb.listName, BindingFlags.Static | BindingFlags.NonPublic)
  21. .GetValue(atb.classType) as List<Type>;
  22. }
  23. if (stringList != null && stringList.Count != 0)
  24. {
  25. int selectedIndex = Mathf.Max(0, stringList.FindIndex(t => t.Name == property.stringValue));
  26. selectedIndex = EditorGUI.Popup(position, property.name, selectedIndex
  27. , stringList.Select(t => t.Name).ToArray());
  28. property.stringValue = stringList[selectedIndex].Name;
  29. (property.serializedObject.targetObject as EntityDescriptorHolder).type =
  30. Activator.CreateInstance(stringList[selectedIndex]) as IEntityDescriptor;
  31. }
  32. else
  33. {
  34. EditorGUI.TextArea(position, "Error - no valid entity descriptors found");
  35. }
  36. }
  37. }
  38. #endif