コード例 #1
0
        private PoolObjectComponent ReturnToPool(PoolObjectComponent component)
        {
            var next = firstAvailable;

            firstAvailable = component;
            return(next);
        }
コード例 #2
0
        public static PoolObjectComponent AddComponentTo(GameObject target, PoolObjectComponent next,
                                                         Func <PoolObjectComponent, PoolObjectComponent> returnCallback)
        {
            var component = target.AddComponent <PoolObjectComponent>();

            component.next         = next;
            component.returnToPool = returnCallback;
            return(component);
        }
コード例 #3
0
 public void Clear()
 {
     firstAvailable = null;
     foreach (var poolObject in objects)
     {
         UnityEngine.Object.Destroy(poolObject.gameObject);
     }
     objects.Clear();
 }
コード例 #4
0
 private void OnDisable()
 {
     // when object become a not active in hierarchy
     // make it free for use in the pool
     if (returnToPool == null)
     {
         return;
     }
     next = returnToPool(this);
 }
コード例 #5
0
        private GameObject InstantiateAndAddToPool()
        {
            var go = UnityEngine.Object.Instantiate(prefab);

            if (container != null)
            {
                go.transform.SetParent(container);
            }
            var poolObject = PoolObjectComponent.AddComponentTo(go, firstAvailable, ReturnToPool);

            go.SetActive(false);
            objects.Add(poolObject);
            return(go);
        }
コード例 #6
0
        /// <summary>
        /// Get first available or create new object
        /// </summary>
        /// <returns>GameObject instance</returns>
        public GameObject Create()
        {
            // if no one object in the PoolObjectComponent linked list or reach the end of the list
            // instantiate and add that new instance to linked list
            if (firstAvailable.next == null)
            {
                InstantiateAndAddToPool();
            }

            // return current first available object from list
            // and set next object as first available
            var obj = firstAvailable;

            firstAvailable = obj.next;
            return(obj.gameObject);
        }