using Cysharp.Threading.Tasks; /// /// UI返回键策略接口 /// public interface IUIBackStrategy { /// /// 处理返回键 /// /// 触发返回的窗口 /// 是否成功处理 UniTask HandleBack(UIWindow window); } /// /// 策略1:不响应返回键 /// public class UIBackStrategy_None : IUIBackStrategy { public async UniTask HandleBack(UIWindow window) { // 不响应,返回false让下一个窗口处理 await UniTask.CompletedTask; return false; } } /// /// 策略2:关闭自己 /// public class UIBackStrategy_CloseSelf : IUIBackStrategy { public async UniTask HandleBack(UIWindow window) { if (window != null) { window.CloseWindow(destroy: true); DebugUtil.Log($"[BackStrategy] Close window: {window.WindowName}"); await UniTask.CompletedTask; return true; } return false; } } /// /// 策略3:什么都不做(拦截返回键但不执行任何操作) /// 适用场景:剧情播放中、加载中等不希望用户中断的场景 /// public class UIBackStrategy_DoNothing : IUIBackStrategy { public async UniTask HandleBack(UIWindow window) { // 拦截返回键,但什么都不做 DebugUtil.Log($"[BackStrategy] Back blocked by: {window?.WindowName}"); await UniTask.CompletedTask; return true; // 返回true表示已处理,阻止继续传递 } } /// /// 策略4:弹出退出游戏确认界面 /// 适用场景:主界面、登录界面等根界面 /// public class UIBackStrategy_ShowExitConfirm : IUIBackStrategy { public async UniTask HandleBack(UIWindow window) { // 检查退出确认界面是否已经打开 if (!UIManager.Instance.CheckWindowCreated(UINameConst.UIBackReturn)) { await UIManager.Instance.CreateAndOpenWindow(UINameConst.UIBackReturn); DebugUtil.Log($"[BackStrategy] Show exit confirm from: {window?.WindowName}"); } return true; } } /// /// 返回策略工厂 /// public static class UIBackStrategyFactory { private static UIBackStrategy_None _noneStrategy; private static UIBackStrategy_CloseSelf _closeSelfStrategy; private static UIBackStrategy_DoNothing _doNothingStrategy; private static UIBackStrategy_ShowExitConfirm _showExitConfirmStrategy; /// /// 根据策略类型创建策略实例 /// public static IUIBackStrategy CreateStrategy(UIBackStrategyType strategyType) { switch (strategyType) { case UIBackStrategyType.PassThrough: return GetNoneStrategy(); case UIBackStrategyType.CloseSelf: return GetCloseSelfStrategy(); case UIBackStrategyType.Intercept: return GetDoNothingStrategy(); case UIBackStrategyType.ShowExitConfirm: return GetShowExitConfirmStrategy(); default: return GetCloseSelfStrategy(); // 默认关闭自己 } } /// /// 获取"不响应"策略实例(单例) /// public static IUIBackStrategy GetNoneStrategy() { if (_noneStrategy == null) _noneStrategy = new UIBackStrategy_None(); return _noneStrategy; } /// /// 获取"关闭自己"策略实例(单例) /// public static IUIBackStrategy GetCloseSelfStrategy() { if (_closeSelfStrategy == null) _closeSelfStrategy = new UIBackStrategy_CloseSelf(); return _closeSelfStrategy; } /// /// 获取"什么都不做"策略实例(单例) /// public static IUIBackStrategy GetDoNothingStrategy() { if (_doNothingStrategy == null) _doNothingStrategy = new UIBackStrategy_DoNothing(); return _doNothingStrategy; } /// /// 获取"弹出退出确认"策略实例(单例) /// public static IUIBackStrategy GetShowExitConfirmStrategy() { if (_showExitConfirmStrategy == null) _showExitConfirmStrategy = new UIBackStrategy_ShowExitConfirm(); return _showExitConfirmStrategy; } }