/// <summary> /// 释放 /// </summary> public virtual void Release() { m_isReleased = true; id = -1; unitTrans = null; foreach (var item in m_comDic.Values) { item.Release(); UnitComponentBase.RecoverComponent(item); } m_comDic.Clear(); }
//回收一个对象到池中 internal static void RecoverComponent(UnitComponentBase com) { if (com.m_isRecover) { #if DEBUG UnityEngine.Debug.LogError("错误,多次回收同个UnitComponent:" + com); #endif return; } com.m_isRecover = true; com.ResetRole(null); com.m_refCount++; Type type = com.GetType(); s_pool[type].Push(com); }
/// <summary> /// 移除一个指定类型的组件 /// </summary> public void RemoveComponent <T>() where T : UnitComponentBase, new() { Type type = typeof(T); if (!m_comDic.ContainsKey(type)) { #if DEBUG Debug.LogError("移除角色组件错误,角色不含有该组件" + type.Name); #endif return; } UnitComponentBase com = m_comDic[type]; com.Release(); UnitComponentBase.RecoverComponent(com); m_comDic.Remove(type); }
/// <summary> /// 添加一个指定类型的角色组件,不允许添加已存在组件 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T AddComponent <T>() where T : UnitComponentBase, new() { Type type = typeof(T); if (m_comDic.ContainsKey(type)) { #if DEBUG Debug.LogError("添加角色组件错误,角色重复添加组件" + type.Name); #endif return(null); } T com = UnitComponentBase.GetComponent <T>(this); com.Init(); m_comDic.Add(type, com); return(com); }