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.

100 lines
3.8KB

  1. #if UNITY_5 || UNITY_5_3_OR_NEWER
  2. using UnityEngine;
  3. namespace Svelto.ECS.Unity
  4. {
  5. public static class SveltoGUIHelper
  6. {
  7. public static T CreateFromPrefab<T>(ref uint startIndex, Transform contextHolder, IEntityFactory factory,
  8. ExclusiveGroup group, string groupNamePostfix = null) where T : MonoBehaviour, IEntityDescriptorHolder
  9. {
  10. var holder = Create<T>(new EGID(startIndex++, group), contextHolder, factory);
  11. var childs = contextHolder.GetComponentsInChildren<IEntityDescriptorHolder>(true);
  12. foreach (var child in childs)
  13. {
  14. if (child.GetType() != typeof(T))
  15. {
  16. var monoBehaviour = child as MonoBehaviour;
  17. var childImplementors = monoBehaviour.GetComponents<IImplementor>();
  18. startIndex = InternalBuildAll(
  19. startIndex,
  20. child,
  21. factory,
  22. group,
  23. childImplementors,
  24. groupNamePostfix);
  25. }
  26. }
  27. return holder;
  28. }
  29. public static T Create<T>(EGID ID, Transform contextHolder, IEntityFactory factory)
  30. where T : MonoBehaviour, IEntityDescriptorHolder
  31. {
  32. var holder = contextHolder.GetComponentInChildren<T>(true);
  33. DBC.ECS.Check.Assert(holder != null, $"`{nameof(holder)}` is null! No component of type " +
  34. $"`{typeof(T)}` was found between its children.");
  35. var implementors = holder.GetComponents<IImplementor>();
  36. factory.BuildEntity(ID, holder.GetDescriptor(), implementors);
  37. return holder;
  38. }
  39. public static EntityStructInitializer CreateWithEntity<T>(EGID ID, Transform contextHolder,
  40. IEntityFactory factory, out T holder)
  41. where T : MonoBehaviour, IEntityDescriptorHolder
  42. {
  43. holder = contextHolder.GetComponentInChildren<T>(true);
  44. var implementors = holder.GetComponents<IImplementor>();
  45. return factory.BuildEntity(ID, holder.GetDescriptor(), implementors);
  46. }
  47. public static uint CreateAll<T>(uint startIndex, ExclusiveGroup group,
  48. Transform contextHolder, IEntityFactory factory, string groupNamePostfix = null) where T : MonoBehaviour, IEntityDescriptorHolder
  49. {
  50. var holders = contextHolder.GetComponentsInChildren<T>(true);
  51. foreach (var holder in holders)
  52. {
  53. var implementors = holder.GetComponents<IImplementor>();
  54. startIndex = InternalBuildAll(startIndex, holder, factory, group, implementors, groupNamePostfix);
  55. }
  56. return startIndex;
  57. }
  58. static uint InternalBuildAll(uint startIndex, IEntityDescriptorHolder descriptorHolder,
  59. IEntityFactory factory, ExclusiveGroup group, IImplementor[] implementors, string groupNamePostfix)
  60. {
  61. ExclusiveGroup.ExclusiveGroupStruct realGroup = group;
  62. if (string.IsNullOrEmpty(descriptorHolder.groupName) == false)
  63. {
  64. realGroup = ExclusiveGroup.Search(!string.IsNullOrEmpty(groupNamePostfix)
  65. ? $"{descriptorHolder.groupName}{groupNamePostfix}"
  66. : descriptorHolder.groupName);
  67. }
  68. EGID egid;
  69. var holderId = descriptorHolder.id;
  70. if (holderId == 0)
  71. egid = new EGID(startIndex++, realGroup);
  72. else
  73. egid = new EGID(holderId, realGroup);
  74. var init = factory.BuildEntity(egid, descriptorHolder.GetDescriptor(), implementors);
  75. init.Init(new EntityHierarchyStruct(group));
  76. return startIndex;
  77. }
  78. }
  79. }
  80. #endif