コード例 #1
0
        public void ForceRestore(T t)
        {
            var reset = t as IReset;

            reset?.Reset();
            AvaliableObjects.Add(t);
        }
コード例 #2
0
        public void Clear()
        {
            RestoreAll();

            foreach (var instance in AvaliableObjects)
            {
                if (instance != null)
                {
                    destroy?.Invoke(instance);
                }
            }

            AvaliableObjects.Clear();
            DispatchedObjects.Clear();
        }
コード例 #3
0
        public bool Restore(T t)
        {
            //  必须是由该池创建的对象才允许被归还到池内。
            if (t == null || !DispatchedObjects.Contains(t))
            {
                return(false);
            }

            //T 可能为 C# 核心库类型,所以对 T 不做 IReset 限制
            var reset = t as IReset;

            reset?.Reset();

            DispatchedObjects.Remove(t);
            AvaliableObjects.Add(t);
            return(true);
        }
コード例 #4
0
        public T Take()
        {
            T instance;

            if (AvaliableObjects.Count == 0)
            {
                instance = createFunc();
                onCreated?.Invoke(instance);
            }
            else
            {
                instance = AvaliableObjects[AvaliableObjects.Count - 1];
                AvaliableObjects.Remove(instance);
            }

            if (DispatchedObjects == null)
            {
                DispatchedObjects = new List <T>(initCount);
            }

            DispatchedObjects.Add(instance);
            return(instance);
        }