Skip to content

Commit 3f858aa

Browse files
committed
refactor(UI): 移除界面回收至对象池功能并优化回收逻辑
- 移除 IsRecycleToPool 相关代码,简化界面回收机制 - 使用队列优化界面回收处理流程 - 改进错误处理,避免抛出异常 - 清理不再需要的代码和注释
1 parent bdfb950 commit 3f858aa

5 files changed

Lines changed: 42 additions & 86 deletions

File tree

Editor/UIComponentInspector.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ internal sealed class UIComponentInspector : ComponentTypeComponentInspector
5252

5353
private SerializedProperty m_InstanceExpireTime = null;
5454
private SerializedProperty m_RecycleInterval = null;
55-
private SerializedProperty m_IsRecycleToPool = null;
5655

5756
// private SerializedProperty m_InstancePriority = null;
5857
private SerializedProperty m_InstanceUGUIRoot = null;
@@ -83,8 +82,6 @@ public override void OnInspectorGUI()
8382
// EditorGUILayout.PropertyField(m_EnableOpenUIFormDependencyAssetEvent);
8483
EditorGUILayout.PropertyField(m_EnableCloseUIFormCompleteEvent);
8584
EditorGUILayout.IntSlider(m_RecycleInterval, 10, 600, "Recycle Interval");
86-
EditorGUILayout.HelpBox("是否回收到对象池,建议设置为 true", MessageType.Info);
87-
EditorGUILayout.PropertyField(m_IsRecycleToPool);
8885
EditorGUILayout.PropertyField(m_IsEnableUIShowAnimation);
8986
EditorGUILayout.PropertyField(m_IsEnableUIHideAnimation);
9087
}
@@ -188,7 +185,6 @@ protected override void Enable()
188185
m_InstanceCapacity = serializedObject.FindProperty("m_InstanceCapacity");
189186
m_InstanceExpireTime = serializedObject.FindProperty("m_InstanceExpireTime");
190187
m_RecycleInterval = serializedObject.FindProperty("m_RecycleInterval");
191-
m_IsRecycleToPool = serializedObject.FindProperty("m_IsRecycleToPool");
192188
// m_InstancePriority = serializedObject.FindProperty("m_InstancePriority");
193189
m_InstanceUGUIRoot = serializedObject.FindProperty("m_InstanceUGUIRoot");
194190
m_InstanceFairyGUIRoot = serializedObject.FindProperty("m_InstanceFairyGUIRoot");

Runtime/BaseUIManager.Close.cs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
// ==========================================================================================
3131

3232
using System;
33-
using System.Collections.Generic;
3433
using GameFrameX.Runtime;
3534

3635
namespace GameFrameX.UI.Runtime
@@ -58,12 +57,6 @@ public event EventHandler<CloseUIFormCompleteEventArgs> CloseUIFormComplete
5857
/// <param name="isDispose">是否销毁释放</param>
5958
protected abstract void RecycleUIForm(IUIForm uiForm, bool isDispose = false);
6059

61-
/// <summary>
62-
/// 回收界面实例对象到实例池。
63-
/// </summary>
64-
/// <param name="uiForm">要回收的界面实例对象。</param>
65-
protected abstract void RecycleToPoolUIForm(IUIForm uiForm);
66-
6760
/// <summary>
6861
/// 关闭界面。
6962
/// </summary>
@@ -85,7 +78,8 @@ public void CloseUIForm(int serialId, object userData, bool isNowRecycle = false
8578
var uiForm = GetUIForm(serialId);
8679
if (uiForm == null)
8780
{
88-
throw new GameFrameworkException(Utility.Text.Format("Can not find UI form '{0}'.", serialId));
81+
Log.Error(Utility.Text.Format("Can not find UI form '{0}'.", serialId));
82+
return;
8983
}
9084

9185
CloseUIForm(uiForm, userData, isNowRecycle);
@@ -137,6 +131,13 @@ public void CloseUIForm<T>(object userData, bool isNowRecycle = false) where T :
137131
public void CloseUIForm(IUIForm uiForm, object userData, bool isNowRecycle = false)
138132
{
139133
GameFrameworkGuard.NotNull(uiForm, nameof(uiForm));
134+
var serialId = uiForm.SerialId;
135+
if (IsLoadingUIForm(serialId))
136+
{
137+
m_UIFormsToReleaseOnLoad.Add(serialId);
138+
m_UIFormsBeingLoaded.Remove(serialId);
139+
return;
140+
}
140141

141142
if (uiForm.IsDisableClosing)
142143
{
@@ -145,44 +146,28 @@ public void CloseUIForm(IUIForm uiForm, object userData, bool isNowRecycle = fal
145146

146147
GameFrameworkGuard.NotNull(uiForm.UIGroup, nameof(uiForm.UIGroup));
147148
var uiGroup = (UIGroup)uiForm.UIGroup;
148-
var serialId = uiForm.SerialId;
149+
149150
if (uiForm.EnableHideAnimation)
150151
{
151152
uiGroup.RemoveUIForm(uiForm, true);
152153
uiForm.Hide(m_UIFormHideHandler, () =>
153154
{
154155
uiForm.OnClose(m_IsShutdown, userData);
155156
uiGroup.Refresh();
157+
if (isNowRecycle)
158+
{
159+
RecycleUIForm(uiForm, true);
160+
}
156161
});
157162
}
158163
else
159164
{
160165
uiGroup.RemoveUIForm(uiForm);
161166
uiForm.OnClose(m_IsShutdown, userData);
162167
uiGroup.Refresh();
163-
}
164-
165-
if (IsLoadingUIForm(serialId))
166-
{
167-
m_UIFormsToReleaseOnLoad[serialId] = uiForm;
168-
m_UIFormsBeingLoaded.Remove(serialId);
169-
}
170-
171-
// 回收界面实例对象
172-
if (!uiForm.IsDisableRecycling)
173-
{
174168
if (isNowRecycle)
175169
{
176-
// 立即回收界面实例对象到实例池。
177-
RecycleToPoolUIForm(uiForm);
178170
RecycleUIForm(uiForm, true);
179-
m_UIFormsToReleaseOnLoad.Remove(serialId);
180-
}
181-
else
182-
{
183-
// 界面实例对象未立即回收,需要在实例池回收时间点回收。
184-
m_UIFormsToReleaseOnLoad[serialId] = uiForm;
185-
RecycleToPoolUIForm(uiForm);
186171
}
187172
}
188173

@@ -191,6 +176,20 @@ public void CloseUIForm(IUIForm uiForm, object userData, bool isNowRecycle = fal
191176
var closeUIFormCompleteEventArgs = CloseUIFormCompleteEventArgs.Create(uiForm.SerialId, uiForm.UIFormAssetName, uiGroup, userData);
192177
m_CloseUIFormCompleteEventHandler(this, closeUIFormCompleteEventArgs);
193178
}
179+
180+
// 判断是否禁用了界面的回收
181+
if (uiForm.IsDisableRecycling)
182+
{
183+
return;
184+
}
185+
186+
// 判断是否立即回收界面
187+
if (isNowRecycle)
188+
{
189+
return;
190+
}
191+
192+
m_RecycleQueue.Enqueue(uiForm);
194193
}
195194

196195
/// <summary>
@@ -228,7 +227,7 @@ public void CloseAllLoadingUIForms()
228227
{
229228
foreach (var uiFormBeingLoaded in m_UIFormsBeingLoaded)
230229
{
231-
m_UIFormsToReleaseOnLoad[uiFormBeingLoaded.Key] = GetUIForm(uiFormBeingLoaded.Key);
230+
m_UIFormsToReleaseOnLoad.Add(uiFormBeingLoaded.Key);
232231
}
233232

234233
m_UIFormsBeingLoaded.Clear();
@@ -241,9 +240,9 @@ public void CloseAllLoadingUIForms()
241240
/// <param name="isNowRecycle">是否立即回收界面,默认是否</param>
242241
public void ReleaseAllLoadedUIForms(bool isNowRecycle = false, object userData = null)
243242
{
244-
foreach (var keyValuePair in m_UIFormsToReleaseOnLoad)
243+
foreach (var id in m_UIFormsToReleaseOnLoad)
245244
{
246-
var uiForm = keyValuePair.Value;
245+
var uiForm = GetUIForm(id);
247246
if (uiForm != null)
248247
{
249248
RecycleUIForm(uiForm, isNowRecycle);

Runtime/BaseUIManager.cs

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
// Official Documentation: https://gameframex.doc.alianblank.com/
3030
// ==========================================================================================
3131

32-
using System;
3332
using System.Collections.Generic;
3433
using GameFrameX.Asset.Runtime;
3534
using GameFrameX.ObjectPool;
@@ -48,7 +47,12 @@ public abstract partial class BaseUIManager : GameFrameworkModule, IUIManager
4847
/// <summary>
4948
/// 需要释放的界面实例对象池。
5049
/// </summary>
51-
protected readonly Dictionary<int, IUIForm> m_UIFormsToReleaseOnLoad = new Dictionary<int, IUIForm>();
50+
protected readonly HashSet<int> m_UIFormsToReleaseOnLoad = new HashSet<int>();
51+
52+
/// <summary>
53+
/// 待释放的界面实例队列。
54+
/// </summary>
55+
private Queue<IUIForm> m_RecycleQueue = new Queue<IUIForm>();
5256

5357
/// <summary>
5458
/// 界面实例对象池回收间隔秒数。
@@ -124,16 +128,6 @@ public float InstanceExpireTime
124128
set { m_InstancePool.ExpireTime = value; }
125129
}
126130

127-
private bool m_IsRecycleToPool = false;
128-
129-
/// <summary>
130-
/// 获取或设置界面实例对象池是否回收到对象池。
131-
/// </summary>
132-
public bool IsRecycleToPool
133-
{
134-
get { return m_IsRecycleToPool; }
135-
set { m_IsRecycleToPool = value; }
136-
}
137131

138132
/// <summary>
139133
/// 获取或设置是否启用界面显示动画。
@@ -161,35 +155,13 @@ public bool IsEnableUIShowAnimation
161155
/// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
162156
protected override void Update(float elapseSeconds, float realElapseSeconds)
163157
{
164-
m_RecycleTime += elapseSeconds;
165-
if (m_RecycleTime >= m_RecycleInterval)
158+
while (m_RecycleQueue.Count > 0)
166159
{
167-
m_RecycleTime = 0;
168-
if (m_UIFormsToReleaseOnLoad.Count > 0)
169-
{
170-
foreach (var keyValuePair in m_UIFormsToReleaseOnLoad)
171-
{
172-
var uiForm = keyValuePair.Value;
173-
if (uiForm == null || uiForm.IsCanRecycle == false)
174-
{
175-
continue;
176-
}
177-
178-
try
179-
{
180-
RecycleUIForm(uiForm, !m_IsRecycleToPool);
181-
}
182-
catch (Exception e)
183-
{
184-
Log.Error(e);
185-
}
186-
}
187-
188-
m_UIFormsToReleaseOnLoad.Clear();
189-
}
160+
var uiForm = m_RecycleQueue.Dequeue();
161+
RecycleUIForm(uiForm);
190162
}
191163

192-
foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
164+
foreach (var uiGroup in m_UIGroups)
193165
{
194166
uiGroup.Value.Update(elapseSeconds, realElapseSeconds);
195167
}
@@ -205,6 +177,7 @@ protected override void Shutdown()
205177
m_UIGroups.Clear();
206178
m_UIFormsBeingLoaded.Clear();
207179
m_UIFormsToReleaseOnLoad.Clear();
180+
m_RecycleQueue.Clear();
208181
}
209182

210183
/// <summary>
@@ -230,7 +203,6 @@ public virtual void SetResourceManager(IAssetManager assetManager)
230203
m_AssetManager = assetManager;
231204
}
232205

233-
234206
/// <summary>
235207
/// 设置界面辅助器。
236208
/// </summary>

Runtime/UI/IUIManager.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ public interface IUIManager
5757
/// </summary>
5858
int InstanceCapacity { get; set; }
5959

60-
/// <summary>
61-
/// 获取或设置界面实例对象池是否回收到对象池。
62-
/// </summary>
63-
bool IsRecycleToPool { get; set; }
64-
6560
/// <summary>
6661
/// 获取或设置是否启用界面显示动画。
6762
/// </summary>

Runtime/UIComponent.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ public partial class UIComponent : GameFrameworkComponent
7474
[Tooltip("UI 回收间隔时间/秒")] [SerializeField]
7575
private int m_RecycleInterval = 60;
7676

77-
/// <summary>
78-
/// 获取或设置界面实例对象池是否回收到对象池。
79-
/// </summary>
80-
[Tooltip("UI 是否回收到对象池")] [SerializeField]
81-
private bool m_IsRecycleToPool = true;
8277
// [SerializeField] private int m_InstancePriority = 0;
8378

8479
[SerializeField] private Transform m_InstanceUGUIRoot = null;
@@ -260,7 +255,6 @@ protected override void Awake()
260255
}
261256

262257
m_UIManager.OpenUIFormFailure += OnOpenUIFormFailure;
263-
m_UIManager.IsRecycleToPool = m_IsRecycleToPool;
264258
/*
265259
if (m_EnableOpenUIFormUpdateEvent)
266260
{

0 commit comments

Comments
 (0)