#if UNITY_5 || UNITY_5_3_OR_NEWER using UnityEngine; namespace Svelto.ECS.Unity { public static class SveltoGUIHelper { public static T CreateFromPrefab(ref uint startIndex, Transform contextHolder, IEntityFactory factory, ExclusiveGroup group, string groupNamePostfix = null) where T : MonoBehaviour, IEntityDescriptorHolder { var holder = Create(new EGID(startIndex++, group), contextHolder, factory); var childs = contextHolder.GetComponentsInChildren(true); foreach (var child in childs) { if (child.GetType() != typeof(T)) { var monoBehaviour = child as MonoBehaviour; var childImplementors = monoBehaviour.GetComponents(); startIndex = InternalBuildAll( startIndex, child, factory, group, childImplementors, groupNamePostfix); } } return holder; } public static T Create(EGID ID, Transform contextHolder, IEntityFactory factory) where T : MonoBehaviour, IEntityDescriptorHolder { var holder = contextHolder.GetComponentInChildren(true); DBC.ECS.Check.Assert(holder != null, $"`{nameof(holder)}` is null! No component of type " + $"`{typeof(T)}` was found between its children."); var implementors = holder.GetComponents(); factory.BuildEntity(ID, holder.GetDescriptor(), implementors); return holder; } public static EntityStructInitializer CreateWithEntity(EGID ID, Transform contextHolder, IEntityFactory factory, out T holder) where T : MonoBehaviour, IEntityDescriptorHolder { holder = contextHolder.GetComponentInChildren(true); var implementors = holder.GetComponents(); return factory.BuildEntity(ID, holder.GetDescriptor(), implementors); } public static uint CreateAll(uint startIndex, ExclusiveGroup group, Transform contextHolder, IEntityFactory factory, string groupNamePostfix = null) where T : MonoBehaviour, IEntityDescriptorHolder { var holders = contextHolder.GetComponentsInChildren(true); foreach (var holder in holders) { var implementors = holder.GetComponents(); startIndex = InternalBuildAll(startIndex, holder, factory, group, implementors, groupNamePostfix); } return startIndex; } static uint InternalBuildAll(uint startIndex, IEntityDescriptorHolder descriptorHolder, IEntityFactory factory, ExclusiveGroup group, IImplementor[] implementors, string groupNamePostfix) { ExclusiveGroup.ExclusiveGroupStruct realGroup = group; if (string.IsNullOrEmpty(descriptorHolder.groupName) == false) { realGroup = ExclusiveGroup.Search(!string.IsNullOrEmpty(groupNamePostfix) ? $"{descriptorHolder.groupName}{groupNamePostfix}" : descriptorHolder.groupName); } EGID egid; var holderId = descriptorHolder.id; if (holderId == 0) egid = new EGID(startIndex++, realGroup); else egid = new EGID(holderId, realGroup); var init = factory.BuildEntity(egid, descriptorHolder.GetDescriptor(), implementors); init.Init(new EntityHierarchyStruct(group)); return startIndex; } } } #endif