其他API
我们给Object Pool Manager添加其他的API
是否正在加载
1 2 3 4 5 6 7 8 9
|
public bool IsAsyncLoading(long guid) { return m_AsyncResObjs[guid] != null; }
|
此对象是否由对象池创建
1 2 3 4 5 6 7 8 9 10
|
public bool IsPoolManagerCreat(GameObject obj) { ResourceObj resObj = m_ResourceObjDic[obj.GetInstanceID()]; return resObj == null; }
|
清空全部需要清除的对象池
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
public void ClearCache() { List<uint> tempList = new List<uint>(); foreach (uint resCrc in m_ObjectPoolDic.Keys) { List<ResourceObj> st = m_ObjectPoolDic[resCrc]; for (int i = st.Count - 1; i >= 0; i--) { ResourceObj resObj = st[i]; if(!System.Object.ReferenceEquals(resObj.m_CloneObj,null) && resObj.m_bClear) { GameObject.Destroy(resObj.m_CloneObj); m_ResourceObjDic.Remove(resObj.m_CloneObj.GetInstanceID()); resObj.Reset(); m_ResourceObjPool.Recycle(resObj); st.RemoveAt(i); } } if (st.Count <= 0) { tempList.Add(resCrc); } } for (int i = 0; i < tempList.Count; i++) { uint temp = tempList[i]; if(m_ObjectPoolDic.ContainsKey(temp)) { m_ObjectPoolDic.Remove(temp); } } tempList.Clear(); }
|
清空特定对象的对象池
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
public void ClearPool(uint crc) { List<ResourceObj> st = null; if(!m_ObjectPoolDic.TryGetValue(crc,out st) || st == null) { return; } for (int i = st.Count - 1; i >= 0; i--) { ResourceObj resObj = st[i]; if (resObj.m_bClear) { st.RemoveAt(i); int tempGuid = resObj.m_CloneObj.GetInstanceID(); GameObject.Destroy(resObj.m_CloneObj); resObj.Reset(); m_ResourceObjDic.Remove(tempGuid); m_ResourceObjPool.Recycle(resObj); } } if (st.Count <= 0) { m_ObjectPoolDic.Remove(crc); } }
|
注意这个方法还需要再Resource Manager里面调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| protected void DestroyResourceItem(ResourceItem item, bool destroyCache = false) { AssetBundleManager.Instance.ReleaseAsset (item);
ObjectPoolManager.Instance.ClearPool(item.m_Crc); if(item.m_Obj != null) { item.m_Obj = null; } #if UNITY_EDITOR Resources.UnloadUnusedAssets(); #endif }
|