示例#1
0
        public IEnumerator ObjectCountShrinksToTheNumberOfInstances()
        {
            var numberOfInstances = 3;
            var pool = CreatePool(numberOfInstances);

            yield return(pool.PreloadAsync().ToYieldInstruction());

            yield return(pool.PreactivateAsync().ToYieldInstruction());

            // create twice object
            var array = new PoolManagedBehaviour[numberOfInstances * 2];

            for (int i = 0; i < numberOfInstances * 2; i++)
            {
                array[i] = pool.Rent();
            }

            foreach (var t in array)
            {
                pool.Return(t);
            }

            pool.Count.Is(numberOfInstances * 2);
            pool.Shrink();
            pool.Count.Is(numberOfInstances);
        }
示例#2
0
        IObjectPool <PoolManagedBehaviour> CreatePool(int numberOfInstances)
        {
            var go = new GameObject();
            PoolManagedBehaviour prefab = go.AddComponent <TestBehaviour>();
            var config = new ObjectPoolConfig(numberOfInstances, Mathf.Max(numberOfInstances, 1));

            return(base.CreatePool(prefab, config));
        }
示例#3
0
        public IEnumerator ObjectPoolCanShrink()
        {
            var numberOfInstances = 1;
            var pool = CreatePool(numberOfInstances: numberOfInstances);

            yield return(pool.PreloadAsync().ToYieldInstruction());

            yield return(pool.PreactivateAsync().ToYieldInstruction());

            // create twice objects
            var array = new PoolManagedBehaviour[numberOfInstances * 2];

            for (var i = 0; i < array.Length; i++)
            {
                array[i] = pool.Rent();
            }
            foreach (var o in array)
            {
                pool.Return(o);
            }

            pool.Shrink();
        }