Esempio n. 1
0
        private ObjectPoolBase InternalCreateObjectPool(Type objectType, string name, bool allowMultiSpawn, float autoReleaseInterval, int capacity, float expireTime, int priority)
        {
            if (objectType == null)
            {
                throw new LufyException("Object type is invalid.");
            }

            if (!typeof(ObjectBase).IsAssignableFrom(objectType))
            {
                throw new LufyException(Utility.Text.Format("Object type '{0}' is invalid.", objectType.FullName));
            }

            string key = Utility.Text.Format("{0}.{1}", objectType.FullName, name);

            if (HasObjectPool(objectType, name))
            {
                throw new LufyException(Utility.Text.Format("Already exist object pool '{0}'.", key));
            }

            Type           objectPoolType = typeof(ObjectPool <>).MakeGenericType(objectType);
            ObjectPoolBase objectPool     = (ObjectPoolBase)Activator.CreateInstance(objectPoolType, name, allowMultiSpawn, autoReleaseInterval, capacity, expireTime, priority);

            m_ObjectPools.Add(key, objectPool);
            return(objectPool);
        }
Esempio n. 2
0
        /// <summary>
        /// 销毁对象池。
        /// </summary>
        /// <param name="objectPool">要销毁的对象池。</param>
        /// <returns>是否销毁对象池成功。</returns>
        public bool DestroyObjectPool(ObjectPoolBase objectPool)
        {
            if (objectPool == null)
            {
                throw new LufyException("Object pool is invalid.");
            }

            return(InternalDestroyObjectPool(objectPool.ObjectType, objectPool.Name));
        }
Esempio n. 3
0
        /// <summary>
        /// 获取所有对象池。
        /// </summary>
        /// <returns>所有对象池。</returns>
        public ObjectPoolBase[] GetAllObjectPools()
        {
            int index = 0;

            ObjectPoolBase[] results = new ObjectPoolBase[m_ObjectPools.Count];
            foreach (KeyValuePair <string, ObjectPoolBase> objectPool in m_ObjectPools)
            {
                results[index++] = objectPool.Value;
            }

            return(results);
        }
Esempio n. 4
0
        private bool InternalDestroyObjectPool(Type objectType, string name)
        {
            ObjectPoolBase objectPool = null;
            string         key        = Utility.Text.Format("{0}.{1}", objectType.FullName, name);

            if (m_ObjectPools.TryGetValue(key, out objectPool))
            {
                objectPool.Shutdown();
                return(m_ObjectPools.Remove(key));
            }

            return(false);
        }
Esempio n. 5
0
        private ObjectPoolBase InternalGetObjectPool(Type objectType, string name)
        {
            string key = Utility.Text.Format("{0}.{1}", objectType.FullName, name);

            ObjectPoolBase objectPool = null;

            if (m_ObjectPools.TryGetValue(key, out objectPool))
            {
                return(objectPool);
            }

            return(null);
        }