示例#1
0
        public PoolManager(Transform container, int size, bool autoReuse, bool autoResize, T[] prefabs, params int[] prefabsShuffleBag)
        {
            _autoReuse  = autoReuse;
            _autoResize = autoResize;
            _pool       = new List <T>();
            _active     = new List <T>();

            _container = container;
            T[] children = _container.GetComponentsInChildren <T>();
            foreach (T child in children)
            {
                child.Init();
                _pool.Add(child);
            }

            if (_pool.Count < size && prefabs != null && prefabs.Length > 0 && prefabs[0] != null)
            {
                ShuffleBag <int> bag = new ShuffleBag <int>();
                for (int i = 0; i < prefabs.Length; i++)
                {
                    bag.Add(i, prefabsShuffleBag.Length > i ? prefabsShuffleBag[i] : 1);
                }

                int attempts = size + 200;
                while (_pool.Count < size && attempts > 0)
                {
                    T prefab = prefabs[bag.Next()];
                    if (prefab != null)
                    {
                        T newPoolObject = GameObject.Instantiate(prefabs[bag.Next()]) as T;
                        if (newPoolObject != null)
                        {
                            newPoolObject.transform.SetParent(container);
                            newPoolObject.Init();
                            _pool.Add(newPoolObject);
                        }
                        else
                        {
                            attempts--;
                        }
                    }
                    else
                    {
                        attempts--;
                    }
                }

                bag.Clear();
                bag = null;
            }

            prefabs = null;
        }