示例#1
0
        /// <summary>
        /// Unspawns a given GameObject and adds it back to the available
        /// resource pool.
        /// </summary>
        public void Unspawn(GameObject targetGO)
        {
            PooledBehaviour targetComponent = targetGO.GetComponent <PooledBehaviour> ();

    #if UNITY_EDITOR
            if (targetComponent == null)
            {
                Debug.LogError("Attempting to Unspawn an object not belonging to this pool!");
                return;
            }
    #endif // UNITY_EDITOR

            // If the object has been manually assigned to this pool,
            // add it to our master list.
            if (!_allObjects.Contains(targetComponent))
            {
                _allObjects.Add(targetComponent);
            }

            // Reset parent in case the object was moved by the user.
            targetComponent.CachedXform.parent = _poolParentTransform;

            if (!_availableObjects.Contains(targetComponent))
            {
                targetComponent.OnUnspawn();
                targetGO.SetActive(false);
                _availableObjects.Push(targetComponent);
            }
        }
示例#2
0
        protected override void Awake()
        {
            base.Awake();

            // Create a parent for grouping all objects together.
            {
                StringBuilder stringBuilder = new StringBuilder("UzuPool - ");
                stringBuilder.Append(_prefab.name);
                GameObject poolParent = new GameObject(stringBuilder.ToString());
                _poolParentTransform = poolParent.transform;

                _poolParentTransform.parent        = CachedXform;
                _poolParentTransform.localPosition = Vector3.zero;
                _poolParentTransform.localScale    = Vector3.one;
                _poolParentTransform.localRotation = Quaternion.identity;
            }

            _prefabTransform  = _prefab.transform;
            _availableObjects = new Stack <PooledBehaviour> (_initialCount);
            _allObjects       = new List <PooledBehaviour> (_initialCount);

            // Pre-allocate our pool.
            {
                // Allocate objects.
                for (int i = 0; i < _initialCount; ++i)
                {
                    CreateObject(Vector3.zero, Quaternion.identity);
                }

                // Add to pool.
                for (int i = 0; i < _allObjects.Count; i++)
                {
                    GameObject targetGO = _allObjects [i].gameObject;
                    targetGO.SetActive(false);

                    PooledBehaviour targetComponent = targetGO.GetComponent <PooledBehaviour> ();
                    _availableObjects.Push(targetComponent);
                }
            }
        }
示例#3
0
        private GameObject CreateObject(Vector3 position, Quaternion rotation)
        {
            GameObject      resultGO        = GameObject.Instantiate(_prefab, position, rotation) as GameObject;
            PooledBehaviour resultComponent = resultGO.GetComponent <PooledBehaviour> ();

            if (resultComponent == null)
            {
                Debug.LogError("Pooled object must contain a Uzu.PooledBehaviour component.");
                GameObject.Destroy(resultGO);
                return(null);
            }

            Transform resultTransform = resultComponent.CachedXform;

            resultTransform.parent        = _poolParentTransform;
            resultTransform.localPosition = position;
            resultTransform.localRotation = rotation;
            resultTransform.localScale    = _prefabTransform.localScale;

            _allObjects.Add(resultComponent);
            resultComponent.AddToPool(this);

            return(resultGO);
        }