合并 MenuForm 的 6 个按钮事件为 MenuButtonClickEventArgs

新增 MenuButtonId 枚举区分按钮来源,MenuController 改为单订阅 + switch 分派,删除 Menu{StartGame,File,Guide,Setting,Quit,About}EventArgs 及其 meta。MenuForm 的 OnXxxButtonClick 方法名按 prefab 反射绑定保留,仅薄薄转发到统一事件。
This commit is contained in:
SepComet 2026-06-14 17:38:01 +08:00
parent 4fe75fc8c6
commit 9c51f551c4
16 changed files with 105 additions and 256 deletions

View File

@ -0,0 +1,17 @@
namespace SepCore.Definition
{
/// <summary>
/// 主菜单按钮标识,用于区分 MenuForm 上各按钮触发的统一点击事件。
/// </summary>
public enum MenuButtonId : byte
{
Undefined = 0,
StartGame = 1,
File = 2,
Guide = 3,
Setting = 4,
Quit = 5,
About = 6,
}
}

View File

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

View File

@ -1,21 +0,0 @@
using GameFramework;
using GameFramework.Event;
namespace SepCore.Event
{
public class MenuAboutButtonClickEventArgs : GameEventArgs
{
public static readonly int EventId = typeof(MenuAboutButtonClickEventArgs).GetHashCode();
public override int Id => EventId;
public static MenuAboutButtonClickEventArgs Create()
{
return ReferencePool.Acquire<MenuAboutButtonClickEventArgs>();
}
public override void Clear()
{
}
}
}

View File

@ -0,0 +1,31 @@
using GameFramework;
using GameFramework.Event;
using SepCore.Definition;
namespace SepCore.Event
{
/// <summary>
/// MenuForm 上所有按钮共用的点击事件,按 <see cref="Button"/> 区分来源。
/// 每新增按钮请同步扩展 <see cref="MenuButtonId"/>,并在 MenuController 中处理。
/// </summary>
public class MenuButtonClickEventArgs : GameEventArgs
{
public static readonly int EventId = typeof(MenuButtonClickEventArgs).GetHashCode();
public override int Id => EventId;
public MenuButtonId Button { get; private set; }
public static MenuButtonClickEventArgs Create(MenuButtonId button)
{
var args = ReferencePool.Acquire<MenuButtonClickEventArgs>();
args.Button = button;
return args;
}
public override void Clear()
{
Button = MenuButtonId.Undefined;
}
}
}

View File

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

View File

@ -1,21 +0,0 @@
using GameFramework;
using GameFramework.Event;
namespace SepCore.Event
{
public class MenuFileButtonClickEventArgs : GameEventArgs
{
public static readonly int EventId = typeof(MenuFileButtonClickEventArgs).GetHashCode();
public override int Id => EventId;
public static MenuFileButtonClickEventArgs Create()
{
return ReferencePool.Acquire<MenuFileButtonClickEventArgs>();
}
public override void Clear()
{
}
}
}

View File

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

View File

@ -1,21 +0,0 @@
using GameFramework;
using GameFramework.Event;
namespace SepCore.Event
{
public class MenuGuideButtonClickEventArgs : GameEventArgs
{
public static readonly int EventId = typeof(MenuGuideButtonClickEventArgs).GetHashCode();
public override int Id => EventId;
public static MenuGuideButtonClickEventArgs Create()
{
return ReferencePool.Acquire<MenuGuideButtonClickEventArgs>();
}
public override void Clear()
{
}
}
}

View File

@ -1,21 +0,0 @@
using GameFramework;
using GameFramework.Event;
namespace SepCore.Event
{
public class MenuQuitButtonClickEventArgs : GameEventArgs
{
public static readonly int EventId = typeof(MenuQuitButtonClickEventArgs).GetHashCode();
public override int Id => EventId;
public static MenuQuitButtonClickEventArgs Create()
{
return ReferencePool.Acquire<MenuQuitButtonClickEventArgs>();
}
public override void Clear()
{
}
}
}

View File

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

View File

@ -1,21 +0,0 @@
using GameFramework;
using GameFramework.Event;
namespace SepCore.Event
{
public class MenuSettingButtonClickEventArgs : GameEventArgs
{
public static readonly int EventId = typeof(MenuSettingButtonClickEventArgs).GetHashCode();
public override int Id => EventId;
public static MenuSettingButtonClickEventArgs Create()
{
return ReferencePool.Acquire<MenuSettingButtonClickEventArgs>();
}
public override void Clear()
{
}
}
}

View File

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

View File

@ -1,21 +0,0 @@
using GameFramework;
using GameFramework.Event;
namespace SepCore.Event
{
public class MenuStartGameEventArgs : GameEventArgs
{
public static readonly int EventId = typeof(MenuStartGameEventArgs).GetHashCode();
public override int Id => EventId;
public static MenuStartGameEventArgs Create()
{
return ReferencePool.Acquire<MenuStartGameEventArgs>();
}
public override void Clear()
{
}
}
}

View File

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

View File

@ -17,22 +17,12 @@ namespace SepCore.UI
protected override void SubscribeCustomEvents()
{
GameEntry.Event.Subscribe(MenuStartGameEventArgs.EventId, OnMenuStartGameButtonClick);
GameEntry.Event.Subscribe(MenuFileButtonClickEventArgs.EventId, OnMenuFileButtonClick);
GameEntry.Event.Subscribe(MenuGuideButtonClickEventArgs.EventId, OnMenuGuideButtonClick);
GameEntry.Event.Subscribe(MenuSettingButtonClickEventArgs.EventId, OnMenuSettingButtonClick);
GameEntry.Event.Subscribe(MenuQuitButtonClickEventArgs.EventId, OnMenuQuitButtonClick);
GameEntry.Event.Subscribe(MenuAboutButtonClickEventArgs.EventId, OnMenuAboutButtonClick);
GameEntry.Event.Subscribe(MenuButtonClickEventArgs.EventId, OnMenuButtonClick);
}
protected override void UnsubscribeCustomEvents()
{
GameEntry.Event.Unsubscribe(MenuStartGameEventArgs.EventId, OnMenuStartGameButtonClick);
GameEntry.Event.Unsubscribe(MenuFileButtonClickEventArgs.EventId, OnMenuFileButtonClick);
GameEntry.Event.Unsubscribe(MenuGuideButtonClickEventArgs.EventId, OnMenuGuideButtonClick);
GameEntry.Event.Unsubscribe(MenuSettingButtonClickEventArgs.EventId, OnMenuSettingButtonClick);
GameEntry.Event.Unsubscribe(MenuQuitButtonClickEventArgs.EventId, OnMenuQuitButtonClick);
GameEntry.Event.Unsubscribe(MenuAboutButtonClickEventArgs.EventId, OnMenuAboutButtonClick);
GameEntry.Event.Unsubscribe(MenuButtonClickEventArgs.EventId, OnMenuButtonClick);
}
private static MenuContext BuildMenuContext()
@ -56,73 +46,46 @@ namespace SepCore.UI
Log.Info("MenuForm doesn't need UseCase");
}
private void OnMenuStartGameButtonClick(object sender, GameEventArgs e)
private void OnMenuButtonClick(object sender, GameEventArgs e)
{
if (sender is not MenuForm || e is not MenuStartGameEventArgs)
if (sender is not MenuForm || e is not MenuButtonClickEventArgs args)
{
return;
}
GameEntry.UIRouter.OpenUIAsync(UIFormType.SelectRoleForm).Forget();
}
private void OnMenuFileButtonClick(object sender, GameEventArgs e)
{
if (sender is not MenuForm || e is not MenuFileButtonClickEventArgs)
switch (args.Button)
{
return;
case MenuButtonId.StartGame:
GameEntry.UIRouter.OpenUIAsync(UIFormType.SelectRoleForm).Forget();
break;
case MenuButtonId.Setting:
GameEntry.UIRouter.OpenUIAsync(UIFormType.SettingForm).Forget();
break;
case MenuButtonId.Quit:
GameEntry.UIRouter.OpenUIAsync(UIFormType.DialogForm, new DialogRawData
{
Mode = 2,
Title = GameEntry.Localization.GetString("AskQuitGame.Title"),
Message = GameEntry.Localization.GetString("AskQuitGame.Message"),
OnClickConfirm = delegate(object userData)
{
UnityGameFramework.Runtime.GameEntry.Shutdown(ShutdownType.Quit);
}
}).Forget();
break;
case MenuButtonId.File:
Log.Warning("Menu file button click is not implemented.");
break;
case MenuButtonId.Guide:
Log.Warning("Menu guide button click is not implemented.");
break;
case MenuButtonId.About:
Log.Warning("Menu about button click is not implemented.");
break;
default:
Log.Warning("MenuController received unknown MenuButtonId: {0}", args.Button);
break;
}
Log.Warning("Menu file button click is not implemented.");
}
private void OnMenuGuideButtonClick(object sender, GameEventArgs e)
{
if (sender is not MenuForm || e is not MenuGuideButtonClickEventArgs)
{
return;
}
Log.Warning("Menu guide button click is not implemented.");
}
private void OnMenuSettingButtonClick(object sender, GameEventArgs e)
{
if (sender is not MenuForm || e is not MenuSettingButtonClickEventArgs)
{
return;
}
GameEntry.UIRouter.OpenUIAsync(UIFormType.SettingForm).Forget();
}
private void OnMenuQuitButtonClick(object sender, GameEventArgs e)
{
if (sender is not MenuForm || e is not MenuQuitButtonClickEventArgs)
{
return;
}
GameEntry.UIRouter.OpenUIAsync(UIFormType.DialogForm, new DialogRawData
{
Mode = 2,
Title = GameEntry.Localization.GetString("AskQuitGame.Title"),
Message = GameEntry.Localization.GetString("AskQuitGame.Message"),
OnClickConfirm = delegate(object userData)
{
UnityGameFramework.Runtime.GameEntry.Shutdown(ShutdownType.Quit);
}
}).Forget();
}
private void OnMenuAboutButtonClick(object sender, GameEventArgs e)
{
if (sender is not MenuForm || e is not MenuAboutButtonClickEventArgs)
{
return;
}
Log.Warning("Menu about button click is not implemented.");
}
}
}

View File

@ -1,3 +1,4 @@
using SepCore.Definition;
using SepCore.Event;
using UnityEngine;
using UnityEngine.EventSystems;
@ -39,34 +40,41 @@ namespace SepCore.UI
#endregion
// 以下方法被 MenuForm.prefab 中的 Button.onClick 通过名称反射绑定。
// 重命名前需同步修改 prefab 中的 m_MethodName 引用。
public void OnStartButtonClick()
{
GameEntry.Event.Fire(this, MenuStartGameEventArgs.Create());
FireButtonClick(MenuButtonId.StartGame);
}
public void OnFileButtonClick()
{
GameEntry.Event.Fire(this, MenuFileButtonClickEventArgs.Create());
FireButtonClick(MenuButtonId.File);
}
public void OnGuideButtonClick()
{
GameEntry.Event.Fire(this, MenuGuideButtonClickEventArgs.Create());
FireButtonClick(MenuButtonId.Guide);
}
public void OnSettingButtonClick()
{
GameEntry.Event.Fire(this, MenuSettingButtonClickEventArgs.Create());
FireButtonClick(MenuButtonId.Setting);
}
public void OnQuitButtonClick()
{
GameEntry.Event.Fire(this, MenuQuitButtonClickEventArgs.Create());
FireButtonClick(MenuButtonId.Quit);
}
public void OnAboutButtonClick()
{
GameEntry.Event.Fire(this, MenuAboutButtonClickEventArgs.Create());
FireButtonClick(MenuButtonId.About);
}
private void FireButtonClick(MenuButtonId button)
{
GameEntry.Event.Fire(this, MenuButtonClickEventArgs.Create(button));
}
}
}