public IPoolGameObject Spawn(Transform parent = null)
        {
            bool result         = true;
            bool createFromPool = true;

            if (this._despawned.Count == 0)
            {
                result         = this.InstantiatePrefab();
                createFromPool = false;
            }

            if (!result)
            {
                return(null);
            }

            IPoolGameObject po = this._despawned[0];

            po.Active(createFromPool);
            this._despawned.RemoveAt(0);
            this._spawned.Add(po);

            if (parent != null)
            {
                Transform t = po.transform;
                t.parent        = parent;
                t.localPosition = Vector3.zero;
                t.localScale    = Vector3.one;
                t.localRotation = Quaternion.identity;
            }
            return(po);
        }
Beispiel #2
0
        public void ReleaseObject(GameObject go)
        {
            IPoolGameObject poolObject = go.GetComponent <IPoolGameObject>();

            if (poolObject == null)
            {
                throw new UnityException("Object is not a pool Object");
            }
            poolObject.OnDeactivated();
            go.transform.parent = transform;
            go.SetActive(false);
            mObjectsInUse.Remove(go);
            mAvailableObjects.Add(go);
        }
Beispiel #3
0
        protected GameObject CreateNewObject()
        {
            GameObject go = GameObject.Instantiate <GameObject>(_template, transform);

            go.SetActive(false);
            IPoolGameObject poolObject = go.GetComponent <IPoolGameObject>();

            if (poolObject == null)
            {
                GameObject.DestroyImmediate(go);
                go = null;
                throw new UnityException("Object template is not a Pool Object: " + gameObject.name);
            }
            poolObject.OnCreated(this);
            return(go);
        }
        public bool Despawn(IPoolGameObject po)
        {
            if (po == null)
            {
                return(false);
            }

            if (!this._spawned.Contains(po))
            {
                return(false);
            }

            po.Deactive();
            Utils.AddChild(this._pool._root, po.transform, false, false, false);
            this._spawned.Remove(po);
            this._despawned.Add(po);
            return(true);
        }