Skip to content

Commit f87b455

Browse files
committed
feat(UI): 添加界面单实例打开模式
新增 OptionUIAllowMultiInstanceAttribute 属性,允许标记界面支持多实例。 通过 EnableUIFormSingleton 控制默认行为,未标记属性的界面默认单实例打开。
1 parent cd251b6 commit f87b455

5 files changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// ==========================================================================================
2+
// GameFrameX 组织及其衍生项目的版权、商标、专利及其他相关权利
3+
// GameFrameX organization and its derivative projects' copyrights, trademarks, patents, and related rights
4+
// 均受中华人民共和国及相关国际法律法规保护。
5+
// are protected by the laws of the People's Republic of China and relevant international regulations.
6+
// 使用本项目须严格遵守相应法律法规及开源许可证之规定。
7+
// Usage of this project must strictly comply with applicable laws, regulations, and open-source licenses.
8+
// 本项目采用 MIT 许可证与 Apache License 2.0 双许可证分发,
9+
// This project is dual-licensed under the MIT License and Apache License 2.0,
10+
// 完整许可证文本请参见源代码根目录下的 LICENSE 文件。
11+
// please refer to the LICENSE file in the root directory of the source code for the full license text.
12+
// 禁止利用本项目实施任何危害国家安全、破坏社会秩序、
13+
// It is prohibited to use this project to engage in any activities that endanger national security, disrupt social order,
14+
// 侵犯他人合法权益等法律法规所禁止的行为!
15+
// or infringe upon the legitimate rights and interests of others, as prohibited by laws and regulations!
16+
// 因基于本项目二次开发所产生的一切法律纠纷与责任,
17+
// Any legal disputes and liabilities arising from secondary development based on this project
18+
// 本项目组织与贡献者概不承担。
19+
// shall be borne solely by the developer; the project organization and contributors assume no responsibility.
20+
// GitHub 仓库:https://github.com/GameFrameX
21+
// GitHub Repository: https://github.com/GameFrameX
22+
// Gitee 仓库:https://gitee.com/GameFrameX
23+
// Gitee Repository: https://gitee.com/GameFrameX
24+
// CNB 仓库:https://cnb.cool/GameFrameX
25+
// CNB Repository: https://cnb.cool/GameFrameX
26+
// 官方文档:https://gameframex.doc.alianblank.com/
27+
// Official Documentation: https://gameframex.doc.alianblank.com/
28+
// ==========================================================================================
29+
30+
using System;
31+
32+
namespace GameFrameX.UI.Runtime
33+
{
34+
/// <summary>
35+
/// 控制指定界面是否允许多实例打开。
36+
/// </summary>
37+
/// <remarks>
38+
/// Controls whether the specified UI form allows multi-instance opening.
39+
/// </remarks>
40+
[AttributeUsage(AttributeTargets.Class)]
41+
[UnityEngine.Scripting.Preserve]
42+
public sealed class OptionUIAllowMultiInstanceAttribute : Attribute
43+
{
44+
/// <summary>
45+
/// 是否允许多实例打开,默认为 false。
46+
/// </summary>
47+
/// <remarks>
48+
/// Whether to allow multi-instance opening. Default is false.
49+
/// </remarks>
50+
[UnityEngine.Scripting.Preserve]
51+
public bool Enable { get; private set; }
52+
53+
/// <summary>
54+
/// 初始化特性实例。
55+
/// </summary>
56+
/// <remarks>
57+
/// Initializes the attribute instance.
58+
/// </remarks>
59+
/// <param name="enable">是否允许多实例打开。/ Whether to allow multi-instance opening.</param>
60+
[UnityEngine.Scripting.Preserve]
61+
public OptionUIAllowMultiInstanceAttribute(bool enable = false)
62+
{
63+
Enable = enable;
64+
}
65+
}
66+
}

Runtime/Attribute/OptionUIAllowMultiInstanceAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/BaseUIManager.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
// Official Documentation: https://gameframex.doc.alianblank.com/
2828
// ==========================================================================================
2929

30+
using System;
3031
using System.Collections.Generic;
3132
using GameFrameX.Asset.Runtime;
3233
using GameFrameX.ObjectPool;
@@ -203,6 +204,42 @@ public bool IsEnableUIShowAnimation
203204
set { m_IsEnableUIShowAnimation = value; }
204205
}
205206

207+
[UnityEngine.Scripting.Preserve]
208+
private bool m_EnableUIFormSingleton = true;
209+
210+
/// <summary>
211+
/// 获取或设置是否启用界面单实例打开模式。
212+
/// </summary>
213+
/// <remarks>
214+
/// Gets or sets whether singleton mode is enabled when opening UI forms.
215+
/// </remarks>
216+
[UnityEngine.Scripting.Preserve]
217+
public bool EnableUIFormSingleton
218+
{
219+
get { return m_EnableUIFormSingleton; }
220+
set { m_EnableUIFormSingleton = value; }
221+
}
222+
223+
/// <summary>
224+
/// 是否对指定界面类型采用单实例打开模式。
225+
/// </summary>
226+
/// <remarks>
227+
/// Determines whether singleton open mode should be used for the specified UI form type.
228+
/// </remarks>
229+
/// <param name="uiFormType">界面类型。/ The UI form type.</param>
230+
/// <returns>是否采用单实例模式。/ Whether singleton mode should be used.</returns>
231+
[UnityEngine.Scripting.Preserve]
232+
protected bool UseSingletonOpenMode(Type uiFormType)
233+
{
234+
if (!EnableUIFormSingleton || uiFormType == null)
235+
{
236+
return false;
237+
}
238+
239+
var allowMultiAttr = Attribute.GetCustomAttribute(uiFormType, typeof(OptionUIAllowMultiInstanceAttribute)) as OptionUIAllowMultiInstanceAttribute;
240+
return allowMultiAttr == null || !allowMultiAttr.Enable;
241+
}
242+
206243
[UnityEngine.Scripting.Preserve]
207244
protected IObjectPool<UIFormInstanceObject> m_InstancePool = null;
208245
[UnityEngine.Scripting.Preserve]

Runtime/UI/IUIManager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ public interface IUIManager
8585
/// </remarks>
8686
bool IsEnableUIHideAnimation { get; set; }
8787

88+
/// <summary>
89+
/// 获取或设置是否启用界面单实例打开模式。
90+
/// </summary>
91+
/// <remarks>
92+
/// Gets or sets whether singleton mode is enabled when opening UI forms.
93+
/// </remarks>
94+
bool EnableUIFormSingleton { get; set; }
95+
8896
/// <summary>
8997
/// 获取或设置界面实例对象池对象过期秒数。
9098
/// </summary>

Runtime/UIComponent.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ public partial class UIComponent : GameFrameworkComponent
103103
[UnityEngine.Scripting.Preserve]
104104
[SerializeField] private bool m_IsEnableUIHideAnimation = false;
105105

106+
/// <summary>
107+
/// 是否启用界面单实例打开模式。
108+
/// </summary>
109+
/// <remarks>
110+
/// Whether to enable singleton mode when opening UI forms.
111+
/// </remarks>
112+
[UnityEngine.Scripting.Preserve]
113+
[SerializeField] private bool m_EnableUIFormSingleton = true;
114+
106115
/// <summary>
107116
/// UI 自动回收间隔时间/秒。
108117
/// </summary>
@@ -286,6 +295,26 @@ public bool IsEnableUIHideAnimation
286295
get { return m_UIManager.IsEnableUIHideAnimation; }
287296
}
288297

298+
/// <summary>
299+
/// 获取或设置是否启用界面单实例打开模式。
300+
/// </summary>
301+
/// <remarks>
302+
/// Gets or sets whether singleton mode is enabled when opening UI forms.
303+
/// </remarks>
304+
[UnityEngine.Scripting.Preserve]
305+
public bool EnableUIFormSingleton
306+
{
307+
get { return m_EnableUIFormSingleton; }
308+
set
309+
{
310+
m_EnableUIFormSingleton = value;
311+
if (m_UIManager != null)
312+
{
313+
m_UIManager.EnableUIFormSingleton = value;
314+
}
315+
}
316+
}
317+
289318
/// <summary>
290319
/// 获取界面组数量。
291320
/// </summary>
@@ -481,6 +510,7 @@ private void Start()
481510
m_UIManager.RecycleInterval = m_RecycleInterval;
482511
m_UIManager.IsEnableUIHideAnimation = m_IsEnableUIHideAnimation;
483512
m_UIManager.IsEnableUIShowAnimation = m_IsEnableUIShowAnimation;
513+
m_UIManager.EnableUIFormSingleton = m_EnableUIFormSingleton;
484514
// m_UIManager.InstancePriority = m_InstancePriority;
485515

486516
m_CustomUIGroupHelper = Helper.CreateHelper(m_UIGroupHelperTypeName, m_CustomUIGroupHelper);

0 commit comments

Comments
 (0)