/// <summary> /// 获取所有对象池 /// </summary> /// <param name="sort">是否根据对象池的优先级排序</param> /// <returns>所有对象池</returns> public ObjectPoolBase[] GetAllObjectPools(bool sort) { if (sort) { List <ObjectPoolBase> results = new List <ObjectPoolBase>(); foreach (KeyValuePair <string, ObjectPoolBase> objectPool in m_ObjectPools) { results.Add(objectPool.Value); } results.Sort(ObjectPoolComparer); return(results.ToArray()); } else { int index = 0; ObjectPoolBase[] results = new ObjectPoolBase[m_ObjectPools.Count]; foreach (KeyValuePair <string, ObjectPoolBase> objectPool in m_ObjectPools) { results[index++] = objectPool.Value; } return(results); } }
/// <summary> /// 销毁对象池 /// </summary> /// <param name="objectPool">要销毁的对象池</param> /// <returns>是否销毁对象池成功</returns> public bool DestroyObjectPool(ObjectPoolBase objectPool) { if (objectPool == null) { throw new Exception("Object pool is invalid."); } return(InternalDestroyObjectPool(TextUtil.GetFullName(objectPool.ObjectType, objectPool.Name))); }
private bool InternalDestroyObjectPool(string fullName) { ObjectPoolBase objectPool = null; if (m_ObjectPools.TryGetValue(fullName, out objectPool)) { objectPool.Shutdown(); return(m_ObjectPools.Remove(fullName)); } return(false); }
/// <summary> /// 获取对象池 /// </summary> /// <param name="fullName">对象池完整名称</param> /// <returns>要获取的对象池</returns> public ObjectPoolBase GetObjectPool(string fullName) { if (string.IsNullOrEmpty(fullName)) { throw new Exception("Full name is invalid."); } ObjectPoolBase objectPool = null; if (m_ObjectPools.TryGetValue(fullName, out objectPool)) { return(objectPool); } return(null); }
private void DrawObjectPool(ObjectPoolBase objectPool) { GUILayout.Label(TextUtil.Format("<b>Object Pool: {0}</b>", objectPool.FullName)); GUILayout.BeginVertical("box"); { DrawItem("Name", objectPool.Name); DrawItem("Type", objectPool.ObjectType.FullName); DrawItem("Auto Release Interval", objectPool.AutoReleaseInterval.ToString()); DrawItem("Capacity", objectPool.Capacity.ToString()); DrawItem("Used Count", objectPool.Count.ToString()); DrawItem("Can Release Count", objectPool.CanReleaseCount.ToString()); DrawItem("Expire Time", objectPool.ExpireTime.ToString()); DrawItem("Priority", objectPool.Priority.ToString()); ObjectInfo[] objectInfos = objectPool.GetAllObjectInfos(); GUILayout.BeginHorizontal(); { GUILayout.Label("<b>Name</b>"); GUILayout.Label("<b>Locked</b>", GUILayout.Width(60f)); GUILayout.Label(objectPool.AllowMultiSpawn ? "<b>Count</b>" : "<b>In Use</b>", GUILayout.Width(60f)); GUILayout.Label("<b>Priority</b>", GUILayout.Width(60f)); GUILayout.Label("<b>Last Use Time</b>", GUILayout.Width(120f)); } GUILayout.EndHorizontal(); if (objectInfos.Length > 0) { for (int i = 0; i < objectInfos.Length; i++) { GUILayout.BeginHorizontal(); { GUILayout.Label(objectInfos[i].Name); GUILayout.Label(objectInfos[i].Locked.ToString(), GUILayout.Width(60f)); GUILayout.Label(objectPool.AllowMultiSpawn ? objectInfos[i].SpawnCount.ToString() : objectInfos[i].IsInUse.ToString(), GUILayout.Width(60f)); GUILayout.Label(objectInfos[i].Priority.ToString(), GUILayout.Width(60f)); GUILayout.Label(objectInfos[i].LastUseTime.ToString("yyyy-MM-dd HH:mm:ss"), GUILayout.Width(120f)); } GUILayout.EndHorizontal(); } } else { GUILayout.Label("<i>Object Pool is Empty ...</i>"); } } GUILayout.EndVertical(); }
private ObjectPoolBase InternalCreateObjectPool(Type objectType, string name, bool allowMultiSpawn, float autoReleaseInterval, int capacity, float expireTime, int priority) { if (objectType == null) { throw new Exception("Object type is invalid."); } if (!typeof(ObjectBase).IsAssignableFrom(objectType)) { throw new Exception(TextUtil.Format("Object type '{0}' is invalid.", objectType.FullName)); } if (HasObjectPool(objectType, name)) { throw new Exception(TextUtil.Format("Already exist object pool '{0}'.", TextUtil.GetFullName(objectType, name))); } Type objectPoolType = typeof(ObjectPool <>).MakeGenericType(objectType); ObjectPoolBase objectPool = (ObjectPoolBase)Activator.CreateInstance(objectPoolType, name, allowMultiSpawn, autoReleaseInterval, capacity, expireTime, priority); m_ObjectPools.Add(TextUtil.GetFullName(objectType, name), objectPool); return(objectPool); }
private int ObjectPoolComparer(ObjectPoolBase a, ObjectPoolBase b) { return(a.Priority.CompareTo(b.Priority)); }
/// <summary> /// 销毁对象池 /// </summary> /// <param name="objectPool">要销毁的对象池</param> /// <returns>是否销毁对象池成功</returns> public bool DestroyObjectPool(ObjectPoolBase objectPool) { return(m_PoolManager.DestroyObjectPool(objectPool)); }