/// <summary> /// Registers a a reactive group with the list of managers. If the group already exists it will return it, if not it will create a new one and return that. /// </summary> /// <param name="componentId"></param> /// <typeparam name="TGroupType">The group type class. Usually derives from ReactiveGroup </typeparam> /// <typeparam name="TComponent"></typeparam> /// <returns>The instance of the group manager.</returns> public TGroupType RegisterGroup <TGroupType, TComponent>(int componentId = 0) where TComponent : IEcsComponent where TGroupType : IReactiveGroup, new() { IEcsComponentManager existing; if (!ComponentManagers.TryGetValue(typeof(TComponent), out existing)) { existing = new TGroupType(); ComponentManagers.Add(typeof(TComponent), existing); if (existing.ComponentId > 0) { try { ComponentManagersById.Add(existing.ComponentId, existing); } catch (ArgumentException ex) { Debug.LogErrorFormat("Cannot register component {0} with ID {1}. Component with such Id is already registered: {2}.", typeof(TGroupType).Name, existing.ComponentId, ComponentManagersById[existing.ComponentId].For.Name); } } return((TGroupType)existing); } else { return((TGroupType)existing); } }
/// <summary> /// This method should be used to add any entity to the ecs component system /// /// > You can use this if you want to register components that aren't derived from EcsComponent which requires MonoBehaviour, but you won't be able to see it in the unity inspector. /// </summary> /// <param name="componentType">The type of component to register.</param> /// <param name="instance">The actual instance that is being registered</param> /// <code> /// System.ComponentSystem.RegisterComponent(typeof(CustomComponent), new CustomComponent()); /// </code> public void RegisterComponentInstance(Type componentType, IEcsComponent instance) { IEcsComponentManager existing; if (!ComponentManagers.TryGetValue(componentType, out existing)) { var type = typeof(EcsComponentManagerOf <>).MakeGenericType(componentType); existing = Activator.CreateInstance(type) as EcsComponentManager; ComponentManagers.Add(componentType, existing); if (instance.ComponentId > 0) { ComponentManagersById.Add(instance.ComponentId, existing); } } if (_componentCreatedSubject != null) { _componentCreatedSubject.OnNext(instance); } existing.RegisterComponent(instance); }
public IEcsComponentManagerOf <TComponent> RegisterComponent <TComponent>(int componentId = 0) where TComponent : class, IEcsComponent { IEcsComponentManager existing; if (!ComponentManagers.TryGetValue(typeof(TComponent), out existing)) { existing = new EcsComponentManagerOf <TComponent>(); existing.ComponentId = componentId; ComponentManagers.Add(typeof(TComponent), existing); //if (componentId > 0) // ComponentManagersById.Add(componentId, existing); //else //{ // // Throw warning here? //} return((IEcsComponentManagerOf <TComponent>)existing); } else { return((IEcsComponentManagerOf <TComponent>)existing); } }