123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- namespace Engine.Component
- {
- /// <summary>游戏组件基类</summary>
- public class BaseComponent : MonoBehaviour
- {
- /// <summary>组件是否被销毁</summary>
- protected bool IsDestroy = false;
- /// <summary>组件是否被释放</summary>
- protected bool IsDispose = false;
- /// <summary>是否为空闲状态</summary>
- public bool IsReset = false;
- /// <summary>初始化函数</summary>
- public virtual void Awake()
- {
- CacheComponents(this);
- }
- /// <summary>重置组件,清空组件对外的所用引用,如果需要组件重用,需要在缓存前调用次函数</summary>
- public virtual void Reset()
- {
- IsReset = true;
- this.gameObject.SetActive(false);
- }
- /// <summary>开始使用</summary>
- public virtual void StartUse()
- {
- IsReset = false;
- this.gameObject.SetActive(true);
- }
- /// <summary>获得gameObject对象</summary>
- public virtual GameObject GetGameObject()
- {
- return this.gameObject;
- }
- /// <summary>释放函数,调用此函数后组件不可再用</summary>
- public virtual void Dispose()
- {
- //修改释放标记
- IsDispose = true;
- //是否需要调用销毁
- if (!IsDestroy)
- {
- Destroy(this);
- }
- }
- /// <summary>组件被销毁时调用,Unity3D底层API自动触发</summary>
- private void OnDestroy()
- {
- DelComponents(this);
- //修改销毁标记
- IsDestroy = true;
- //是否需要调用释放函数
- if (!IsDispose)
- {
- this.Dispose();
- }
- }
- private string mComponentKey = string.Empty;
- /// <summary>组件缓存键值</summary>
- public string ComponentKey
- {
- get
- {
- if (mComponentKey == string.Empty)
- {
- mComponentKey = GetComponentKey (this.GetGameObject());
- }
- return mComponentKey;
- }
- }
- public static Dictionary<string, BaseComponent> DictBaseComponents = new Dictionary<string, BaseComponent>();
- /// <summary>缓存特效</summary>
- protected static void CacheComponents(BaseComponent baseComponents)
- {
- if (!DictBaseComponents.ContainsKey(baseComponents.ComponentKey))
- {
- DictBaseComponents.Add(baseComponents.ComponentKey, baseComponents);
- }
- else
- {
- DictBaseComponents[baseComponents.ComponentKey] = baseComponents;
- }
- }
- /// <summary>查找组件</summary>
- public static BaseComponent FindComponents(GameObject componentObj)
- {
- string componentKey = GetComponentKey (componentObj);
- if (DictBaseComponents.ContainsKey(componentKey))
- {
- return DictBaseComponents[componentKey];
- }
- else
- {
- return null;
- }
- }
- /// <summary>删除缓存</summary>
- protected static void DelComponents(BaseComponent baseComponents)
- {
- if (!DictBaseComponents.ContainsKey(baseComponents.ComponentKey))
- {
- DictBaseComponents.Remove(baseComponents.ComponentKey);
- }
- }
- /// <summary>获取某个GameObject上某个组件的键值</summary>
- protected static string GetComponentKey(GameObject componentObj)
- {
- //StringBuilder strBuillder = new StringBuilder ();
- //strBuillder.Append (componentObj.GetInstanceID());
- //strBuillder.Append (componentType.ToString());
- return componentObj.gameObject.GetInstanceID().ToString();//strBuillder.ToString ();
- }
- }
- }
|