Ejemplo n.º 1
0
    internal static ObjectPool _GetPool(PoolableObject prefabPoolComponent)
    {
        GameObject gameObject = prefabPoolComponent.gameObject;
        ObjectPool objectPool;

        if (!_pools.TryGetValue(gameObject, out objectPool))
        {
            objectPool = new ObjectPool(gameObject);
            _pools.Add(gameObject, objectPool);
        }
        return(objectPool);
    }
Ejemplo n.º 2
0
    public void Destroy(GameObject obj)
    {
        PoolableObject pobj = obj.GetComponent <PoolableObject>();

        if (pobj == null)
        {
            Debug.LogWarning("Tried to destroy an unpooled object with a pool method. Object name : " + obj.name);
            return;
        }

        pobj.Destroy();
    }
 public override void Init(PoolableObject paramPoolableObject, int initialPooledAmount)
 {
     this.poolableObject = paramPoolableObject;
     poolList            = new Queue <PoolableObject>();
     for (int i = 0; i < initialPooledAmount; i++)
     {
         tempObject            = Instantiate(poolableObject);
         tempObject.objectPool = this;
         tempObject.gameObject.SetActive(false);
         poolList.Enqueue(tempObject);
     }
 }
Ejemplo n.º 4
0
 private void Start()
 {
     for (int currentTeam = 0; currentTeam < _TotalAITeams; currentTeam++)
     {
         for (int aIUnit = 0; aIUnit < _AICountPerTeam; aIUnit++)
         {
             Spawner        spawner = Spawner.SpawnList.GetRandom();
             PoolableObject clone   = PoolManager.GetNext(_PoolableAI, spawner.transform.position, Quaternion.identity, true);
             clone.GetComponent <Health>().SetTeamNumber(currentTeam);
         }
     }
 }
Ejemplo n.º 5
0
    PoolableObject InstantiatePrefab(PoolableObject prefab)
    {
        if (prefab == null)
        {
            return(null);
        }

        var newGameObject = Instantiate(prefab);

        newGameObject.transform.SetParent(transform, false);
        return(newGameObject);
    }
Ejemplo n.º 6
0
        internal PoolableObject PreloadInstance()
        {
            _ValidatePooledObjectDataContainer();

            PoolableObject poolObj = _NewPooledInstance(null, null);

            poolObj._wasPreloaded = true;

            _SetAvailable(poolObj, true);

            return(poolObj);
        }
Ejemplo n.º 7
0
 void OnParticleSystemStopped()
 {
     pool = GetComponent <PoolableObject>();
     if (pool != null)
     {
         pool.ReturnToPool();
     }
     else
     {
         Destroy(gameObject);
     }
 }
    public void HideObject(GameObject obj)
    {
        obj.transform.parent = this.transform;
        obj.SetActive(false);
        obj.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
        PoolableObject poolableObject = obj.GetComponent <PoolableObject>();

        if (poolableObject != null)
        {
            _poolObjects[poolableObject.id].objectsCreated.Add(obj);
        }
    }
Ejemplo n.º 9
0
 /// <summary>
 /// Returns the gameobject to the specified pool
 /// </summary>
 /// <param name="poolable"></param>
 /// <param name="obj"></param>
 public static void ReturnToPool(PoolableObject poolable, GameObject obj)
 {
     if (instance.objectPool.ContainsKey(poolable))
     {
         instance.objectPool[poolable].Enqueue(obj);
         obj.SetActive(false);
     }
     else
     {
         Debug.LogError($"No entry exists for '{poolable.poolableName}' in the object pool");
     }
 }
Ejemplo n.º 10
0
    private GameObject AddObjectToPool()
    {
        PoolableObject newObject = Instantiate(prefab, transform);

        pooledObjects.Push(newObject.gameObject);

        newObject.gameObject.SetActive(false);

        newObject.Init(this);

        return(newObject.gameObject);
    }
Ejemplo n.º 11
0
    /// <summary>
    /// Spawns an object from the desired object pool and sets the transform
    /// </summary>
    /// <param name="pooledObject"></param>
    /// <param name="position"></param>
    /// <param name="rotation"></param>
    /// <returns></returns>
    public static GameObject SpawnObject(PoolableObject pooledObject, Vector3 position, Quaternion rotation)
    {
        //Use the base method to return an object
        instance.objCache = SpawnObject(pooledObject);

        //Apply the postion and rotation to the transform
        instance.objCache.transform.position = position;
        instance.objCache.transform.rotation = rotation;

        //returned the transformed object
        return(instance.objCache);
    }
Ejemplo n.º 12
0
    public virtual void Return(PoolableObject obj)
    {
        if (poolSceneObj == null)
        {
            InitializePool();
        }

        //Return the object to the available stack
        obj.OnReturn();
        available.Push(obj);
        obj.gameObject.SetActive(false);
    }
Ejemplo n.º 13
0
    /// <summary>
    /// Creates the new pool with prefab.
    /// </summary>
    /// <returns>The new pool with prefab.</returns>
    /// <param name="_poolableObjectPrefab">Poolable object prefab.</param>
    /// <param name="_parent">Parent.</param>
    /// <param name="_poolName">Pool name.</param>
    /// <param name="_poolObjName">Pool object name.</param>
    /// <param name="_maxPoolSize">Max pool size.</param>
    public static GenericPool CreateNewPoolWithPrefab(
        PoolableObject _poolableObjectPrefab,
        Transform _parent,
        string _poolName    = "Pool",
        string _poolObjName = "PooledObj",
        int _maxPoolSize    = NoSizeLimit)
    {
        GenericPool pool = CreateNewPool(_parent, _poolName, _maxPoolSize);

        pool.SetPoolableObject(_poolableObjectPrefab, _poolObjName);
        return(pool);
    }
Ejemplo n.º 14
0
        public void TestValidate()
        {
            Assert.AreEqual(true, m_RefPool.CheckValid());
            m_RefPool.Release(new PoolableObject());
            m_RefPool.Release(new PoolableObject());
            Assert.AreEqual(0, m_RefPool.Statistics.DropCount);
            Assert.AreEqual(true, m_RefPool.CheckValid());
            var objectToReleaseTwice = new PoolableObject();

            m_RefPool.Release(objectToReleaseTwice);
            m_RefPool.Release(objectToReleaseTwice);
            Assert.AreEqual(false, m_RefPool.CheckValid());
        }
Ejemplo n.º 15
0
    /// <summary>
    /// Spawn an instance of the specified poolId with the provided position, rotation and newParent, casted to the typeof(R).
    /// </summary>
    /// <returns>The R instance.</returns>
    /// <param name="poolId">Pool identifier.</param>
    /// <param name="position">Position.</param>
    /// <param name="rotation">Rotation.</param>
    /// <param name="newParent">New parent.</param>
    /// <typeparam name="R">The type of the PoolableObject parameter.</typeparam>
    public override R SpawnLike <R>(string poolId,
                                    Vector3 position    = default(Vector3),
                                    Quaternion rotation = default(Quaternion),
                                    Transform newParent = null)
    {
        PoolableObject obj = Spawn(poolId, position, rotation, newParent);

        if (obj != null)
        {
            return((R)obj);
        }
        return(null);
    }
Ejemplo n.º 16
0
    private List <PoolableObject> NewPoolable(PoolableObject basePoolable, Transform parent)
    {
        var list = new List <PoolableObject>();

        for (int i = 0; i < poolQuantity; i++)
        {
            var obj = (PoolableObject)PrefabUtility.InstantiatePrefab(basePoolable, parent);
            list.Add(obj);
            obj.gameObject.SetActive(false);
        }
        EditorUtility.SetDirty(parent);
        return(list);
    }
Ejemplo n.º 17
0
    /// <summary>
    /// Get or create new PoolableObject from Pool.
    /// </summary>
    /// <returns>The or create.</returns>
    public PoolableObject GetOrCreate()
    {
        // Just to make sure pool is initialized.
        InitializePool();

        PoolableObject obj = GetPooledObject();

        if (obj == null)
        {
            obj = CreatePooledObject();
        }
        return(obj);
    }
Ejemplo n.º 18
0
 private void ReuseObject(PoolableObject pooledObject)
 {
     if (PoolSizeType == PoolSizeType.Fixed && PoolObjects.Count >= MaximumSize)
     {
         MonoBehaviour.Destroy(pooledObject);
     }
     else
     {
         pooledObject.gameObject.SetActive(false);
         pooledObject.transform.parent = PooledObjectsContainer;
         PoolObjects.Enqueue(pooledObject);
     }
 }
Ejemplo n.º 19
0
    public ObjectPool GetPool(PoolableObject poolableObject)
    {
        if (poolableObject == null)
        {
            return(null);
        }

        if (m_ObjectPools.ContainsKey(poolableObject))
        {
            return(m_ObjectPools[poolableObject]);
        }

        return(null);
    }
Ejemplo n.º 20
0
    static internal ObjectPool _GetPool(PoolableObject prefabPoolComponent)
    {
        ObjectPool pool;

        GameObject prefab = prefabPoolComponent.gameObject;

        if (!_pools.TryGetValue(prefab, out pool))
        {
            pool = new ObjectPool(prefab);
            _pools.Add(prefab, pool);
        }

        return(pool);
    }
Ejemplo n.º 21
0
    private PoolableObject GetPooledObject()
    {
        PoolableObject poolableObject = null;

        while (pool.Count > 0 && poolableObject == null)
        {
            poolableObject = pool.Dequeue();
        }
        if (poolableObject != null)
        {
            poolableObject.gameObject.SetActive(true);
        }
        return(poolableObject);
    }
Ejemplo n.º 22
0
        private PoolableObject _NewPooledInstance(Vector3?position, Quaternion?rotation, bool createActive, bool addToPool)
        {
            _isDuringInstantiate = true;

            _prefab._SetActive(false);

            GameObject go = (GameObject)GameObject.Instantiate(
                _prefab,
                position ?? Vector3.zero,
                rotation ?? Quaternion.identity
                );

            _prefab._SetActive(true);

            PoolableObject poolObj = go.GetComponent <PoolableObject>();

            poolObj._pool         = this;
            poolObj._serialNumber = ++_globalSerialNumber;
            poolObj.name         += poolObj._serialNumber;

            if (poolObj.doNotDestroyOnLoad)
            {
                GameObject.DontDestroyOnLoad(poolParent);
            }

            _pool.Add(poolObj);

            if (addToPool)
            {
                poolObj._PutIntoPool();
            }
            else
            {
                poolObj._usageCount++;

                if (createActive)
                {
                    go.SetActive(true);

                    if (poolObj.sendPoolableActivateDeactivateMessages)
                    {
                        CallMethodOnObject(poolObj.gameObject, "OnPoolableObjectActivated", true, true, poolObj.useReflectionInsteadOfMessages);
                    }
                }
            }

            _isDuringInstantiate = false;

            return(poolObj);
        }
Ejemplo n.º 23
0
    // **************************************************************************************************/
    //          public functions
    // **************************************************************************************************/

    /// <summary>
    /// Retrieves an instance of the specified prefab. Either returns a new instance or it claims an instance
    /// from the pool.
    /// </summary>
    /// <param name="prefab">The prefab to be instantiated.</param>
    /// <returns>
    /// An instance of the prefab.
    /// </returns>
    /// <remarks>
    /// Can be used on none-poolable objects as well. It is good practice to use <c>ObjectPoolController.Instantiate</c>
    /// whenever you may possibly make your prefab poolable in the future.
    /// </remarks>
    /// <seealso cref="Destroy(GameObject)"/>
    static public GameObject Instantiate(GameObject prefab, Transform parent = null)
    {
        PoolableObject prefabPool = prefab.GetComponent <PoolableObject>();

        if (prefabPool == null)
        {
            //Debug.LogWarning( "Object " + prefab.name + " not poolable " );
            return(( GameObject )GameObject.Instantiate(prefab));   // prefab not pooled, instantiate normally
        }

        GameObject go = _GetPool(prefabPool).GetPooledInstance(null, null, prefab.activeSelf, parent);

        return(go ?? InstantiateWithoutPool(prefab, parent));
    }
Ejemplo n.º 24
0
 //If a object spawned without the use of the pooler add it to a pool
 public static void AddMissingObject(PoolableObject prefab)
 {
     //Check if it already has pool to enter
     if (_pools.ContainsKey(prefab))
     {
         prefab.Initialize(_pools[prefab]);
         _pools[prefab].Return(prefab);
     }
     else    //If it doesn't have a pool create a new one
     {
         InitializePool(prefab);
         prefab.Initialize(_pools[prefab]);
     }
 }
    protected override IEnumerator Attack()
    {
        WaitForSeconds Wait = new WaitForSeconds(AttackDelay);

        yield return(Wait);

        while (Damageables.Count > 0)
        {
            for (int i = 0; i < Damageables.Count; i++)
            {
                if (HasLineOfSightTo(Damageables[i].GetTransform()))
                {
                    targetDamageable = Damageables[i];
                    OnAttack?.Invoke(Damageables[i]);
                    Agent.enabled = false;
                    break;
                }
            }

            if (targetDamageable != null)
            {
                PoolableObject poolableObject = BulletPool.GetObject();
                if (poolableObject != null)
                {
                    bullet = poolableObject.GetComponent <Bullet>();

                    bullet.Damage             = Damage;
                    bullet.transform.position = transform.position + BulletSpawnOffset;
                    bullet.transform.rotation = Agent.transform.rotation;
                    bullet.Rigidbody.AddForce(Agent.transform.forward * BulletPrefab.MoveSpeed, ForceMode.VelocityChange);
                }
            }
            else
            {
                Agent.enabled = true; // no target in line of sight, keep trying to get closer
            }

            yield return(Wait);

            if (targetDamageable == null || !HasLineOfSightTo(targetDamageable.GetTransform()))
            {
                Agent.enabled = true;
            }

            Damageables.RemoveAll(DisabledDamageables);
        }

        Agent.enabled   = true;
        AttackCoroutine = null;
    }
Ejemplo n.º 26
0
 public T Get()
 {
     if (!_objComponent)
     {
         return(null);
     }
     if (_pooledObj && (_pooledObj._usageCount != _initialUsageCount || _pooledObj._isAvailableForPooling))
     {
         _objComponent = null;
         _pooledObj    = null;
         return(null);
     }
     return(_objComponent);
 }
Ejemplo n.º 27
0
    private void SpawnPowerUp()
    {
        int            index          = Random.Range(0, m_PowerUpPools.Length);
        PoolableObject poolableObject = m_PowerUpPools[index].GetObject();
        Vector3        pos            = GetRandomPos();

        while (DetectPlayer(pos))
        {
            pos = GetRandomPos();
        }
        poolableObject.SpawnObject(pos);

        m_ActivePowerUp = poolableObject.GetComponent <PowerUp>();
    }
Ejemplo n.º 28
0
 private void SpawnEnemys(int ammount)
 {
     for (int i = 0; i < ammount; i++)
     {
         int index = Random.Range(0, m_SpawnPoints.Length);
         if (DetectPlayer(m_SpawnPoints[index]))
         {
             index = Random.Range(0, m_SpawnPoints.Length);
         }
         PoolableObject poolableObject = m_EnemiesPool.GetObject();
         poolableObject.SpawnObject(m_SpawnPoints[index]);
         m_EnemiesAlive.Add(poolableObject.GetComponent <Enemy>());
     }
 }
Ejemplo n.º 29
0
        public void TestUnspawn()
        {
            PoolableObject poolableObject = null;

            Assert.AreEqual(0, m_objectPool.ActiveInstanceCount);
            for (int i = 0; i < 5; i++)
            {
                poolableObject = m_objectPool.Spawn();
                Assert.NotNull(poolableObject);
            }
            Assert.AreEqual(5, m_objectPool.ActiveInstanceCount);
            m_objectPool.Unspawn(poolableObject);
            Assert.AreEqual(4, m_objectPool.ActiveInstanceCount);
        }
 public override GameObject Place(Vector3 position)
 {
     if (poolList.Count > 0)
     {
         tempObject = poolList.Dequeue();
     }
     else
     {
         tempObject = Instantiate(poolableObject);
     }
     tempObject.gameObject.SetActive(true);
     tempObject.transform.position = position;
     return(tempObject.gameObject);
 }