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.

69 lines
2.2KB

  1. #if UNITY_5 || UNITY_5_3_OR_NEWER
  2. using System;
  3. using System.Collections.Generic;
  4. using Svelto.ECS.Hybrid;
  5. using UnityEngine;
  6. namespace Svelto.ECS.Extensions.Unity
  7. {
  8. public class ListToPopupAttribute : PropertyAttribute
  9. {
  10. public Type classType;
  11. public string listName;
  12. public ListToPopupAttribute(Type classType, string listName)
  13. {
  14. this.classType = classType;
  15. this.listName = listName;
  16. }
  17. }
  18. /// <summary>
  19. /// I introduced this option thinking it could be a good idea, but I am not sure anymore. Although it's slightly
  20. /// more annoying, extending GenericEntityDescriptorHolder is wiser than using this class.
  21. /// Consider this experimental
  22. /// Todo: sort in alphabetic order
  23. /// Todo: hide inner descriptors
  24. /// </summary>
  25. public class EntityDescriptorHolder : MonoBehaviour, IEntityDescriptorHolder
  26. {
  27. public IEntityDescriptor GetDescriptor() { return type; }
  28. public string groupName => _groupName;
  29. public ushort id => _id;
  30. [Tooltip(
  31. "it's possible to name groups and query group by name. This entity will be created in a named group if inserted")]
  32. [SerializeField]
  33. string _groupName;
  34. [Tooltip("this entity will be created with the selected ID, if inserted. An ID must be unique in each group")]
  35. [SerializeField]
  36. ushort _id;
  37. [Tooltip("choose the entity type, not optional")]
  38. [ListToPopup(typeof(EntityDescriptorHolder), "DescriptorList")]
  39. [SerializeField]
  40. string Descriptor;
  41. internal IEntityDescriptor type;
  42. static List<Type> DescriptorList = new List<Type>();
  43. static EntityDescriptorHolder()
  44. {
  45. var assemblies = AssemblyUtility.GetCompatibleAssemblies();
  46. Type d1 = typeof(IEntityDescriptor);
  47. foreach (var assembly in assemblies)
  48. foreach (Type type in AssemblyUtility.GetTypesSafe(assembly))
  49. {
  50. if (type != null && d1.IsAssignableFrom(type) && type.IsAbstract == false)
  51. {
  52. DescriptorList.Add(type);
  53. }
  54. }
  55. }
  56. }
  57. }
  58. #endif