示例#1
0
        /// <summary>
        /// 以脚本类型为模板,从对象池中获取闲置对象
        /// </summary>
        public T FromComponentPool <T>(int poolName, ref T template, OnComponentPoolItem <T> onInit = null, bool visible = true) where T : Component
        {
            Tester.Instance.Log(TesterBaseApp.Instance, TesterBaseApp.LOG, template == null, "error: template is null, the type is ".Append(typeof(T).ToString()));

            Component tempResult = null;
            bool      flag       = CheckPoolResult(poolName, ref mCompPool, ref tempResult);

            if (tempResult == null)
            {
                tempResult = Object.Instantiate(template);
            }
            else
            {
                if (flag && (PoolContainer != null))
                {
                    PoolContainer.Get(tempResult.gameObject);
                }
            }

            T result = (T)tempResult;

            result.gameObject.SetActive(visible);
            onInit?.Invoke(result);

            return(result);
        }
示例#2
0
        public void ToPool <T>(int poolName, T target, OnComponentPoolItem <T> onRevert = null, bool visible = false) where T : Component
        {
            if (target == null || mIsDispose)
            {
                return;
            }
            else
            {
            }

            onRevert?.Invoke(target);

            if (mCompPool.IsContainsKey(poolName))
            {
                int elmMax             = mCompPoolElmMax.IsContainsKey(poolName) ? mCompPoolElmMax[poolName] : int.MaxValue;
                Stack <Component> pool = mCompPool[poolName];
                if (pool.Count < elmMax)
                {
                    pool.Push(target);

                    if (PoolContainer != null)
                    {
                        PoolContainer.Collect(target.gameObject, visible);
                    }
                    else
                    {
                        target.gameObject.SetActive(visible);
                    }
                }
                else
                {
                    Object.DestroyImmediate(target.gameObject);
                }
            }
            else
            {
                Stack <Component> pool = new Stack <Component>();
                mCompPool[poolName] = pool;

                ToPool(poolName, target, onRevert);
            }
        }