public void Destroy() { if (go != null) { //Debug.Log("About to destroy obj from: " + prefab); GameObject.Destroy(go); } pool = null; }
public static void Back <T>(T obj) { AbstractObjectPool pool; if (!pools.TryGetValue(obj.GetType(), out pool)) { Debug.LogError("DataObjectPool internal error: not init before: " + typeof(T)); return; } SimpleObjectPool <T> op = (SimpleObjectPool <T>)pool; op.Put(obj); }
//pools[typeof(T)] = pool; public static T Factory <T> () { AbstractObjectPool pool; if (!pools.TryGetValue(typeof(T), out pool)) { Debug.LogError("DataObjectPool internal error: not init before: " + typeof(T)); return(default(T)); } SimpleObjectPool <T> op = (SimpleObjectPool <T>)pool; return(op.Get()); }
public void PreInitPooledGameObjects(string prefab, GameObject templatePrefabGO = null, int count = 0) { int size = GetInitSize(prefab); if (count > 0) { size = count; } SimpleObjectPool <GameObjectPoolItem> pool = null; if (!poolGroup.TryGetValue(prefab, out pool)) { pool = new SimpleObjectPool <GameObjectPoolItem>(); poolGroup[prefab] = pool; } //pool.Init(size); int totalNeedInitCount = size - pool.Size(); //Debug.Log(prefab +" --> need init game object size: " + totalNeedInitCount); List <GameObject> list = new List <GameObject>(); for (int i = 0; i < totalNeedInitCount; i++) { if (templatePrefabGO == null) { list.Add(GetPooledGameObject(prefab, false)); } else { list.Add(GetPooledGameObject(templatePrefabGO, false)); } } foreach (GameObject go in list) { BackGameObject(go); } list.Clear(); }
public static void CreatePool <T>(Type t, SimpleObjectPool <T> pool, int size = 20) { Debug.Log("Create pool: " + typeof(T)); pools[t] = pool; pool.Init(size); }
private GameObject FactoryPooledGameObject(String prefab, GameObject templatePrefabGO, bool logEnable = true) { SimpleObjectPool <GameObjectPoolItem> pool = null; if (!poolGroup.TryGetValue(prefab, out pool)) { Debug.Log("Please pre init this prefab pool before fighting: " + prefab); pool = new SimpleObjectPool <GameObjectPoolItem>(); poolGroup[prefab] = pool; } GameObjectPoolItem item = pool.Get(logEnable); if (item.go == null) { if (logEnable) { //Debug.Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " GameObject.Instantiate prefab:-->" + templatePrefabGO.name + " --> " + pool.ToString()); } item.prefab = prefab; if (templatePrefabGO == null) { //GameObject prefabGO = WarRes.GetPrefab(prefab.ToLower()); //item.go = GameObject.Instantiate<GameObject>(prefabGO); } else { item.go = GameObject.Instantiate <GameObject>(templatePrefabGO); } if (objectPoolRootNode != null) { item.go.transform.SetParent(objectPoolRootNode.transform, false); } //这句代码会报很多警告,导致游戏帧率变低(开发模式下) //GameObject.DontDestroyOnLoad(item.go); item.pool = pool; item.go.AddComponent <GameObjectPoolItemTag>(); GameObjectPoolItemTag tag = item.go.GetComponent <GameObjectPoolItemTag>(); //item.go.SetActive(false); // default item.entityBehaviors = item.go.GetComponents <PooledEntityMonoBehaviour>(); tag.poolItem = item; } else { item.go.SetActive(true); } foreach (PooledEntityMonoBehaviour component in item.entityBehaviors) { component.OnSpawnFromPool(); } return(item.go); }