コード例 #1
0
ファイル: PoolManager.cs プロジェクト: mounirsemaan1/NCache
        public void CreateSimplePool <T>(ObjectPoolType poolType, PoolingOptions <T> options) where T : ILeasable
        {
            var index = (int)poolType;

            if (index < 0 || index >= _objectPools.Length)
            {
                throw new ArgumentException("Invalid argument for type of pool provided.", nameof(poolType));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            // If the same pool has already been added, ignore it
            if (_objectPools[index] != null)
            {
                return;
            }

            // We ought to avoid locking here
            // since we initialize all the pools synchronously
            //lock (_objectPools)
            //{
            // A form of double check locking
            if (_objectPools[index] == null)
            {
                _objectPools[index] = CreateSimpleObjectPool(options);
            }
            //}
        }
コード例 #2
0
    private PooledObject GetPrefabFromPool(ObjectPoolType type)
    {
        int count = pools.Count;

        for (int i = 0; i < count; i++)
        {
            if (pools[i].type == type)
            {
                //if (variant != PoolVariantType.None)
                //{
                //    int vCount = pools[i].variants.Count;
                //    for (int j = 0; j < count; j++)
                //    {
                //        if(pools[i].variants[j].variant == variant)
                //        {
                //            return pools[i].variants[j].prefab;
                //        }
                //    }
                //}
                //else
                //{
                return(pools[i].prefab);
                //}
            }
        }

        return(null);
    }
コード例 #3
0
ファイル: PoolManager.cs プロジェクト: mounirsemaan1/NCache
        public void AddPool <T>(ObjectPoolType poolType, ObjectPool <T> pool) where T : ILeasable
        {
            var index = (int)poolType;

            if (index < 0 || index >= _objectPools.Length)
            {
                throw new ArgumentException("Invalid argument for type of pool provided.", nameof(poolType));
            }

            if (pool == null)
            {
                throw new ArgumentNullException(nameof(pool));
            }

            // If a pool already exists, ignore it
            if (_objectPools[index] != null)
            {
                return;
            }

            // We ought to avoid locking here
            // since we initialize all the pools synchronously
            //lock (_objectPools)
            //{
            // A form of double check locking
            if (_objectPools[index] == null)
            {
                _objectPools[index] = pool;
            }
            //}
        }
コード例 #4
0
    private void SpawnBase(ObjectPoolType objectPoolType, Transform parent, Vector3 position, Vector3 localScale, float damage = 0)
    {
        GameObject poolObject;

        switch (objectPoolType)
        {
        case ObjectPoolType.DamagePopup:
            poolObject = damagePopupObjectPoolList.ObjectPoolSpawn(parent, active: false);

            var damagePopupController = poolObject.GetComponent <DamagePopupController>();
            damagePopupController.damage       = damage;
            poolObject.transform.localPosition = Vector3.zero;
            if (GameManager.instance.gamePlayMode == GamePlayMode.AR)
            {
                poolObject.transform.localScale = localScale;
            }
            poolObject.SetActive(true);
            break;

        case ObjectPoolType.BloodSprayEffect:
            poolObject = bloodSprayEffectObjectPoolList.ObjectPoolSpawn(parent, active: false);
            poolObject.GetComponentsInChildren <Transform>().DoForAll(transforms => transforms.localScale = localScale);
            poolObject.GetComponent <ParticleSystem>().Play();
            poolObject.SetActive(true);
            break;
        }
    }
コード例 #5
0
    public GameObject GetObject(ObjectPoolType type, Vector3 startPosition)
    {
        if (!objectPoolList.ContainsKey(type.ToString()))
        {
            return(null);
        }

        ObjectPool pool = (ObjectPool)objectPoolList[type.ToString()];

        GameObject obj;

        if (pool.UnusedList.Count > 0)
        {
            obj = pool.UnusedList[0];
            pool.UnusedList.RemoveAt(0);
            obj.SetActive(true);
        }
        else
        {
            obj = Instantiate(pool.Prefab);
            obj.transform.name   = pool.Prefab.name;
            obj.transform.parent = pool.Group.transform;
        }

        obj.transform.position = startPosition;
        obj.transform.rotation = Quaternion.identity;

        return(obj);
    }
コード例 #6
0
    public GameObject GetPooledObject(ObjectPoolType type)
    {
        var itemInPool = _pooledObjects.Where(i => !i.activeInHierarchy && i.tag.ToUpper() == type.ToString().ToUpper()).FirstOrDefault();

        if (itemInPool != null)
        {
            itemInPool.SetActive(true);

            return(itemInPool);
        }

        var objectPoolItem = _itemsToPool.Where(i => i.Type == type).FirstOrDefault();

        if (objectPoolItem != null && objectPoolItem.ShouldExpand)
        {
            var obj = Instantiate(objectPoolItem.ObjectToPool);

            obj.name = objectPoolItem.Type.ToString() + "_" + _pooledObjects.Where(i => i.tag.ToUpper() == type.ToString().ToUpper()).Count();
            obj.SetActive(true);

            _pooledObjects.Add(obj);

            return(obj);
        }

        return(null);
    }
コード例 #7
0
    public void AddToPool(GameObject instance, ObjectPoolType type)
    {
        instance.SetActive(false);

        ObjectPoolInfo poolInfo = allPools.Find(pool => pool.type.Equals(type));

        poolInfo.pool.Enqueue(instance);
    }
コード例 #8
0
    private Queue <PooledObject> GetQueue(ObjectPoolType type)
    {
        if (poolDictionary.ContainsKey(type) == false)
        {
            return(null);
        }

        return(poolDictionary[type]);
    }
コード例 #9
0
        /// <summary>
        /// Returns correct type of object from the correct pool via wanted type.
        /// </summary>
        /// <param name="objectPoolType">The type of pool that holds the correct objects we want to receive.</param>
        /// <returns>An object of our wanted pool type.</returns>
        public IPoolObject GetObjectFromPool(ObjectPoolType objectPoolType)
        {
            IPoolObject wantedObject = null;

            if (poolByType.ContainsKey(objectPoolType))
            {
                wantedObject = poolByType[objectPoolType].GetObject();
            }

            return(wantedObject);
        }
コード例 #10
0
 public PoolRequestInfo(ObjectPoolType poolType, Transform parent = null, Vector3?position = null,
                        Quaternion?rotation = null, bool worldPositionStays = false, bool localPosition = false)
 {
     this.poolType           = poolType;
     this.parent             = parent;
     this.position           = position;
     this.rotation           = rotation;
     this.worldPositionStays = worldPositionStays;
     this.localPosition      = localPosition;
     //this.variant = variant;
 }
コード例 #11
0
        private void InitializeSpawning(ObjectPoolType objectPool, float offset, float spawnRadius)
        {
            objectPoolType = objectPool;
            RaycastHit hit;

            if (Physics.Raycast(spawnPos, -Vector3.up, out hit, maxLinecastDistance, layerMask))
            {
                ySpawnOffset = hit.distance - offset;
            }
            else
            {
                ySpawnOffset = maxLinecastDistance;
            }

            spawnSpaceRadius = spawnRadius;
        }
コード例 #12
0
    public GameObject GetFromPool(ObjectPoolType type)
    {
        ObjectPoolInfo     poolInfo = allPools.Find(p => p.type.Equals(type));
        Queue <GameObject> pool     = poolInfo.pool;

        if (poolInfo.canGrow && pool.Count == 0)
        {
            GrowPool(poolInfo);
        }

        GameObject instance = pool.Dequeue();

        instance.SetActive(true);

        return(instance);
    }
コード例 #13
0
ファイル: PoolManager.cs プロジェクト: mounirsemaan1/NCache
        public ProtoPoolBase <T> GetPool <T>(ObjectPoolType poolType) where T : ILeasable
        {
            var index = (int)poolType;

            if (index < 0 || index >= _objectPools.Length)
            {
                throw new ArgumentException("Invalid type of pool requested.", nameof(poolType));
            }

            // Lock not taken on purpose since our pools are always initialized before use
            // and we don't want our operations to halt because of locking
            var pool = _objectPools[index] as ProtoPoolBase <T>;

            if (pool == null)
            {
                throw new InvalidOperationException("Requested pool has not been added to this manager.");
            }

            return(pool);
        }
コード例 #14
0
    private void CreateAndAddObject(ObjectPoolType poolType)
    {
        Queue <PooledObject> targetPool = GetQueue(poolType);

        if (targetPool == null)
        {
            Debug.LogError("Pool null");
            return;
        }

        PooledObject targetObject = GetPrefabFromPool(poolType);

        PooledObject newObject = Instantiate(targetObject) as PooledObject;

        newObject.poolType        = poolType;
        newObject.gameObject.name = poolType.ToString() + GameManager.Instance.createdPooledObjects;
        newObject.gameObject.SetActive(false);
        newObject.transform.SetParent(transform, false);
        targetPool.Enqueue(newObject);
        GameManager.AddCreations();
    }
コード例 #15
0
 public void Spawn(ObjectPoolType objectPoolType, Vector3 position, Vector3 localScale, float damage = 0)
 {
     SpawnBase(objectPoolType, null, position, localScale, damage);
 }
コード例 #16
0
 public void Spawn(ObjectPoolType objectPoolType, Transform parent, Vector3 localScale, float damage = 0)
 {
     SpawnBase(objectPoolType, parent, Vector3.zero, localScale, damage);
 }
コード例 #17
0
 public static PoolRequestInfo CreatePoolInfo(ObjectPoolType poolType, Transform parent = null, Vector3?position = null,
                                              Quaternion?rotation = null, bool worldPositionStays = false, bool localPosition = false)
 {
     return(new PoolRequestInfo(poolType, parent, position, rotation, worldPositionStays, localPosition));
 }