Exemplo n.º 1
0
    public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation)
    {
        if (!poolDictionary.ContainsKey(tag))
        {
            Debug.LogWarning("Pool with tag " + tag + " doesn't exist!");
            return(null);
        }

        IPoolableObject poolableObject = poolDictionary [tag].Peek().GetComponent <IPoolableObject>();

        if (poolableObject.GetAlive())
        {
            return(null);
        }

        GameObject objectToSpawn = poolDictionary [tag].Dequeue();

        objectToSpawn.SetActive(true);
        objectToSpawn.transform.position = position;
        objectToSpawn.transform.rotation = rotation;

        //IPoolableObject poolableObject = objectToSpawn.GetComponent<IPoolableObject> ();

        if (poolableObject != null)
        {
            poolableObject.OnObjectSpawn();
        }

        poolDictionary [tag].Enqueue(objectToSpawn);

        return(objectToSpawn);
    }
    public void release(GameObject releasedObject, PoolableTypes objectType)
    {
        if (activeGameObjects.Contains(releasedObject))
        {
            List <GameObject> instanciatedGameObjectsList;

            if (disabledGameObjects.TryGetValue(objectType, out instanciatedGameObjectsList))
            {
                instanciatedGameObjectsList.Add(releasedObject);
            }

            IPoolableObject ipo = releasedObject.GetComponent <IPoolableObject>();

            if (ipo == null)
            {
                ipo = releasedObject.GetComponentInChildren <IPoolableObject>();
            }

            ipo.OnRelease();

            activeGameObjects.Remove(releasedObject);
            if (objectType == PoolableTypes.Enemy)
            {
                enemyList.Remove(releasedObject);
            }
            else if (objectType == PoolableTypes.PlayerBot)
            {
                playerBotList.Remove(releasedObject);
            }

            releasedObject.SetActive(false);
        }
    }
Exemplo n.º 3
0
        public virtual bool TryReturnObject(T o)
        {
            bool result;

            lock (this.lockObj)
            {
                if (this.freeStack.Count == 0)
                {
                    this.freeStack.Push(o);
                }
                else
                {
                    if (!o.Preallocated)
                    {
                        return(false);
                    }
                    IPoolableObject poolableObject = this.freeStack.Peek();
                    if (!poolableObject.Preallocated)
                    {
                        T           t          = this.freeStack.Pop();
                        IDisposable disposable = t as IDisposable;
                        if (disposable != null)
                        {
                            disposable.Dispose();
                        }
                    }
                    this.freeStack.Push(o);
                }
                result = true;
            }
            return(result);
        }
    public object PopObject(object inPrefab)
    {
        Debug.Assert((inPrefab is IPoolableObject) == true, "Error: " + inPrefab + " does not implement IPoolableObject");

        object rtnVal = null;

        System.Type            type = inPrefab.GetType();
        List <IPoolableObject> poolableObjects;

        if (pools.TryGetValue(type, out poolableObjects))
        {
            if (poolableObjects.Count == 0)
            {
                IPoolableObject poolableObj = (IPoolableObject)inPrefab;
                rtnVal = poolableObj.CreateInstance();
            }
            else
            {
                IPoolableObject poolableObj = poolableObjects[poolableObjects.Count - 1];
                poolableObjects.Remove(poolableObj);
                poolableObj.Activate();
                rtnVal = poolableObj.GetValue();
            }
        }
        else
        {
            IPoolableObject poolableObj = (IPoolableObject)inPrefab;
            rtnVal = poolableObj.CreateInstance().GetValue();
        }
        return(rtnVal);
    }
Exemplo n.º 5
0
 protected virtual void StartPooling()
 {
     foreach (var item in StringGameObjectDictionary)
     {
         if (!_objectPool.ContainsKey(item.Key))
         {
             List <GameObject> newList = new List <GameObject>();
             int             objectsToCreate;
             GameObject      testInstatiated = Instantiate(StringGameObjectDictionary[item.Key], transform.position, transform.rotation);
             IPoolableObject testPoolable    = testInstatiated.GetComponent <IPoolableObject>();
             if (testPoolable != null)
             {
                 objectsToCreate = testPoolable.instances;
             }
             else
             {
                 objectsToCreate = baseNumberOfItems;
             }
             DestroyImmediate(testInstatiated.gameObject);
             for (int i = 0; i < objectsToCreate; i++)
             {
                 GameObject instatiated = Instantiate(StringGameObjectDictionary[item.Key], transform.position, transform.rotation);
                 instatiated.transform.SetParent(this.transform);
                 IPoolableObject poolable = instatiated.GetComponent <IPoolableObject>();
                 if (poolable != null)
                 {
                     poolable.PoolReset();
                 }
                 instatiated.SetActive(false);
                 newList.Add(instatiated);
             }
             _objectPool[item.Key] = newList;
         }
     }
 }
Exemplo n.º 6
0
    public void Push(IPoolableObject po)
    {
        if (!po.m_isUsing)
        {
            return;
        }

        po.m_isUsing = false;
        m_poolQueue.Enqueue(po);
    }
Exemplo n.º 7
0
    void Start()
    {
        pooledInstance = 0;
        poolDictionary = new Dictionary <string, Queue <GameObject> >();
        poolContainers = new Dictionary <string, Transform>();

        foreach (Pool pool in pools)
        {
            if (pool.prefab != null)
            {
                if (pool.size <= 0)
                {
                    Debug.LogError("Invalid pool capacity for " + pool.prefab.name);
                    continue;
                }

                Queue <GameObject> objectPool = new Queue <GameObject>(pool.size + 1);

                // container
                GameObject container = new GameObject();
                container.name                 = pool.prefab.name + " container";
                container.transform.parent     = transform;
                container.transform.position   = Vector3.zero;
                container.transform.rotation   = Quaternion.identity;
                container.transform.localScale = Vector3.one;

                pool.hasPoolInterface = pool.prefab.GetComponent <IPoolableObject>() != null;
                sortedPools.Add(pool.prefab.name, pool);

                // pool
                for (int i = 0; i < pool.size; i++)
                {
                    GameObject obj = Instantiate(pool.prefab);
                    obj.name                 = pool.prefab.name;
                    obj.transform.parent     = container.transform;
                    obj.transform.position   = Vector3.zero;
                    obj.transform.rotation   = Quaternion.identity;
                    obj.transform.localScale = Vector3.one;
                    obj.SetActive(false);

                    if (pool.hasPoolInterface)
                    {
                        IPoolableObject poolInterface = obj.GetComponent <IPoolableObject>();
                        poolInterface.OnInit();
                    }

                    objectPool.Enqueue(obj);
                }

                pooledInstance += pool.size;
                poolDictionary.Add(pool.prefab.name, objectPool);
                poolContainers.Add(pool.prefab.name, container.transform);
            }
        }
    }
Exemplo n.º 8
0
        public void SetObjectInStorage(IPoolableObject currentObject)
        {
            var go   = currentObject as IPoolableObject;
            var type = go.GetObjectType();

            if (!_objectStorage.ContainsKey(type))
            {
                _objectStorage.Add(type, new List <IPoolableObject>());
            }
            _objectStorage[type].Add(currentObject);
            go.DisablePoolObject(_storageTransform);
        }
Exemplo n.º 9
0
    public IPoolableObject Pop()
    {
        if (m_poolQueue.Count == 0)
        {
            Push(Create(m_original));
        }

        IPoolableObject popObject = m_poolQueue.Dequeue();

        popObject.m_isUsing = true;

        return(popObject);
    }
Exemplo n.º 10
0
    //NOTE:要素数が増えるとパフォーマンスに影響が出るかもしれないので、できる限り初期化ロード時点で済ませておくべきか
    //一回で除外する数を小さくするか
    public void LogoutFromPool(IPoolableObject RemoveObj, string PoolName = "")
    {
        string poolName = string.IsNullOrEmpty(PoolName) ? DefaultPoolName : PoolName;

        if (poolDict.ContainsKey(poolName))
        {
            if (poolDict[poolName].Contains(RemoveObj))
            {
                poolDict[poolName].Remove(RemoveObj);
            }

            Debug.Log($"{poolName}の残り要素数 == " + poolDict[poolName].Count);
        }
    }
Exemplo n.º 11
0
    //NOTE:要素数が増えるとパフォーマンスに影響が出るかもしれないので、できる限り初期化の時点で済ませておくべきか
    //一回でログインする数を小さくするか
    public void LoginToPool(IPoolableObject AddObj, string PoolName = "")
    {
        string poolName = string.IsNullOrEmpty(PoolName) ? DefaultPoolName : PoolName;

        if (poolDict.ContainsKey(poolName))
        {
            if (!poolDict[poolName].Contains(AddObj))
            {
                poolDict[poolName].Add(AddObj);
            }

            Debug.Log($"{poolName}更新後の要素数 == " + poolDict[poolName].Count);
        }
    }
Exemplo n.º 12
0
 public void DisposeObject(T obj)
 {
     if (obj != null)
     {
         IPoolableObject poolableObj = obj as IPoolableObject;
         if (poolableObj != null)
         {
             poolableObj.Reset();
         }
         if (_objects.Count < _capactiy)
         {
             _objects.Enqueue(obj);
         }
     }
 }
Exemplo n.º 13
0
        public void CreateAtPosition(Vector3 position, Quaternion rotation)
        {
            IPoolableObject available = GetNextAvailableObject();

            if (available != null)
            {
                available.ResetPoolObject(position);
                //Debug.Log("Create From Pool");
            }
            else
            {
                pool.Add(GameObject.Instantiate(prefab, position, rotation));
                //Debug.Log("Create New");
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Returns an object from the pool initialized to the given position and rotation
        /// </summary>
        /// <returns>The object.</returns>
        /// <param name="position">Position.</param>
        /// <param name="rotation">Rotation.</param>
        public IPoolableObject GetObject(Vector3 position, Quaternion rotation)
        {
            if (IsEmpty())
            {
                Debug.LogError("Cannot remove object from empty pool");
                return(null);
            }

            IPoolableObject obj = pool[0];

            pool.RemoveAt(0);

            obj.Init(position, rotation);
            return(obj);
        }
Exemplo n.º 15
0
        public IPoolableObject ProvideObject(Type objectType)
        {
            IPoolableObject currentobject = null;

            if (_storage.CheckObjectInStorage(objectType))
            {
                currentobject = _storage.GetObjectInStorage(objectType);
            }
            else
            {
                currentobject = _creator.CreatePoolObject(objectType);
            }

            return(currentobject);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Adds the object.
        /// </summary>
        /// <param name="obj">Object.</param>
        public void AddObject(IPoolableObject obj)
        {
            if (obj == null)
            {
                Debug.LogError("Cannot add null gameobject to pool");
            }

            if (pool == null)
            {
                pool = new List <IPoolableObject>();
            }

            obj.Release();
            pool.Add(obj);
        }
Exemplo n.º 17
0
    public virtual void RecycleObject(GameObject incommingGameObject)
    {
        IPoolableObject baseShoot = incommingGameObject.GetComponent <IPoolableObject>();

        if (baseShoot != null)
        {
            string            objectType = baseShoot.PoolReset();
            List <GameObject> objects    = _objectPool[objectType];

            if (!objects.Contains(incommingGameObject))
            {
                incommingGameObject.SetActive(false);
                objects.Add(incommingGameObject);
            }
        }
    }
Exemplo n.º 18
0
    public void Destroy(GameObject go)
    {
        if (go == null)
        {
            return;
        }

        IPoolableObject poolable = go.GetComponent <IPoolableObject>();

        if (poolable != null)
        {
            PoolManager.Instance.Push(poolable);
            return;
        }

        Object.Destroy(go);
    }
Exemplo n.º 19
0
        public static void Return(IPoolableObject itemToReturn)
        {
            if (poolContainer == null)
            {
                poolContainer = new GameObject("Pool container").transform;
                poolContainer.gameObject.SetActive(false);
            }

            if (!pools.ContainsKey(itemToReturn.Prefab))
            {
                pools.Add(itemToReturn.Prefab, new Queue <IPoolableObject>());
            }

            itemToReturn.gameObject.transform.SetParent(poolContainer);
            itemToReturn.gameObject.SetActive(false);
            pools[itemToReturn.Prefab].Enqueue(itemToReturn);
        }
Exemplo n.º 20
0
    public virtual GameObject getObjectOfType(string objectType, SpawnPosition spawnerTransform)
    {
        if (StringGameObjectDictionary.ContainsKey(objectType))
        {
            List <GameObject> objects = _objectPool[objectType];

            if (objects.Count > 0)
            {
                GameObject g = objects[0];
                g.SetActive(true);
                IPoolableObject baseShoot = g.GetComponent <IPoolableObject>();
                if (baseShoot != null)
                {
                    baseShoot.PoolAdquire(spawnerTransform, objectType, this);
                }
                if (objects.Contains(g))
                {
                    objects.Remove(g);
                }
                return(g);
            }
            else
            {
                GameObject instatiated = Instantiate(StringGameObjectDictionary[objectType], transform.position, transform.rotation);
                Debug.Log(string.Format("--- New Instance Created of Type {0} consider to increase the number of base instances", instatiated.name));
                instatiated.transform.SetParent(this.transform);
                IPoolableObject baseShoot = instatiated.GetComponent <IPoolableObject>();
                if (gameObject.name == "EnemyPool")
                {
                    Debug.Log("EnemyPool");
                }
                if (baseShoot != null)
                {
                    baseShoot.PoolAdquire(spawnerTransform, objectType, this);
                }

                return(instatiated);
            }
        }
        else
        {
            return(null);
        }
    }
    public void PushObject(object objectToPool)
    {
        IPoolableObject poolableObject = (IPoolableObject)objectToPool;

        poolableObject.Deactivate();

        System.Type            type = objectToPool.GetType();
        List <IPoolableObject> poolableObjects;

        if (pools.TryGetValue(type, out poolableObjects))
        {
            poolableObjects.Add(poolableObject);
        }
        else
        {
            poolableObjects = new List <IPoolableObject>();
            poolableObjects.Add(poolableObject);
            pools.Add(type, poolableObjects);
        }
    }
Exemplo n.º 22
0
 public T Spawn()
 {
     if (_currentIndex < Count)
     {
         T obj = _pool.Peek();
         _currentIndex++;
         IPoolableObject po = obj as IPoolableObject;
         po.Respawn();
         return(obj);
     }
     else
     {
         T obj = new T();
         _pool.Push(obj);
         _currentIndex++;
         IPoolableObject po = obj as IPoolableObject;
         po.New();
         return(obj);
     }
 }
Exemplo n.º 23
0
        public static IPoolableObject Get(IPoolableObject prefab)
        {
            IPoolableObject item;

            if (!pools.ContainsKey(prefab))
            {
                pools.Add(prefab, new Queue <IPoolableObject>());
            }

            if (pools[prefab].Count > 0)
            {
                item = pools[prefab].Dequeue();
                item.gameObject.transform.parent = null;
            }
            else
            {
                item        = GameObject.Instantiate(prefab.gameObject).GetComponent <IPoolableObject>();
                item.Prefab = prefab;
            }

            return(item);
        }
Exemplo n.º 24
0
    public GameObject SpawnPoolObject(string tag, Vector3 position, Quaternion rotation)
    {
        if (!poolDictionary.ContainsKey(tag))
        {
            return(null);
        }

        GameObject objectToSpawn = poolDictionary[tag].Dequeue();

        objectToSpawn.SetActive(true);
        objectToSpawn.transform.position = position;
        objectToSpawn.transform.rotation = rotation;

        IPoolableObject pooledObj = objectToSpawn.GetComponent <IPoolableObject>();

        if (pooledObj != null)
        {
            pooledObj.OnObjectSpawn();
        }

        poolDictionary[tag].Enqueue(objectToSpawn);

        return(objectToSpawn);
    }
Exemplo n.º 25
0
	public static IPoolableObject getRandomObjectFromPool(IPoolableObject[] objects)
	{
		int i;
		int sumOfWeights = 0;
		int index = -1;
		
		for (i = 0; i < objects.Length; ++i) {
			sumOfWeights += objects[i].getPoolWeight();
		}
		
		//bug with Random.Range() needs +1 to randomly choose last element in the range
		int randomNumber = Random.Range(0, sumOfWeights + 1);
		
		sumOfWeights = 0;
		for (i = 0; i < objects.Length; ++i) {
			sumOfWeights += objects[i].getPoolWeight();
			if (randomNumber <= sumOfWeights) {
				index = i;
				break;
			}
		}
		
		return objects[index];
	}
Exemplo n.º 26
0
 public void ReturnObject(IPoolableObject currentObject)
 {
     _storage.SetObjectInStorage(currentObject);
 }
Exemplo n.º 27
0
 public void ReturnPlayer(IPoolableObject poolable)
 {
     _playerPool.ReturnObjectToPool(poolable);
 }
Exemplo n.º 28
0
 public void Push(IPoolableObject po)
 {
 }
    public GameObject get(PoolableTypes objectType, Transform locationData = null, Guid parentGuid = new Guid())
    {
        List <GameObject> instanciatedGameObjectsList;

        if (disabledGameObjects.TryGetValue(objectType, out instanciatedGameObjectsList))
        {
            int lastIndex = instanciatedGameObjectsList.Count - 1;

            GameObject go = instanciatedGameObjectsList[lastIndex];
            instanciatedGameObjectsList.RemoveAt(lastIndex);
            activeGameObjects.Add(go);
            IPoolableObject ipo = go.GetComponent <IPoolableObject>();

            if (ipo == null)
            {
                ipo = go.GetComponentInChildren <IPoolableObject>();
            }

            if (locationData != null)
            {
                go.transform.position = locationData.position;
                go.transform.rotation = locationData.rotation;
                go.transform.forward  = locationData.forward;
            }

            ipo.OnPoolCreation();

            if (parentGuid != Guid.Empty)
            {
                Guid objectId = Guid.Empty;
                Guid getId;

                Bot botScript = go.GetComponent <Bot>();
                if (botScript != null)
                {
                    objectId = botScript.Id;
                }
                else
                {
                    SavableObject savableObjectScript = go.GetComponent <SavableObject>();
                    if (savableObjectScript != null)
                    {
                        objectId = savableObjectScript.Id;
                    }
                }

                if (objectId != Guid.Empty &&
                    !GameObjectStateManager.Instance.ParentIds.TryGetValue(objectId, out getId))
                {
                    GameObjectStateManager.Instance.ParentIds.Add(objectId, parentGuid);
                }
            }

            go.SetActive(true);

            if (objectType == PoolableTypes.Enemy)
            {
                enemyList.Add(go);
            }
            else if (objectType == PoolableTypes.PlayerBot)
            {
                playerBotList.Add(go);
            }

            return(go);
        }

        return(null);
    }
Exemplo n.º 30
0
 public void ReturnObjectToPool(IPoolableObject poolableObject)
 {
     _objectPool.Enqueue(poolableObject);
 }
Exemplo n.º 31
0
 public void ReturnParticles(IPoolableObject poolable)
 {
     _particlePool.ReturnObjectToPool(poolable);
 }