123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- /// <summary>prefab缓存池</summary>
- public class PrefabManager : MonoBehaviour
- {
- #region 单利函数
- private static PrefabManager _instance = null;
- public int RevertCt = 0;
- public static PrefabManager Instance
- {
- get
- {
- if (_instance == null) {
- GameObject go = new GameObject ();
- go.name = "PrefabManager";
- DontDestroyOnLoad (go);
- _instance = go.AddComponent<PrefabManager> ();
- }
- return _instance;
- }
- }
- void Awake()
- {
- _instance = this;
- }
- #endregion
- /// <summary>缓存列表</summary>
- private Dictionary<string, PrefabInfo> mDictPrefab = new Dictionary<string, PrefabInfo> ();
- public int GetPrefabCt(string assetsName, UnityEngine.Object assetsObject)
- {
- PrefabInfo prefabInfo = GetPrefabInfo(assetsName, assetsObject);
- return prefabInfo.Ct;
- }
- /// <summary>得到一个组件</summary>
- public GameObject GetPrefab(string assetsName, UnityEngine.Object assetsObject)
- {
- PrefabInfo prefabInfo = GetPrefabInfo(assetsName, assetsObject);
- return prefabInfo.GetPrefab();
- }
- /// <summary>删除全部</summary>
- public void RemoveAllPrefab()
- {
- foreach (PrefabInfo info in mDictPrefab.Values)
- {
- info.Dispose ();
- }
- mDictPrefab.Clear ();
- }
- /// <summary>删除一组资源</summary>
- public void RemovePrefab(string assetsName)
- {
- PrefabInfo prefabInfo = GetPrefabInfo(assetsName);
- if (prefabInfo != null)
- {
- mDictPrefab.Remove (prefabInfo.PrefabKey);
- prefabInfo.Dispose ();
- }
- }
- /// <summary>恢复一个GameObject的缓存状态</summary>
- public void RevertPrefab(GameObject prefabGO, string assetsName, UnityEngine.Object assetsObject = null)
- {
- //CDebug.Log ("RevertPrefab " + prefabGO.name + " " + assetsName);
- PrefabInfo prefabInfo = GetPrefabInfo(assetsName, assetsObject);
- if (prefabInfo == null) {
- //CDebug.Log ("RevertPrefab " + prefabGO.name + " is NULL");
- } else {
- //CDebug.Log ("RevertPrefab " + prefabInfo.Ct);
- }
- prefabInfo.RevertPrefab(prefabGO);
- RevertCt++;
- }
- /// <summary>获得PrefabInfo</summary>
- private PrefabInfo GetPrefabInfo(string assetsName, UnityEngine.Object assetsObject = null)
- {
- string assetsKey = PrefabInfo.AssetsToKey (assetsName);
- PrefabInfo prefabInfo = null;
- if (mDictPrefab.ContainsKey (assetsKey))
- {
- prefabInfo = mDictPrefab [assetsKey];
- }
- else
- {
- if (assetsObject != null)
- {
- prefabInfo = new PrefabInfo (assetsName, assetsObject);
- mDictPrefab.Add (assetsKey, prefabInfo);
- }
- }
- return prefabInfo;
- }
- }
- /// <summary>内存中的组件对象</summary>
- public class PrefabInfo
- {
- /// <summary>资源源对象</summary>
- public UnityEngine.Object mResourceObject = null;
- /// <summary>资源包名称</summary>
- public string mAssetsName = string.Empty;
- /// <summary>资源Key值</summary>
- private string mPrefabKey = string.Empty;
- /// <summary>缓存的GameObject列表</summary>
- private List<GameObject> mCatchObject = new List<GameObject> ();
- /// <summary>构造函数</summary>
- public PrefabInfo(string assetsName, UnityEngine.Object assetsObject = null)
- {
- mAssetsName = assetsName;
- if (assetsObject == null)
- {
- CDebug.LogError ("缓存了错误的资源:" + PrefabKey);
- }
- else
- {
- mResourceObject = assetsObject;
- }
- }
- /// <summary>释放函数</summary>
- public void Dispose()
- {
- for (int i = 0; i < mCatchObject.Count; i++)
- {
- GameObject.Destroy (mCatchObject[i]);
- }
- mCatchObject.Clear ();
- mResourceObject = null;
- }
- /// <summary>得到资源</summary>
- public GameObject GetPrefab()
- {
- int catchIndex = GetIndex(mCatchObject, false);
- GameObject returnObj = (catchIndex != -1) ? mCatchObject [catchIndex] : null;
- //存在已经创建的对象
- if (returnObj != null)
- {
- mCatchObject [catchIndex] = null;
- }
- //创建一个新的
- else
- {
- if (mResourceObject != null)
- {
- returnObj = (GameObject)GameObject.Instantiate (mResourceObject);
- returnObj.transform.position = Vector3.zero;
- returnObj.transform.localScale = Vector3.one;
- returnObj.transform.rotation = Quaternion.Euler (Vector3.zero);
- //CDebug.Log ("Create : " + returnObj.name);
- }
- else
- {
- CDebug.LogError ("缓存池创建资源错误:" + mAssetsName);
- }
- if (CStaticMethod.IsEditor)
- {
- returnObj.name = PrefabKey;
- }
- returnObj.SetActive (false);
- }
- return returnObj;
- }
- /// <summary>恢复一个GameObject的缓存状态</summary>
- public void RevertPrefab(GameObject prefabGO)
- {
- int nullIndex = GetIndex(mCatchObject, true);
- if (nullIndex != -1)
- {
- mCatchObject [nullIndex] = prefabGO;
- }
- else
- {
- mCatchObject.Add (prefabGO);
- }
- prefabGO.transform.position = Vector3.zero;
- prefabGO.SetActive (false);
- }
- /// <summary>得到索引, 非空位置或null位置</summary>
- private int GetIndex(List<GameObject> list, bool isGetNull = true)
- {
- int catchCount = list.Count;
- for (int i = 0; i < catchCount; i++)
- {
- if ((list [i] != null && !isGetNull) || (list [i] == null && isGetNull))
- {
- return i;
- }
- }
- return -1;
- }
- /// <summary>资源Key值</summary>
- public string PrefabKey
- {
- get
- {
- if (mPrefabKey == string.Empty)
- {
- mPrefabKey = AssetsToKey (mAssetsName);
- }
- return mPrefabKey;
- }
- }
- /// <summary>转换资源Key值</summary>
- public static string AssetsToKey(string assetsName)
- {
- return assetsName;
- /*
- StringBuilder stringBuilder = new StringBuilder ();
- stringBuilder.Append (assetsName);
- stringBuilder.Append ("_");
- return stringBuilder.ToString ();*/
- }
- public int Ct
- {
- get{ return mCatchObject.Count;}
- }
- }
|