Пример #1
0
        /// <summary>
        /// Disable provided GameObject and put it into the pool for later use. If the pool size reached capacity limit - GameObject will be simply Destroyed.
        /// </summary>
        /// <param name="sceneObject">GameObject to put into the pool</param>
        public void FastDestroy(GameObject sceneObject)
        {
            if (sceneObject != null)
            {
#if !AUTO_SIZE_POOL
                if (cache.Count < Capacity || Capacity <= 0)
#endif
                {
                    cache.Push(sceneObject);
                    cached_internal++;
                    if (ParentOnCache)
                    {
                        sceneObject.transform.SetParent(parentTransform, false);
                    }

                    switch (NotificationType)
                    {
                    case PoolItemNotificationType.Interface:
                    {
                        IFastPoolItem poolItem = sceneObject.GetComponent <IFastPoolItem>();
                        if (poolItem != null)
                        {
                            poolItem.OnFastDestroy();
                        }
                        Debug.Assert(poolItem.RefCount == 0);
                    }
                    break;

                    case PoolItemNotificationType.SendMessage:
                        sceneObject.SendMessage("OnFastDestroy");
                        break;
                    }

                    sceneObject.SetActive(false);
                }
#if !AUTO_SIZE_POOL
                else
                {
                    GameObject.Destroy(sceneObject);
                }
#endif
            }
            else
            {
                Debug.LogWarning("Attempt to destroy a null object");
            }
        }
Пример #2
0
        /// <summary>
        /// Quickly instantiate GameObject from pool and return provided component from an instantiated GameObject.
        /// </summary>
        /// <typeparam name="T">Component to find on instantiated GameObject</typeparam>
        /// <param name="position">Position for the instantiated GameObject</param>
        /// <param name="rotation">Rotation of instantiated GameObject</param>
        /// <param name="parent">Make instantiated GameObject the child of provided transform. Null if no parent needed</param>
        /// <returns></returns>
        public T FastInstantiate <T>(bool active, Vector3 position, Quaternion rotation, Transform parent = null)
            where T : Component
        {
            GameObject clone = FastInstantiate(active, position, rotation, parent);

            if (clone == null)
            {
                Debug.LogError("FastInstantiate null");
            }

            T component = clone.GetComponent <T>();

            if (component == null)
            {
                component = clone.AddComponent <T>();
            }

            switch (NotificationType)
            {
            case PoolItemNotificationType.Interface:
            {
                IFastPoolItem poolItem = clone.GetComponent <IFastPoolItem>();
                if (poolItem != null)
                {
                    poolItem.OnFastInstantiate(ID);
                }
            }
            break;

            case PoolItemNotificationType.SendMessage:
                clone.SendMessage("OnFastInstantiate");
                break;

            case PoolItemNotificationType.BroadcastMessage:
                clone.BroadcastMessage("OnFastInstantiate");
                break;
            }

            return(component);
        }