/// <summary> /// 初始化助手 /// </summary> public void OnInitialization() { _module = Module as EntityManager; _entityRoot = _module.transform.Find("EntityRoot"); List <Type> types = ReflectionToolkit.GetTypesInRunTimeAssemblies(type => { return(type.IsSubclassOf(typeof(EntityLogicBase)) && !type.IsAbstract); }); for (int i = 0; i < types.Count; i++) { EntityResourceAttribute attribute = types[i].GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { Entities.Add(types[i], new List <EntityLogicBase>()); GameObject group = new GameObject(types[i].Name + "[Group]"); group.transform.SetParent(_entityRoot); group.transform.localPosition = Vector3.zero; group.transform.localRotation = Quaternion.identity; group.transform.localScale = Vector3.one; group.SetActive(true); _entitiesGroup.Add(types[i], group); ObjectPools.Add(types[i], new Queue <GameObject>()); } else { throw new HTFrameworkException(HTFrameworkModule.Entity, "创建实体逻辑对象失败:实体逻辑类 " + types[i].Name + " 丢失 EntityResourceAttribute 标记!"); } } }
private void RecoveryEntity(EntityLogic entityLogic) { Type type = entityLogic.GetType(); EntityResourceAttribute attribute = type.GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { if (_entities.ContainsKey(type)) { if (attribute.IsUseObjectPool) { _entities[type].Remove(entityLogic); entityLogic.OnDestroy(); Main.m_ReferencePool.Despawn(entityLogic); _objectPool[type].Enqueue(entityLogic.Entity); entityLogic.Entity.SetActive(false); entityLogic.Entity = null; entityLogic = null; } else { _entities[type].Remove(entityLogic); entityLogic.OnDestroy(); Main.m_ReferencePool.Despawn(entityLogic); Destroy(entityLogic.Entity); entityLogic.Entity = null; entityLogic = null; } } else { GlobalTools.LogError(string.Format("销毁实体失败:实体对象 {0} 并未存在!", type.Name)); } } }
//回收实体 private void RecoveryEntity(EntityLogicBase entityLogic) { Type type = entityLogic.GetType(); EntityResourceAttribute attribute = type.GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { if (Entities.ContainsKey(type)) { if (attribute.IsUseObjectPool) { Entities[type].Remove(entityLogic); entityLogic.OnDestroy(); Main.m_ReferencePool.Despawn(entityLogic); ObjectPools[type].Enqueue(entityLogic.Entity); entityLogic.Entity.SetActive(false); entityLogic.Entity = null; entityLogic = null; } else { Entities[type].Remove(entityLogic); entityLogic.OnDestroy(); Main.m_ReferencePool.Despawn(entityLogic); Main.Kill(entityLogic.Entity); entityLogic.Entity = null; entityLogic = null; } } else { throw new HTFrameworkException(HTFrameworkModule.Entity, "销毁实体失败:实体对象 " + type.Name + " 并未存在!"); } } }
public override void OnInitialization() { base.OnInitialization(); _entityRoot = transform.FindChildren("EntityRoot"); //创建所有实体的逻辑对象 List <Type> types = GlobalTools.GetTypesInRunTimeAssemblies(); for (int i = 0; i < types.Count; i++) { if (types[i].IsSubclassOf(typeof(EntityLogic))) { EntityResourceAttribute attribute = types[i].GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { _entities.Add(types[i], new List <EntityLogic>()); GameObject group = new GameObject(types[i].Name + "[Group]"); group.transform.SetParent(_entityRoot.transform); group.transform.localPosition = Vector3.zero; group.transform.localRotation = Quaternion.identity; group.transform.localScale = Vector3.one; group.SetActive(true); _entitiesGroup.Add(types[i], group); _objectPool.Add(types[i], new Queue <GameObject>()); } else { GlobalTools.LogError(string.Format("创建实体逻辑对象失败:实体逻辑类 {0} 丢失 EntityResourceAttribute 标记!", types[i].Name)); } } } }
/// <summary> /// 创建实体 /// </summary> /// <param name="type">实体逻辑类</param> /// <param name="entityName">实体指定名称(为 <None> 时默认使用实体逻辑类名称)</param> /// <param name="loadingAction">创建实体过程进度回调</param> /// <param name="loadDoneAction">创建实体完成回调</param> /// <returns>加载协程</returns> public Coroutine CreateEntity(Type type, string entityName, HTFAction <float> loadingAction, HTFAction <EntityLogicBase> loadDoneAction) { EntityResourceAttribute attribute = type.GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { if (Entities.ContainsKey(type)) { if (attribute.IsUseObjectPool && ObjectPools[type].Count > 0) { EntityLogicBase entityLogic = GenerateEntity(type, ObjectPools[type].Dequeue(), entityName == "<None>" ? type.Name : entityName); loadingAction?.Invoke(1); loadDoneAction?.Invoke(entityLogic); Main.m_Event.Throw(this, Main.m_ReferencePool.Spawn <EventCreateEntitySucceed>().Fill(entityLogic)); return(null); } else { if (_defineEntities.ContainsKey(type.FullName) && _defineEntities[type.FullName] != null) { EntityLogicBase entityLogic = GenerateEntity(type, Main.Clone(_defineEntities[type.FullName], _entitiesGroup[type].transform), entityName == "<None>" ? type.Name : entityName); loadingAction?.Invoke(1); loadDoneAction?.Invoke(entityLogic); Main.m_Event.Throw(this, Main.m_ReferencePool.Spawn <EventCreateEntitySucceed>().Fill(entityLogic)); return(null); } else { return(Main.m_Resource.LoadPrefab(new PrefabInfo(attribute), _entitiesGroup[type].transform, loadingAction, (obj) => { EntityLogicBase entityLogic = GenerateEntity(type, obj, entityName == "<None>" ? type.Name : entityName); loadDoneAction?.Invoke(entityLogic); Main.m_Event.Throw(this, Main.m_ReferencePool.Spawn <EventCreateEntitySucceed>().Fill(entityLogic)); })); } } } else { throw new HTFrameworkException(HTFrameworkModule.Entity, "创建实体失败:实体对象 " + type.Name + " 并未存在!"); } } return(null); }
//提取实体 private Coroutine ExtractEntity(Type type, string entityName = "<None>", HTFAction <float> loadingAction = null, HTFAction <EntityLogicBase> loadDoneAction = null) { EntityResourceAttribute attribute = type.GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { if (_entities.ContainsKey(type)) { if (attribute.IsUseObjectPool && _objectPool[type].Count > 0) { EntityLogicBase entityLogic = Main.m_ReferencePool.Spawn(type) as EntityLogicBase; _entities[type].Add(entityLogic); entityLogic.Entity = _objectPool[type].Dequeue(); entityLogic.Entity.name = entityLogic.Name = entityName == "<None>" ? type.Name : entityName; entityLogic.Entity.SetActive(true); entityLogic.OnInit(); entityLogic.OnShow(); loadingAction?.Invoke(1); loadDoneAction?.Invoke(entityLogic); Main.m_Event.Throw(this, Main.m_ReferencePool.Spawn <EventCreateEntitySucceed>().Fill(entityLogic)); return(null); } else { return(Main.m_Resource.LoadPrefab(new PrefabInfo(attribute), _entitiesGroup[type].transform, loadingAction, (obj) => { EntityLogicBase entityLogic = Main.m_ReferencePool.Spawn(type) as EntityLogicBase; _entities[type].Add(entityLogic); entityLogic.Entity = obj; entityLogic.Entity.name = entityLogic.Name = entityName == "<None>" ? type.Name : entityName; entityLogic.Entity.SetActive(true); entityLogic.OnInit(); entityLogic.OnShow(); loadDoneAction?.Invoke(entityLogic); Main.m_Event.Throw(this, Main.m_ReferencePool.Spawn <EventCreateEntitySucceed>().Fill(entityLogic)); })); } } else { GlobalTools.LogError(string.Format("创建实体失败:实体对象 {0} 并未存在!", type.Name)); } } return(null); }
internal override void OnInitialization() { base.OnInitialization(); for (int i = 0; i < DefineEntityNames.Count; i++) { if (!_defineEntities.ContainsKey(DefineEntityNames[i])) { _defineEntities.Add(DefineEntityNames[i], DefineEntityTargets[i]); } } _entityRoot = transform.Find("EntityRoot"); //创建所有实体的逻辑对象 List <Type> types = ReflectionToolkit.GetTypesInRunTimeAssemblies(type => { return(type.IsSubclassOf(typeof(EntityLogicBase))); }); for (int i = 0; i < types.Count; i++) { EntityResourceAttribute attribute = types[i].GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { _entities.Add(types[i], new List <EntityLogicBase>()); GameObject group = new GameObject(types[i].Name + "[Group]"); group.transform.SetParent(_entityRoot); group.transform.localPosition = Vector3.zero; group.transform.localRotation = Quaternion.identity; group.transform.localScale = Vector3.one; group.SetActive(true); _entitiesGroup.Add(types[i], group); _objectPool.Add(types[i], new Queue <GameObject>()); } else { throw new HTFrameworkException(HTFrameworkModule.Entity, "创建实体逻辑对象失败:实体逻辑类 " + types[i].Name + " 丢失 EntityResourceAttribute 标记!"); } } }
//批量回收实体 private void RecoveryEntities(Type type) { EntityResourceAttribute attribute = type.GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { if (_entities.ContainsKey(type)) { if (attribute.IsUseObjectPool) { for (int i = 0; i < _entities[type].Count; i++) { EntityLogicBase entityLogic = _entities[type][i]; entityLogic.OnDestroy(); Main.m_ReferencePool.Despawn(entityLogic); _objectPool[type].Enqueue(entityLogic.Entity); entityLogic.Entity.SetActive(false); entityLogic.Entity = null; } _entities[type].Clear(); } else { for (int i = 0; i < _entities[type].Count; i++) { EntityLogicBase entityLogic = _entities[type][i]; entityLogic.OnDestroy(); Main.m_ReferencePool.Despawn(entityLogic); Main.Kill(entityLogic.Entity); entityLogic.Entity = null; } _entities[type].Clear(); } } else { throw new HTFrameworkException(HTFrameworkModule.Entity, "销毁实体失败:实体对象 " + type.Name + " 并未存在!"); } } }
private void ExtractEntity(Type type, HTFAction <float> loadingAction = null) { EntityResourceAttribute attribute = type.GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { if (_entities.ContainsKey(type)) { if (attribute.IsUseObjectPool && _objectPool[type].Count > 0) { EntityLogic entityLogic = Main.m_ReferencePool.Spawn(type) as EntityLogic; entityLogic.Entity = _objectPool[type].Dequeue(); entityLogic.Entity.SetActive(true); entityLogic.OnInit(); entityLogic.OnShow(); _entities[type].Add(entityLogic); Main.m_Event.Throw(this, Main.m_ReferencePool.Spawn <EventCreateEntitySucceed>().Fill(entityLogic)); } else { Main.m_Resource.LoadPrefab(new PrefabInfo(attribute), _entitiesGroup[type].transform, loadingAction, (obj) => { EntityLogic entityLogic = Main.m_ReferencePool.Spawn(type) as EntityLogic; entityLogic.Entity = obj; entityLogic.Entity.SetActive(true); entityLogic.OnInit(); entityLogic.OnShow(); _entities[type].Add(entityLogic); Main.m_Event.Throw(this, Main.m_ReferencePool.Spawn <EventCreateEntitySucceed>().Fill(entityLogic)); }); } } else { GlobalTools.LogError(string.Format("创建实体失败:实体对象 {0} 并未存在!", type.Name)); } } }
//批量回收实体 private void RecoveryEntities(Type type) { EntityResourceAttribute attribute = type.GetCustomAttribute <EntityResourceAttribute>(); if (attribute != null) { if (_entities.ContainsKey(type)) { if (attribute.IsUseObjectPool) { for (int i = 0; i < _entities[type].Count; i++) { EntityLogicBase entityLogic = _entities[type][i]; entityLogic.OnDestroy(); Main.m_ReferencePool.Despawn(entityLogic); _objectPool[type].Enqueue(entityLogic.Entity); entityLogic.Entity.SetActive(false); entityLogic.Entity = null; } _entities[type].Clear(); } else { for (int i = 0; i < _entities[type].Count; i++) { EntityLogicBase entityLogic = _entities[type][i]; entityLogic.OnDestroy(); Main.m_ReferencePool.Despawn(entityLogic); Destroy(entityLogic.Entity); entityLogic.Entity = null; } _entities[type].Clear(); } } else { GlobalTools.LogError(string.Format("销毁实体失败:实体对象 {0} 并未存在!", type.Name)); } } }