mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
MemPool
MemPool
This commit is contained in:
8
Assets/TEngine/Editor/Inspector.meta
Normal file
8
Assets/TEngine/Editor/Inspector.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7585e0dfc1e636344b3ba6d4642fe925
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
58
Assets/TEngine/Editor/Inspector/Base/TEngineInspector.cs
Normal file
58
Assets/TEngine/Editor/Inspector/Base/TEngineInspector.cs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 05b15d046064d604d8cbdb1c30c2617b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
77
Assets/TEngine/Editor/Inspector/MemPoolInspector.cs
Normal file
77
Assets/TEngine/Editor/Inspector/MemPoolInspector.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Editor/Inspector/MemPoolInspector.cs.meta
Normal file
11
Assets/TEngine/Editor/Inspector/MemPoolInspector.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1793e1900fd65841aec71776ecbd91e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
23
Assets/TEngine/Runtime/Core/MemPoolComponent.cs
Normal file
23
Assets/TEngine/Runtime/Core/MemPoolComponent.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Core/MemPoolComponent.cs.meta
Normal file
11
Assets/TEngine/Runtime/Core/MemPoolComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f83b489779008a4aaf472eb9b490ac5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user