Beispiel #1
0
    static private void _DetachChildrenAndDestroy(Transform transform, bool destroyImmediate)
    {
        var po = transform.GetComponent <PoolableObject>();

        if (transform.childCount > 0)
        {
            List <PoolableObject> poolableChilds = new List <PoolableObject>();
            transform.GetComponentsInChildren <PoolableObject>(true, poolableChilds);

            if (po != null)
            {
                poolableChilds.Remove(po);
            }

            //first destroy all poolable childs.
            for (int i = 0; i < poolableChilds.Count; i++)
            {
                if (poolableChilds[i] == null || poolableChilds[i]._isInPool)
                {
                    continue;                                                                 //can happen when a poolable is a child of another poolable
                }
                if (destroyImmediate)
                {
                    ObjectPoolController.DestroyImmediate(poolableChilds[i].gameObject);
                }
                else
                {
                    ObjectPoolController.Destroy(poolableChilds[i].gameObject);
                }
            }
        }

        if (po != null)
        {
            //move poolable Object to pool
            po._PutIntoPool();
        }
        else
        {
            //destroy non-poolable object itself
            if (destroyImmediate)
            {
                GameObject.DestroyImmediate(transform.gameObject);
            }
            else
            {
                GameObject.Destroy(transform.gameObject);
            }
        }
    }