PrefabManager.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. /// <summary>prefab缓存池</summary>
  6. public class PrefabManager : MonoBehaviour
  7. {
  8. #region 单利函数
  9. private static PrefabManager _instance = null;
  10. public int RevertCt = 0;
  11. public static PrefabManager Instance
  12. {
  13. get
  14. {
  15. if (_instance == null) {
  16. GameObject go = new GameObject ();
  17. go.name = "PrefabManager";
  18. DontDestroyOnLoad (go);
  19. _instance = go.AddComponent<PrefabManager> ();
  20. }
  21. return _instance;
  22. }
  23. }
  24. void Awake()
  25. {
  26. _instance = this;
  27. }
  28. #endregion
  29. /// <summary>缓存列表</summary>
  30. private Dictionary<string, PrefabInfo> mDictPrefab = new Dictionary<string, PrefabInfo> ();
  31. public int GetPrefabCt(string assetsName, UnityEngine.Object assetsObject)
  32. {
  33. PrefabInfo prefabInfo = GetPrefabInfo(assetsName, assetsObject);
  34. return prefabInfo.Ct;
  35. }
  36. /// <summary>得到一个组件</summary>
  37. public GameObject GetPrefab(string assetsName, UnityEngine.Object assetsObject)
  38. {
  39. PrefabInfo prefabInfo = GetPrefabInfo(assetsName, assetsObject);
  40. return prefabInfo.GetPrefab();
  41. }
  42. /// <summary>删除全部</summary>
  43. public void RemoveAllPrefab()
  44. {
  45. foreach (PrefabInfo info in mDictPrefab.Values)
  46. {
  47. info.Dispose ();
  48. }
  49. mDictPrefab.Clear ();
  50. }
  51. /// <summary>删除一组资源</summary>
  52. public void RemovePrefab(string assetsName)
  53. {
  54. PrefabInfo prefabInfo = GetPrefabInfo(assetsName);
  55. if (prefabInfo != null)
  56. {
  57. mDictPrefab.Remove (prefabInfo.PrefabKey);
  58. prefabInfo.Dispose ();
  59. }
  60. }
  61. /// <summary>恢复一个GameObject的缓存状态</summary>
  62. public void RevertPrefab(GameObject prefabGO, string assetsName, UnityEngine.Object assetsObject = null)
  63. {
  64. //CDebug.Log ("RevertPrefab " + prefabGO.name + " " + assetsName);
  65. PrefabInfo prefabInfo = GetPrefabInfo(assetsName, assetsObject);
  66. if (prefabInfo == null) {
  67. //CDebug.Log ("RevertPrefab " + prefabGO.name + " is NULL");
  68. } else {
  69. //CDebug.Log ("RevertPrefab " + prefabInfo.Ct);
  70. }
  71. prefabInfo.RevertPrefab(prefabGO);
  72. RevertCt++;
  73. }
  74. /// <summary>获得PrefabInfo</summary>
  75. private PrefabInfo GetPrefabInfo(string assetsName, UnityEngine.Object assetsObject = null)
  76. {
  77. string assetsKey = PrefabInfo.AssetsToKey (assetsName);
  78. PrefabInfo prefabInfo = null;
  79. if (mDictPrefab.ContainsKey (assetsKey))
  80. {
  81. prefabInfo = mDictPrefab [assetsKey];
  82. }
  83. else
  84. {
  85. if (assetsObject != null)
  86. {
  87. prefabInfo = new PrefabInfo (assetsName, assetsObject);
  88. mDictPrefab.Add (assetsKey, prefabInfo);
  89. }
  90. }
  91. return prefabInfo;
  92. }
  93. }
  94. /// <summary>内存中的组件对象</summary>
  95. public class PrefabInfo
  96. {
  97. /// <summary>资源源对象</summary>
  98. public UnityEngine.Object mResourceObject = null;
  99. /// <summary>资源包名称</summary>
  100. public string mAssetsName = string.Empty;
  101. /// <summary>资源Key值</summary>
  102. private string mPrefabKey = string.Empty;
  103. /// <summary>缓存的GameObject列表</summary>
  104. private List<GameObject> mCatchObject = new List<GameObject> ();
  105. /// <summary>构造函数</summary>
  106. public PrefabInfo(string assetsName, UnityEngine.Object assetsObject = null)
  107. {
  108. mAssetsName = assetsName;
  109. if (assetsObject == null)
  110. {
  111. CDebug.LogError ("缓存了错误的资源:" + PrefabKey);
  112. }
  113. else
  114. {
  115. mResourceObject = assetsObject;
  116. }
  117. }
  118. /// <summary>释放函数</summary>
  119. public void Dispose()
  120. {
  121. for (int i = 0; i < mCatchObject.Count; i++)
  122. {
  123. GameObject.Destroy (mCatchObject[i]);
  124. }
  125. mCatchObject.Clear ();
  126. mResourceObject = null;
  127. }
  128. /// <summary>得到资源</summary>
  129. public GameObject GetPrefab()
  130. {
  131. int catchIndex = GetIndex(mCatchObject, false);
  132. GameObject returnObj = (catchIndex != -1) ? mCatchObject [catchIndex] : null;
  133. //存在已经创建的对象
  134. if (returnObj != null)
  135. {
  136. mCatchObject [catchIndex] = null;
  137. }
  138. //创建一个新的
  139. else
  140. {
  141. if (mResourceObject != null)
  142. {
  143. returnObj = (GameObject)GameObject.Instantiate (mResourceObject);
  144. returnObj.transform.position = Vector3.zero;
  145. returnObj.transform.localScale = Vector3.one;
  146. returnObj.transform.rotation = Quaternion.Euler (Vector3.zero);
  147. //CDebug.Log ("Create : " + returnObj.name);
  148. }
  149. else
  150. {
  151. CDebug.LogError ("缓存池创建资源错误:" + mAssetsName);
  152. }
  153. if (CStaticMethod.IsEditor)
  154. {
  155. returnObj.name = PrefabKey;
  156. }
  157. returnObj.SetActive (false);
  158. }
  159. return returnObj;
  160. }
  161. /// <summary>恢复一个GameObject的缓存状态</summary>
  162. public void RevertPrefab(GameObject prefabGO)
  163. {
  164. int nullIndex = GetIndex(mCatchObject, true);
  165. if (nullIndex != -1)
  166. {
  167. mCatchObject [nullIndex] = prefabGO;
  168. }
  169. else
  170. {
  171. mCatchObject.Add (prefabGO);
  172. }
  173. prefabGO.transform.position = Vector3.zero;
  174. prefabGO.SetActive (false);
  175. }
  176. /// <summary>得到索引, 非空位置或null位置</summary>
  177. private int GetIndex(List<GameObject> list, bool isGetNull = true)
  178. {
  179. int catchCount = list.Count;
  180. for (int i = 0; i < catchCount; i++)
  181. {
  182. if ((list [i] != null && !isGetNull) || (list [i] == null && isGetNull))
  183. {
  184. return i;
  185. }
  186. }
  187. return -1;
  188. }
  189. /// <summary>资源Key值</summary>
  190. public string PrefabKey
  191. {
  192. get
  193. {
  194. if (mPrefabKey == string.Empty)
  195. {
  196. mPrefabKey = AssetsToKey (mAssetsName);
  197. }
  198. return mPrefabKey;
  199. }
  200. }
  201. /// <summary>转换资源Key值</summary>
  202. public static string AssetsToKey(string assetsName)
  203. {
  204. return assetsName;
  205. /*
  206. StringBuilder stringBuilder = new StringBuilder ();
  207. stringBuilder.Append (assetsName);
  208. stringBuilder.Append ("_");
  209. return stringBuilder.ToString ();*/
  210. }
  211. public int Ct
  212. {
  213. get{ return mCatchObject.Count;}
  214. }
  215. }