예제 #1
0
        public T GetObject(InstantiateValue instantiateCallback = null)
        {
            foreach (var property in ObjectList)
            {
                if (property.Property.IsAvailable)
                {
                    return(property.ObjectRef);
                }
            }

            var obj         = Object.Instantiate(_item);
            var objProperty = obj.GetComponent <T>();

            instantiateCallback?.Invoke(obj);

            obj.name = $"{ObjectList.Count} {_item.name}";

            PoolingProperty <T> newProperty = new PoolingProperty <T>
            {
                ObjectRef = objProperty, Property = objProperty as IModuleProperty
            };

            newProperty.Property.Init();

            if (_parent != null)
            {
                newProperty.Property.Parent = _parent;
            }

            ObjectList.Add(newProperty);

            return(newProperty.ObjectRef);
        }
예제 #2
0
        public static ObjectPool <T> InstantiatePool(GameObject item, uint length, Transform parent = null,
                                                     bool inChild = false, InstantiateValue instantiateCallback = null)
        {
            if (inChild)
            {
                Transform listParent = new GameObject($"{item.name}s").transform;
                listParent.SetParent(parent);

                listParent.localPosition = Vector3.zero;
                parent = listParent;
            }

            ObjectPool <T> pool = new ObjectPool <T>
            {
                ObjectList = new List <PoolingProperty <T> >(), _item = item, _parent = parent
            };


            for (int i = 0; i < length; i++)
            {
                GameObject obj         = Object.Instantiate(item);
                T          objProperty = obj.GetComponent <T>();

                instantiateCallback?.Invoke(obj);

                obj.name = $"{pool.ObjectList.Count} {item.name}";

                PoolingProperty <T> property = new PoolingProperty <T>
                {
                    ObjectRef = objProperty, Property = objProperty as IModuleProperty
                };

                property.Property.Init();

                if (parent != null)
                {
                    property.Property.Parent = parent;
                }

                pool.ObjectList.Add(property);
            }

            return(pool);
        }