//recycle a prafab to pool public void RecyclePrefab(GameObject obj, ObjCachePoolType poolType, string poolKey) { if (obj == null) { return; } if (obj.GetComponent <PoolGameObject>() == null) { Debug.LogError(string.Format("you can not recycle an gameobject without pool flag {0}", obj.name)); Destroy(obj); return; } if (!mDicObjPools.ContainsKey(poolType)) { Debug.LogError(string.Format("ObjCachePoolMgr::RecyclePrefab pool type {0} is not defined!", poolType)); Destroy(obj); return; } ObjectCachePool dstPool = mDicObjPools[poolType]; obj.SetActive(false); obj.transform.SetParent(dstPool.get_root_node(), false); if (!dstPool.add_cache(poolKey, obj)) { CacheReleaseHandlerPrefab(obj); } }
//recycle a c# object to pool public void RecycleCSharpObject(object obj, ObjCachePoolType poolType, string poolKey) { if (obj == null) { Debug.LogError(string.Format("ObjCachePoolMgr::RecycleCSharpObject recycle an null gameobject you may repeat recycle, pool key {0}", poolKey)); return; } if (!mDicObjPools.ContainsKey(poolType)) { Debug.LogError(string.Format("ObjCachePoolMgr::RecycleCSharpObject pool type {0} is not defined!", poolType)); return; } ObjectCachePool dstPool = mDicObjPools[poolType]; dstPool.add_cache(poolKey, obj); obj = null; }
/*private void Update() * { * mPoolRoot.name = string.Format("PoolRoot_{0}/{1}", mDicObjPools [ObjCachePoolType.SMALL_PREFAB].get_free_cache_count() , mDicObjPools [ObjCachePoolType.SMALL_PREFAB].get_cache_count()); * }*/ //load c# object from pool, it not found in pool, return null //notice: if you got object by this method, you should call RecycleCSharpObject() after using it. public T TryLoadCSharpObject <T>(ObjCachePoolType poolType, string poolKey) where T : class { if (poolKey == null) { return(null); } ObjectCachePool dstPool = null; if (!mDicObjPools.TryGetValue(poolType, out dstPool)) { Debug.LogError(string.Format("ObjCachePoolMgr::TryLoadCSharpObject pool type {0} is not defined!", poolType)); return(null); } //try load from pool return(dstPool.try_hit_cache(poolKey) as T); }
//load a prefab from pool, it not found in pool, return null //notice: if you got object by this method, you should call RecyclePrefab() after using it. public object LoadFromCache(ObjCachePoolType poolType, string poolKey) { if (!mDicObjPools.ContainsKey(poolType)) { Debug.LogError(string.Format("ObjCachePoolMgr::LoadPrefab pool type {0} is not defined!", poolType)); return(null); } ObjectCachePool dstPool = mDicObjPools[poolType]; //try load from pool GameObject go = dstPool.try_hit_cache(poolKey) as GameObject; if (go != null) { go.transform.SetParent(null, false); go.transform.position = new Vector3(0, -1000, 0); } return(go); }
//load a prefab from pool, it not found in pool, return null //notice: if you got object by this method, you should call RecyclePrefab() after using it. public IEnumerator LoadPrefab(string assetPath, ObjCachePoolType poolType, string poolKey, ObjectWrapper prefab) { if (poolKey == null) { poolKey = assetPath; } if (!mDicObjPools.ContainsKey(poolType)) { Debug.LogError(string.Format("ObjCachePoolMgr::LoadPrefab pool type {0} is not defined!", poolType)); yield break; } ObjectCachePool dstPool = mDicObjPools[poolType]; //try load from pool GameObject obj = dstPool.try_hit_cache(poolKey) as GameObject; if (obj == null) { PrefabResource pr = new PrefabResource(); yield return(StartCoroutine(ResourceLoader.Instance.load_prefab(string.Format("Assets/Res/{0}.prefab", assetPath), pr))); obj = pr.obj_; if (obj == null) { yield break; } PoolGameObject pg = obj.AddComponent <PoolGameObject>(); pg.poolType = poolType; pg.key = poolKey; } else { obj.transform.SetParent(null, false); obj.SetActive(true); } obj.transform.position = new Vector3(0, -1000, 0); prefab.obj = obj; }
//clear the prefab pool cache private void ClearCachePool(ObjCachePoolType type, bool includeRent) { ObjectCachePool pool = mDicObjPools[type]; pool.clear_all_cache(includeRent); }
public void RemoveFromCachePool(ObjCachePoolType poolType, string poolKey, object obj) { ObjectCachePool dstPool = mDicObjPools[poolType]; dstPool.remove_from_cache(poolKey, obj); }