MemPool
This commit is contained in:
ALEXTANG
2022-08-10 12:55:46 +08:00
parent 14cf2c2088
commit 40058f1d19
8 changed files with 214 additions and 1 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7585e0dfc1e636344b3ba6d4642fe925
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,58 @@
using UnityEditor;
namespace TEngine.Editor
{
/// <summary>
/// 游戏框架 Inspector 抽象类。
/// </summary>
public abstract class TEngineInspector : UnityEditor.Editor
{
private bool m_IsCompiling = false;
/// <summary>
/// 绘制事件。
/// </summary>
public override void OnInspectorGUI()
{
if (m_IsCompiling && !EditorApplication.isCompiling)
{
m_IsCompiling = false;
OnCompileComplete();
}
else if (!m_IsCompiling && EditorApplication.isCompiling)
{
m_IsCompiling = true;
OnCompileStart();
}
}
/// <summary>
/// 编译开始事件。
/// </summary>
protected virtual void OnCompileStart()
{
}
/// <summary>
/// 编译完成事件。
/// </summary>
protected virtual void OnCompileComplete()
{
}
protected bool IsPrefabInHierarchy(UnityEngine.Object obj)
{
if (obj == null)
{
return false;
}
#if UNITY_2018_3_OR_NEWER
return PrefabUtility.GetPrefabAssetType(obj) != PrefabAssetType.Regular;
#else
return PrefabUtility.GetPrefabType(obj) != PrefabType.Prefab;
#endif
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 05b15d046064d604d8cbdb1c30c2617b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace TEngine.Editor
{
[CustomEditor(typeof(MemPoolComponent))]
internal sealed class MemPoolInspector : TEngineInspector
{
private readonly HashSet<string> m_OpenedItems = new HashSet<string>();
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (!EditorApplication.isPlaying)
{
EditorGUILayout.HelpBox("Available during runtime only.", MessageType.Info);
return;
}
MemPoolComponent t = (MemPoolComponent)target;
if (IsPrefabInHierarchy(t.gameObject))
{
EditorGUILayout.LabelField("Object Pool Count", t.Count.ToString());
var objectPools = t.GetAllObjectPools();
foreach (var objectPool in objectPools)
{
DrawObjectPool(objectPool);
}
}
Repaint();
}
private void OnEnable()
{
}
private void DrawObjectPool(IMemPoolBase objectPool)
{
bool lastState = m_OpenedItems.Contains(objectPool.GetName());
bool currentState = EditorGUILayout.Foldout(lastState, objectPool.GetName());
if (currentState != lastState)
{
if (currentState)
{
m_OpenedItems.Add(objectPool.GetName());
}
else
{
m_OpenedItems.Remove(objectPool.GetName());
}
}
if (currentState)
{
EditorGUILayout.BeginVertical("box");
{
EditorGUILayout.LabelField("Name", objectPool.GetName());
EditorGUILayout.LabelField("Type", objectPool.GetName());
EditorGUILayout.LabelField("Capacity", objectPool.GetPoolItemCount().ToString());
EditorGUILayout.LabelField("Used Count", objectPool.GetPoolItemCount().ToString());
EditorGUILayout.LabelField("Can Release Count", objectPool.GetPoolItemCount().ToString());
}
EditorGUILayout.EndVertical();
EditorGUILayout.Separator();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e1793e1900fd65841aec71776ecbd91e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,23 @@
using System.Collections.Generic;
using UnityEngine;
namespace TEngine
{
public class MemPoolComponent:UnitySingleton<MemPoolComponent>
{
public MemPoolMgr PoolMgr;
protected override void OnLoad()
{
base.OnLoad();
PoolMgr = MemPoolMgr.Instance;
}
public int Count => PoolMgr.Count;
public List<IMemPoolBase> GetAllObjectPools()
{
return PoolMgr.GetAllObjectPools();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5f83b489779008a4aaf472eb9b490ac5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -18,6 +18,16 @@ namespace TEngine
public class MemPoolMgr : TSingleton<MemPoolMgr>
{
protected override void Init()
{
base.Init();
#if UNITY_EDITOR
MemPoolComponent.Active();
#endif
}
public int Count => m_listPool.Count;
List<IMemPoolBase> m_listPool = new List<IMemPoolBase>();
[Conditional("UNITY_EDITOR")]
@@ -46,6 +56,11 @@ namespace TEngine
pool.ClearPool();
}
}
public List<IMemPoolBase> GetAllObjectPools()
{
return m_listPool;
}
}
public class GameMemPool<T> : TSingleton<GameMemPool<T>>, IMemPoolBase where T : IMemPoolObject, new()
@@ -114,5 +129,4 @@ namespace TEngine
return m_objPool.Count;
}
}
}