示例#1
0
    // Spawn Object and set its pool change its parent to poolParent then disable it
    IObjectPoolItem SpawnObject( )
    {
        IObjectPoolItem item = GameObject.Instantiate(pooledObject, poolParent).GetComponent <IObjectPoolItem> ( );

        item.Pool = this;
        item.gameObject.SetActive(false);
        return(item);
    }
示例#2
0
 public ObjectPool(int pool_size, int data_size)
 {
     packDataPool = new T[pool_size];
     for (int i = 0; i < pool_size; i++)
     {
         T data = new T();
         if (data is IObjectPoolItem)
         {
             IObjectPoolItem item = data as IObjectPoolItem;
             item.init(data_size);
         }
         packDataPool[i] = data;
     }
 }
示例#3
0
        private void PlaceTarget(IObjectPoolItem item, Vector3 placementPoint, Quaternion rotation)
        {
            var targetObj = item.GetGameObject();

            targetObj.transform.position = placementPoint;
            targetObj.transform.rotation = rotation;

            var target = targetObj.GetComponentInChildren <Target>(true);

            if (target != null)
            {
                target.Lock();
            }
        }
示例#4
0
 /// <summary>Return IObjectPoolItem</summary>
 /// <remarks>Need to convert to type which user need
 /// e.g. Bullets inherit from Mono,IObjectPoolItem
 /// if we ask for a bullet we need to convert type to bullet</remarks>
 /// <example><code>Bullet bullet = Bullets.GetPooledObject ( ) as Bullet;</code></example>
 public IObjectPoolItem GetPooledObject( )
 {
     if (poolObjects.Count != 0)
     {
         IObjectPoolItem item = poolObjects.Dequeue( );
         item.Init( );
         item.gameObject.SetActive(true);
         return(item);
     }
     if (IsGrow)
     {
         IObjectPoolItem item = SpawnObject( );
         item.Init( );
         item.gameObject.SetActive(true);
         return(item);
     }
     return(null);
 }
示例#5
0
        public void TakeTest()
        {
            using (ObjectPool <TestClass> pool = new ObjectPool <TestClass>(1, new EmptyPoolObjectProvider <TestClass>())) {
                using (IObjectPoolItem <TestClass> item = pool.TakeItem()) {
                    Assert.IsNotNull(item);
                    Assert.AreEqual(0, pool.CurrentPoolSize);

                    new Thread(() => {
                        TestClass obj1 = null;

                        try {
                            obj1 = pool.TakeObject();
                        } finally {
                            pool.ReturnObject(obj1);
                        }

                        Assert.AreEqual(1, pool.CurrentPoolSize);
                    })
                    {
                        IsBackground = false,
                    }.Start();
                    Thread.Sleep(1000);

                    TestClass obj2 = null;
                    try {
                        obj2 = pool.TakeObject();
                        Assert.AreEqual(0, pool.CurrentPoolSize);
                    } finally {
                        pool.ReturnObject(obj2);
                    }

                    Assert.AreEqual(1, pool.CurrentPoolSize);
                }

                Assert.AreEqual(2, pool.CurrentPoolSize);
            }
        }
示例#6
0
 /// <summary>Recycle Object to Pooling again</summary>
 /// <param name="item">which item will be Recycle to ObjectPooling</param>
 public void RecycleObject(IObjectPoolItem item)
 {
     poolObjects.Enqueue(item);
     item.gameObject.SetActive(false);
 }