Exemplo n.º 1
0
        public bool BackGameObject(GameObject go, bool forceDestroy = false)
        {
            if (forceDestroy)
            {
                Debug.Log("Pool force destroy: " + go.name);
                GameObject.Destroy(go);
            }
            else
            {
                GameObjectPoolItemTag tag = go.GetComponent <GameObjectPoolItemTag>(); // take much gc?
                if (tag != null && tag.poolItem != null && tag.poolItem.pool != null)
                {
                    //tag.poolItem.go = go;

                    if (objectPoolRootNode != null && tag.gameObject != null)
                    {
                        tag.gameObject.transform.SetParent(objectPoolRootNode.transform, false);
                    }

                    tag.poolItem.pool.Put(tag.poolItem);
                    //Debug.Log("Returned back : " + go.name + ", "+tag.poolItem.pool.ToString());
                    return(true);
                }
                else
                {
                    // destroy it ?
                    //Debug.Log("Destroy gameobject not in pool: " + go.name);
                    GameObject.Destroy(go);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        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);
        }