using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
/// prefab缓存池
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 ();
}
return _instance;
}
}
void Awake()
{
_instance = this;
}
#endregion
/// 缓存列表
private Dictionary mDictPrefab = new Dictionary ();
public int GetPrefabCt(string assetsName, UnityEngine.Object assetsObject)
{
PrefabInfo prefabInfo = GetPrefabInfo(assetsName, assetsObject);
return prefabInfo.Ct;
}
/// 得到一个组件
public GameObject GetPrefab(string assetsName, UnityEngine.Object assetsObject)
{
PrefabInfo prefabInfo = GetPrefabInfo(assetsName, assetsObject);
return prefabInfo.GetPrefab();
}
/// 删除全部
public void RemoveAllPrefab()
{
foreach (PrefabInfo info in mDictPrefab.Values)
{
info.Dispose ();
}
mDictPrefab.Clear ();
}
/// 删除一组资源
public void RemovePrefab(string assetsName)
{
PrefabInfo prefabInfo = GetPrefabInfo(assetsName);
if (prefabInfo != null)
{
mDictPrefab.Remove (prefabInfo.PrefabKey);
prefabInfo.Dispose ();
}
}
/// 恢复一个GameObject的缓存状态
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++;
}
/// 获得PrefabInfo
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;
}
}
/// 内存中的组件对象
public class PrefabInfo
{
/// 资源源对象
public UnityEngine.Object mResourceObject = null;
/// 资源包名称
public string mAssetsName = string.Empty;
/// 资源Key值
private string mPrefabKey = string.Empty;
/// 缓存的GameObject列表
private List mCatchObject = new List ();
/// 构造函数
public PrefabInfo(string assetsName, UnityEngine.Object assetsObject = null)
{
mAssetsName = assetsName;
if (assetsObject == null)
{
CDebug.LogError ("缓存了错误的资源:" + PrefabKey);
}
else
{
mResourceObject = assetsObject;
}
}
/// 释放函数
public void Dispose()
{
for (int i = 0; i < mCatchObject.Count; i++)
{
GameObject.Destroy (mCatchObject[i]);
}
mCatchObject.Clear ();
mResourceObject = null;
}
/// 得到资源
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;
}
/// 恢复一个GameObject的缓存状态
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);
}
/// 得到索引, 非空位置或null位置
private int GetIndex(List 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;
}
/// 资源Key值
public string PrefabKey
{
get
{
if (mPrefabKey == string.Empty)
{
mPrefabKey = AssetsToKey (mAssetsName);
}
return mPrefabKey;
}
}
/// 转换资源Key值
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;}
}
}