完整启动流程

This commit is contained in:
2026-05-19 23:20:44 +08:00
parent d0a1112737
commit 5fd981f5b9
22 changed files with 1938 additions and 14 deletions

View File

@@ -27,6 +27,12 @@ namespace BaseGames.Combat
public BreakLevel Break;
public string SourceId;
public string SkillId;
/// <summary>
/// 攻击来源投射物(仅当攻击方是 Projectile 时非 null
/// 用于弹反成功时调用 ReflectAsPlayerProjectile() 翻转阵营。
/// [NonSerialized]MonoBehaviour 引用不参与 Unity 资产序列化。
/// </summary>
[System.NonSerialized] public Projectile SourceProjectile;
// ── Builder ──────────────────────────────────────────────────────────
/// <summary>
@@ -48,6 +54,7 @@ namespace BaseGames.Combat
private BreakLevel _break;
private Vector2 _sourcePosition;
private int _sourceLayer;
private Projectile _sourceProjectile;
public Builder() { }
@@ -65,6 +72,7 @@ namespace BaseGames.Combat
public Builder SetBreak(BreakLevel v) { _break = v; return this; }
public Builder SetSourcePos(Vector2 v) { _sourcePosition = v; return this; }
public Builder SetLayer(int v) { _sourceLayer = v; return this; }
public Builder SetProjectile(Projectile v) { _sourceProjectile = v; return this; }
public DamageInfo Build() => new DamageInfo
{
@@ -83,6 +91,7 @@ namespace BaseGames.Combat
Break = _break,
SourcePosition = _sourcePosition,
SourceLayer = _sourceLayer,
SourceProjectile = _sourceProjectile,
};
}
@@ -95,7 +104,8 @@ namespace BaseGames.Combat
DamageSourceSO so,
Vector2 knockbackDir = default,
Vector2 sourcePos = default,
int sourceLayer = 0)
int sourceLayer = 0,
Projectile sourceProjectile = null)
{
int baseAmt = Mathf.RoundToInt(so.BaseDamage * so.DamageMultiplier);
return new DamageInfo
@@ -115,6 +125,7 @@ namespace BaseGames.Combat
KnockbackForce = so.KnockbackForce,
SourcePosition = sourcePos,
SourceLayer = sourceLayer,
SourceProjectile = sourceProjectile,
};
}
}

View File

@@ -49,6 +49,9 @@ namespace BaseGames.Combat
/// <summary>命中确认委托PlayerCombat / EnemyCombat 订阅)。</summary>
public event System.Action<DamageInfo> OnHitConfirmed;
// 宿主投射物缓存Activate 时填入DamageInfo.SourceProjectile 写入用)
private Projectile _ownerProjectile;
/// <summary>
/// 激活 HitBox。source/attacker 均可选,未传则使用 Inspector 默认值。
/// ⚠️ 不存在 Activate(float duration) 重载。
@@ -86,6 +89,8 @@ namespace BaseGames.Combat
Debug.LogWarning($"[HitBox] {name}: Collider2D.isTrigger 应为 true。", this);
// 缓存 IClashServiceOnTriggerEnter2D 为物理热路径,避免每次调用 Dictionary 查找
_clashService = ServiceLocator.GetOrDefault<IClashService>();
// 缓存宿主投射物(仅 Projectile GameObject 上挂载的 HitBox 非 null
_ownerProjectile = GetComponent<Projectile>();
}
private void OnDisable()
@@ -124,7 +129,8 @@ namespace BaseGames.Combat
_currentSource,
knockDir,
_attackerTransform.position,
_attackerTransform.gameObject.layer);
_attackerTransform.gameObject.layer,
_ownerProjectile);
// ① 拼刀检测:当前 HitBox 携带 CanClash 标记,且碰到对立阵营的 HitBox 层
int otherLayer = other.gameObject.layer;

View File

@@ -64,7 +64,14 @@ namespace BaseGames.Combat
// 2. 弹反检查_parrySystem == null 时跳过)
// ParrySystem 只暴露窗口状态,伤害数据留在 Combat 层,无跨程序集数据依赖。
if (_parrySystem != null && info.Flags.HasFlag(DamageFlags.CanBeParried))
if (_parrySystem.ConsumeParry()) return;
{
if (_parrySystem.ConsumeParry())
{
// 若攻击来源是投射物,翻转其阵营 Layer 与飞行方向
info.SourceProjectile?.ReflectAsPlayerProjectile();
return;
}
}
// 3. 霸体检查_poiseSource == null 时跳过)
if (!info.Flags.HasFlag(DamageFlags.ForceBreak) && _poiseSource != null)

View File

@@ -51,7 +51,25 @@ namespace BaseGames.Combat
gameObject.layer = (ownerLayer == playerLayer) ? playerProjLayer : enemyProjLayer;
}
/// <summary>子类在此设定初速度或附加初始化逻辑。</summary>
/// <summary>
/// 弹反:将投射物阵营从 EnemyProjectile 切换为 PlayerProjectile。
/// 反转飞行方向,并重置 HitBox 命中记录使其能够命中新目标(敌人)。
/// 由 HurtBox.ReceiveDamage() 在弹反成功后调用。
/// </summary>
public virtual void ReflectAsPlayerProjectile()
{
int playerProjLayer = LayerMask.NameToLayer("PlayerProjectile");
if (playerProjLayer < 0) return;
gameObject.layer = playerProjLayer;
Direction = -Direction;
_rb.velocity = -_rb.velocity;
// 重置 HitBox 命中记录,确保反射后可命中新目标
_hitBox.Deactivate();
_hitBox.Activate(_config?.DamageSource);
}
protected virtual void OnInitialized() { }
protected virtual void Update()

View File

@@ -63,6 +63,8 @@ namespace BaseGames.Core.Assets
public const string Poolable = "Poolable";
public const string BGM = "BGM";
public const string Charms = "Charms";
/// <summary>游戏启动时预热下载的资产标签BootSequencer 使用)。</summary>
public const string Preload = "Preload";
}
}
}

View File

@@ -0,0 +1,104 @@
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using BaseGames.Core.Events;
namespace BaseGames.Core
{
/// <summary>
/// 游戏启动序列编排器(挂载在 Persistent 场景)。
///
/// 职责:
/// 1. 触发 Splash 演出(通过 EVT_SplashStartRequest 事件,与 BaseGames.UI 程序集解耦)。
/// 2. 并行预热 Addressable 依赖(标签 <see cref="Assets.AddressKeys.Labels.Preload"/>)。
/// 3. 两者均完成后协程返回,由 <see cref="GameManager"/> 继续加载主菜单场景。
///
/// 配置说明Inspector
/// _preloadLabel — 留空则跳过预热,游戏仍可正常启动。
/// _onSplashStartRequest / _onSplashComplete — 绑定同名 SO 资产;
/// 若留空则视为"无 Splash"直接进入主菜单。
/// </summary>
[DefaultExecutionOrder(-800)]
public class BootSequencer : MonoBehaviour
{
[Header("Addressable 预热(可选)")]
[Tooltip("填写 Addressable 标签名(如 'Preload')以在启动时预热该标签下的所有依赖;留空则跳过。")]
[SerializeField] private AssetLabelReference _preloadLabel;
[Header("Event Channels - Raise")]
[Tooltip("发布后 SplashScreenController 开始播放演出。留空则不显示 Splash。")]
[SerializeField] private VoidEventChannelSO _onSplashStartRequest;
[Tooltip("预热进度 [0,1]")]
[SerializeField] private FloatEventChannelSO _onPreloadProgress;
[Header("Event Channels - Listen")]
[Tooltip("SplashScreenController 演出结束时发布此事件。留空则不等待 Splash 完成。")]
[SerializeField] private VoidEventChannelSO _onSplashComplete;
private bool _splashDone;
private readonly CompositeDisposable _subs = new();
// ── 生命周期 ─────────────────────────────────────────────────────────
private void OnEnable()
{
_onSplashComplete?.Subscribe(() => _splashDone = true).AddTo(_subs);
}
private void OnDisable() => _subs.Clear();
// ── 公开 API供 GameManager 通过 yield 驱动)────────────────────────
/// <summary>
/// 运行完整启动序列Splash 演出 + Addressable 预热并行执行)。
/// 两者均完成后协程返回GameManager 随后加载主菜单场景并切换 FSM 状态。
/// </summary>
public IEnumerator RunBootSequenceCoroutine()
{
// 若未绑定 SplashComplete 频道,则无需等待 Splash直接视为已完成
_splashDone = (_onSplashComplete == null);
// 触发 Splash 演出SplashScreenController 订阅此事件)
_onSplashStartRequest?.Raise();
// 并行启动 Addressable 预热
bool preloadDone = false;
StartCoroutine(PreloadCoroutine(() => preloadDone = true));
// 等待两者均完成
yield return new WaitUntil(() => _splashDone && preloadDone);
}
// ── 内部Addressable 预热 ────────────────────────────────────────────
private IEnumerator PreloadCoroutine(System.Action onDone)
{
if (_preloadLabel == null || string.IsNullOrEmpty(_preloadLabel.labelString))
{
// 无预热配置,立即完成
_onPreloadProgress?.Raise(1f);
onDone?.Invoke();
yield break;
}
// 仅下载依赖(不实例化),最小化内存占用
var handle = Addressables.DownloadDependenciesAsync(_preloadLabel, false);
while (!handle.IsDone)
{
// 保留 10% 余量,避免 PercentComplete 到 0.9 后卡顿
_onPreloadProgress?.Raise(handle.PercentComplete * 0.9f);
yield return null;
}
if (handle.Status != AsyncOperationStatus.Succeeded)
Debug.LogWarning("[BootSequencer] Addressable 预热部分失败,游戏将继续启动。" +
" 请检查 Addressable 标签配置与网络连接。");
_onPreloadProgress?.Raise(1f);
Addressables.Release(handle);
onDone?.Invoke();
}
}
}

View File

@@ -1,5 +1,6 @@
using System.Collections;
using UnityEngine;
using BaseGames.Core.Assets;
using BaseGames.Core.Events;
using BaseGames.Core.States;
@@ -16,6 +17,7 @@ namespace BaseGames.Core
// ── Inspector 引用 ────────────────────────────────────────────────
[Header("Managers")]
[SerializeField] private SettingsManager _settingsManager;
[SerializeField] private BootSequencer _bootSequencer;
[Header("Event Channels - Listen")]
[SerializeField] private VoidEventChannelSO _onPlayerDied;
@@ -24,6 +26,10 @@ namespace BaseGames.Core
[SerializeField] private StringEventChannelSO _onBossFightStarted;
[SerializeField] private BoolEventChannelSO _onBossFightEnded;
[SerializeField] private VoidEventChannelSO _onDeathScreenConfirmed;
[Tooltip("所有场景切换请求(与 SceneService 共享同一 SO")]
[SerializeField] private SceneLoadRequestEventChannelSO _onSceneLoadRequest;
[Tooltip("SceneLoader 完成加载后发布(携带场景名称)")]
[SerializeField] private StringEventChannelSO _onSceneLoaded;
[Header("Event Channels - Raise")]
[SerializeField] private BaseEventChannelSO<GameStateId> _onGameStateChanged;
@@ -51,8 +57,30 @@ namespace BaseGames.Core
private void Start()
{
// 在 Start 广播初始状态,确保其他组件已在 OnEnable 中完成订阅。
// 广播初始状态,确保其他组件已在 OnEnable 中完成订阅。
_onGameStateChanged?.Raise(new Events.GameStateId(GameStates.Initializing.Id));
// 启动引导序列Splash → 预热 → 加载主菜单场景 → 切换到 MainMenu 状态
StartCoroutine(BootCoroutine());
}
/// <summary>
/// 游戏启动引导协程:
/// 1. 运行 BootSequencerSplash + Addressable 预热)
/// 2. 通过 ISceneService 加载主菜单场景
/// 3. SceneLoader 完成后发布 _onSceneLoaded → HandleSceneLoaded 切换 FSM 到 MainMenu
/// </summary>
private IEnumerator BootCoroutine()
{
if (_bootSequencer != null)
yield return StartCoroutine(_bootSequencer.RunBootSequenceCoroutine());
var sceneService = ServiceLocator.GetOrDefault<ISceneService>();
if (sceneService != null)
yield return StartCoroutine(sceneService.LoadMainMenuCoroutine());
else
Debug.LogError("[GameManager] ISceneService 未注册,无法加载主菜单。" +
" 请检查 Persistent 场景的 GameServiceRegistrar 配置。");
}
private void OnEnable()
@@ -63,6 +91,8 @@ namespace BaseGames.Core
_onBossFightStarted? .Subscribe(HandleBossFightStarted).AddTo(_subs);
_onBossFightEnded? .Subscribe(HandleBossFightEnded).AddTo(_subs);
_onDeathScreenConfirmed?.Subscribe(HandleDeathScreenConfirmed).AddTo(_subs);
_onSceneLoadRequest? .Subscribe(HandleSceneLoadRequest).AddTo(_subs);
_onSceneLoaded? .Subscribe(HandleSceneLoaded).AddTo(_subs);
}
private void OnDisable() => _subs.Clear();
@@ -125,6 +155,45 @@ namespace BaseGames.Core
else RequestTransition(GameStates.GameOver);
}
// ── 场景加载状态同步 ──────────────────────────────────────────────
/// <summary>
/// 当 SceneService 开始加载主要场景(非 Room 过渡)时,更新 FSM 到 LoadingScene 状态。
/// Room 过渡TransitionType.Room不经过此处状态保持 Gameplay。
/// </summary>
private void HandleSceneLoadRequest(SceneLoadRequest request)
{
if (request.TransitionType != TransitionType.Scene) return;
// 返回主菜单不经过 LoadingScene 中间状态_onSceneLoaded 会直接处理)
if (request.SceneName == AddressKeys.SceneMainMenu) return;
var cur = _fsm.CurrentStateId;
if (cur == GameStates.MainMenu
|| cur == GameStates.Gameplay
|| cur == GameStates.BossFight)
{
RequestTransition(GameStates.LoadingScene);
}
}
/// <summary>
/// SceneLoader 完成加载后根据目标场景名自动切换 FSM 状态:
/// SceneMainMenu → MainMenu适用于 Initializing / Paused / GameOver / LoadingScene
/// 其他场景 → Gameplay仅当处于 LoadingScene 时Room 过渡不受影响)
/// </summary>
private void HandleSceneLoaded(string sceneName)
{
if (sceneName == AddressKeys.SceneMainMenu)
{
RequestTransition(GameStates.MainMenu);
return;
}
if (_fsm.CurrentStateId == GameStates.LoadingScene)
RequestTransition(GameStates.Gameplay);
}
private bool _deathScreenConfirmed;
private GameStateId _prePauseState = GameStates.Gameplay;
private void HandleDeathScreenConfirmed() => _deathScreenConfirmed = true;

View File

@@ -20,19 +20,39 @@ namespace BaseGames.Core
[Header("Event Channels - Raise")]
[SerializeField] private StringEventChannelSO _onSceneLoaded;
[Header("Event Channels - Raise加载画面")]
[Tooltip("ShowLoadingScreen=true 时,在加载开始时发布。")]
[SerializeField] private VoidEventChannelSO _onLoadingStarted;
[Tooltip("ShowLoadingScreen=true 时,在加载完成时发布。")]
[SerializeField] private VoidEventChannelSO _onLoadingComplete;
[Tooltip("ShowLoadingScreen=true 时,持续发布加载进度 [0,1]")]
[SerializeField] private FloatEventChannelSO _onLoadingProgressUpdated;
private string _currentRoomScene;
private AsyncOperationHandle<SceneInstance> _currentHandle;
public IEnumerator LoadSceneCoroutine(SceneLoadRequest request)
{
if (request.ShowLoadingScreen)
_onLoadingStarted?.Raise();
// 先加载新场景Additive成功后再卸载旧场景
// 顺序保证:若加载失败,旧场景仍保持可用,不会出现无场景的空状态
var loadOp = Addressables.LoadSceneAsync(request.SceneName, LoadSceneMode.Additive);
yield return loadOp;
// 逐帧轮询以上报进度(不能直接 yield return loadOp那样无法回调进度
while (!loadOp.IsDone)
{
if (request.ShowLoadingScreen)
_onLoadingProgressUpdated?.Raise(loadOp.PercentComplete * 0.9f);
yield return null;
}
if (loadOp.Status != AsyncOperationStatus.Succeeded)
{
Debug.LogError($"[SceneLoader] 加载场景失败:{request.SceneName}(旧场景保持不变)");
if (request.ShowLoadingScreen)
_onLoadingComplete?.Raise();
yield break;
}
@@ -45,6 +65,13 @@ namespace BaseGames.Core
_currentHandle = loadOp;
_currentRoomScene = request.SceneName;
if (request.ShowLoadingScreen)
{
_onLoadingProgressUpdated?.Raise(1f);
_onLoadingComplete?.Raise();
}
_onSceneLoaded?.Raise(request.SceneName);
}

View File

@@ -69,6 +69,18 @@ namespace BaseGames.Editor
CreateAsset<VoidEventChannelSO> ("UI", "EVT_MapOpen");
CreateAsset<ColorblindModeEventChannelSO> ("UI", "EVT_ColorblindMode");
// ── 启动流程 / Splash ─────────────────────────────────────────────
CreateAsset<VoidEventChannelSO> ("UI/Splash", "EVT_SplashStartRequest");
CreateAsset<VoidEventChannelSO> ("UI/Splash", "EVT_SplashComplete");
// ── 启动流程 / Loading 画面 ───────────────────────────────────────
CreateAsset<VoidEventChannelSO> ("UI/Loading", "EVT_LoadingStarted");
CreateAsset<VoidEventChannelSO> ("UI/Loading", "EVT_LoadingComplete");
CreateAsset<FloatEventChannelSO> ("UI/Loading", "EVT_LoadingProgressUpdated");
// ── 启动流程 / 主菜单 ─────────────────────────────────────────────
CreateAsset<IntEventChannelSO> ("UI/MainMenu", "EVT_SlotConfirmed");
// ── World ─────────────────────────────────────────────────────────
CreateAsset<StringEventChannelSO> ("World", "EVT_SavePointActivated");
CreateAsset<VoidEventChannelSO> ("World", "EVT_CheckpointReached");

View File

@@ -0,0 +1,530 @@
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using BaseGames.Core;
using BaseGames.Core.Events;
using BaseGames.UI;
using BaseGames.UI.MainMenu;
using BaseGames.UI.Splash;
namespace BaseGames.Editor
{
/// <summary>
/// 启动流程安装向导(四步检查清单窗口)。
///
/// 功能:
/// Step 1 — 检测并创建启动流程所需的全部事件频道 SO 资产。
/// Step 2 — 检测 Persistent 场景中 BootSequencer / SplashScreenController /
/// LoadingScreenManager 的存在性并提供一键脚手架入口。
/// Step 3 — 检测 Scene_MainMenu 是否存在并提供一键脚手架入口。
/// Step 4 — 全局验证(必要字段绑定状态),输出操作报告。
///
/// 菜单BaseGames / Tools / Boot Flow Wizard
/// </summary>
public class BootFlowSetupWizard : EditorWindow
{
// ── 常量 ──────────────────────────────────────────────────────────────
private const string EventRoot = "Assets/_Game/Data/Events";
private const string PersistentName = "Scene_Persistent";
private const string MainMenuName = "Scene_MainMenu";
// 启动流程所需的事件频道资产清单 (subfolder, assetName, SO type)
private static readonly (string folder, string name, System.Type type)[] BootChannels =
{
("UI/Splash", "EVT_SplashStartRequest", typeof(VoidEventChannelSO)),
("UI/Splash", "EVT_SplashComplete", typeof(VoidEventChannelSO)),
("UI/Loading", "EVT_LoadingStarted", typeof(VoidEventChannelSO)),
("UI/Loading", "EVT_LoadingComplete", typeof(VoidEventChannelSO)),
("UI/Loading", "EVT_LoadingProgressUpdated",typeof(FloatEventChannelSO)),
("UI/MainMenu", "EVT_SlotConfirmed", typeof(IntEventChannelSO)),
("Core", "EVT_SceneLoadRequest", typeof(SceneLoadRequestEventChannelSO)),
("Core", "EVT_SceneLoaded", typeof(StringEventChannelSO)),
};
// ── UI 引用 ───────────────────────────────────────────────────────────
private ScrollView _scroll;
private Label _statusBar;
private readonly List<(Label label, System.Func<bool> check)> _checks = new();
// ── 菜单入口 ──────────────────────────────────────────────────────────
[MenuItem("BaseGames/Tools/Boot Flow Wizard", priority = 10)]
public static void Open()
{
var wnd = GetWindow<BootFlowSetupWizard>();
wnd.titleContent = new GUIContent("Boot Flow Wizard",
EditorGUIUtility.IconContent("d_PlayButton").image);
wnd.minSize = new Vector2(480, 620);
}
// ── 生命周期 ──────────────────────────────────────────────────────────
public void CreateGUI()
{
rootVisualElement.style.flexDirection = FlexDirection.Column;
BuildHeader();
_scroll = new ScrollView(ScrollViewMode.Vertical);
_scroll.style.flexGrow = 1;
rootVisualElement.Add(_scroll);
BuildStep1();
BuildStep2();
BuildStep3();
BuildStep4();
BuildStatusBar();
RefreshAllChecks();
}
private void OnFocus() => RefreshAllChecks();
// ── UI 构建 ───────────────────────────────────────────────────────────
private void BuildHeader()
{
var header = new VisualElement();
StyleRow(header, new Color(0.17f, 0.17f, 0.17f));
header.style.paddingLeft = 12;
header.style.paddingTop = 10;
header.style.paddingBottom = 10;
var title = new Label("🚀 Boot Flow Setup Wizard");
title.style.fontSize = 15;
title.style.unityFontStyleAndWeight = FontStyle.Bold;
title.style.color = new Color(0.85f, 0.92f, 1f);
header.Add(title);
var sub = new Label("完成以下四步即可激活完整游戏启动流程。");
sub.style.color = new Color(0.55f, 0.55f, 0.55f);
sub.style.fontSize = 11;
header.Add(sub);
rootVisualElement.Add(header);
}
private void BuildStep1()
{
var section = BuildSection("Step 1 — 创建事件频道资产", new Color(0.22f, 0.35f, 0.22f));
section.Add(MakeDescription("以下 SO 资产在 Assets/_Game/Data/Events/ 下创建,是启动流程各组件通信的基础。"));
foreach (var (folder, name, _) in BootChannels)
{
string path = $"{EventRoot}/{folder}/{name}.asset";
string display = $"{name} ({folder})";
var (row, statusLbl) = MakeCheckRow(display, () => AssetDatabase.LoadMainAssetAtPath(path) != null);
_checks.Add((statusLbl, () => AssetDatabase.LoadMainAssetAtPath(path) != null));
section.Add(row);
}
var btn = new Button(OnCreateEventChannels) { text = "一键创建所有缺失资产" };
StylePrimaryButton(btn);
section.Add(btn);
}
private void BuildStep2()
{
var section = BuildSection("Step 2 — 配置 Persistent 场景", new Color(0.22f, 0.27f, 0.38f));
section.Add(MakeDescription(
$"在打开 {PersistentName} 的情况下,点击下方按钮执行脚手架(幂等,已有组件不会重建)。"));
var checks = new (string label, System.Func<bool> fn)[]
{
("BootSequencer 组件存在", () => FindInLoadedScene<BootSequencer>(PersistentName) != null),
("SplashScreenController 组件存在", () => FindInLoadedScene<SplashScreenController>(PersistentName) != null),
("LoadingScreenManager 组件存在", () => FindInLoadedScene<LoadingScreenManager>(PersistentName) != null),
("BootSequencer._onSplashStartRequest 已绑定",
() => IsFieldBound(FindInLoadedScene<BootSequencer>(PersistentName), "_onSplashStartRequest")),
("BootSequencer._onSplashComplete 已绑定",
() => IsFieldBound(FindInLoadedScene<BootSequencer>(PersistentName), "_onSplashComplete")),
("SceneLoader._onLoadingStarted 已绑定",
() => IsFieldBound(FindInLoadedScene<SceneLoader>(PersistentName), "_onLoadingStarted")),
("GameManager._bootSequencer 已绑定",
() => IsFieldBound(FindInLoadedScene<GameManager>(PersistentName), "_bootSequencer")),
("GameManager._onSceneLoadRequest 已绑定",
() => IsFieldBound(FindInLoadedScene<GameManager>(PersistentName), "_onSceneLoadRequest")),
("GameManager._onSceneLoaded 已绑定",
() => IsFieldBound(FindInLoadedScene<GameManager>(PersistentName), "_onSceneLoaded")),
};
foreach (var (lbl, fn) in checks)
{
var (row, statusLbl) = MakeCheckRow(lbl, fn);
_checks.Add((statusLbl, fn));
section.Add(row);
}
var btn = new Button(OnScaffoldPersistent) { text = "脚手架 Persistent 场景(需先打开该场景)" };
StylePrimaryButton(btn);
section.Add(btn);
}
private void BuildStep3()
{
var section = BuildSection("Step 3 — 搭建 Scene_MainMenu", new Color(0.32f, 0.25f, 0.18f));
section.Add(MakeDescription(
$"创建新场景命名为 {MainMenuName},打开后点击下方按钮生成主菜单层级。"));
var checks = new (string label, System.Func<bool> fn)[]
{
("MainMenuController 组件存在", () => FindInLoadedScene<MainMenuController>(MainMenuName) != null),
("SaveSlotController 组件存在", () => FindInLoadedScene<SaveSlotController>(MainMenuName) != null),
("MainMenuController._onSceneLoadRequest 已绑定",
() => IsFieldBound(FindInLoadedScene<MainMenuController>(MainMenuName), "_onSceneLoadRequest")),
("MainMenuController._onSlotConfirmed 已绑定",
() => IsFieldBound(FindInLoadedScene<MainMenuController>(MainMenuName), "_onSlotConfirmed")),
("MainMenuController._firstGameSceneKey 非空",
() => IsStringFieldNonEmpty(FindInLoadedScene<MainMenuController>(MainMenuName), "_firstGameSceneKey")),
("Scene_MainMenu 已加入 Build Settings",
() => IsInBuildSettings(MainMenuName)),
};
foreach (var (lbl, fn) in checks)
{
var (row, statusLbl) = MakeCheckRow(lbl, fn);
_checks.Add((statusLbl, fn));
section.Add(row);
}
var row2 = new VisualElement();
row2.style.flexDirection = FlexDirection.Row;
row2.style.marginTop = 6;
var scaffoldBtn = new Button(OnScaffoldMainMenu) { text = "脚手架 MainMenu 场景" };
StylePrimaryButton(scaffoldBtn);
scaffoldBtn.style.flexGrow = 1;
scaffoldBtn.style.marginRight = 4;
row2.Add(scaffoldBtn);
var buildSettingsBtn = new Button(OpenBuildSettings) { text = "打开 Build Settings" };
StyleSecondaryButton(buildSettingsBtn);
buildSettingsBtn.style.width = 140;
row2.Add(buildSettingsBtn);
section.Add(row2);
}
private void BuildStep4()
{
var section = BuildSection("Step 4 — 全局验证", new Color(0.28f, 0.20f, 0.32f));
section.Add(MakeDescription("点击「运行全量检查」获取带详情的报告,帮助排查剩余遗漏项。"));
var btn = new Button(OnRunFullValidation) { text = "运行全量检查并输出报告" };
StylePrimaryButton(btn);
section.Add(btn);
}
private void BuildStatusBar()
{
_statusBar = new Label("— 点击「刷新」或切换焦点以更新状态 —");
_statusBar.style.paddingLeft = 8;
_statusBar.style.paddingTop = 4;
_statusBar.style.paddingBottom = 4;
_statusBar.style.borderTopWidth = 1;
_statusBar.style.borderTopColor = new Color(0.15f, 0.15f, 0.15f);
_statusBar.style.color = new Color(0.55f, 0.55f, 0.55f);
_statusBar.style.fontSize = 11;
var refreshBtn = new Button(RefreshAllChecks) { text = "↻ 刷新" };
refreshBtn.style.position = Position.Absolute;
refreshBtn.style.right = 8;
refreshBtn.style.top = 2;
refreshBtn.style.width = 60;
refreshBtn.style.height = 18;
refreshBtn.style.fontSize = 10;
var bar = new VisualElement();
bar.style.flexDirection = FlexDirection.Row;
bar.style.alignItems = Align.Center;
bar.Add(_statusBar);
bar.Add(refreshBtn);
rootVisualElement.Add(bar);
}
// ── 操作回调 ──────────────────────────────────────────────────────────
private void OnCreateEventChannels()
{
CreateEventChannelAssets.CreateAll();
AssetDatabase.Refresh();
RefreshAllChecks();
}
private void OnScaffoldPersistent()
{
SceneScaffoldTools.ScaffoldPersistentScene();
RefreshAllChecks();
}
private void OnScaffoldMainMenu()
{
SceneScaffoldTools.ScaffoldMainMenuScene();
RefreshAllChecks();
}
private static void OpenBuildSettings()
=> EditorWindow.GetWindow(System.Type.GetType(
"UnityEditor.BuildPlayerWindow,UnityEditor"));
private void OnRunFullValidation()
{
var report = new List<string>();
int passCount = 0;
int failCount = 0;
// Event channels
foreach (var (folder, name, _) in BootChannels)
{
string path = $"{EventRoot}/{folder}/{name}.asset";
bool ok = AssetDatabase.LoadMainAssetAtPath(path) != null;
if (ok) passCount++;
else { failCount++; report.Add($"[缺失] 事件频道资产:{path}"); }
}
// Components in Persistent
CheckComponent<BootSequencer>(PersistentName, "BootSequencer", report, ref passCount, ref failCount);
CheckComponent<SplashScreenController>(PersistentName, "SplashScreenController", report, ref passCount, ref failCount);
CheckComponent<LoadingScreenManager>(PersistentName, "LoadingScreenManager", report, ref passCount, ref failCount);
CheckComponent<GameManager>(PersistentName, "GameManager", report, ref passCount, ref failCount);
CheckComponent<SceneLoader>(PersistentName, "SceneLoader", report, ref passCount, ref failCount);
// Field bindings
CheckField<BootSequencer>(PersistentName, "_onSplashStartRequest", report, ref passCount, ref failCount);
CheckField<BootSequencer>(PersistentName, "_onSplashComplete", report, ref passCount, ref failCount);
CheckField<GameManager>(PersistentName, "_bootSequencer", report, ref passCount, ref failCount);
CheckField<GameManager>(PersistentName, "_onSceneLoadRequest", report, ref passCount, ref failCount);
CheckField<GameManager>(PersistentName, "_onSceneLoaded", report, ref passCount, ref failCount);
CheckField<SceneLoader>(PersistentName, "_onLoadingStarted", report, ref passCount, ref failCount);
CheckField<SceneLoader>(PersistentName, "_onLoadingComplete", report, ref passCount, ref failCount);
CheckField<SceneLoader>(PersistentName, "_onLoadingProgressUpdated", report, ref passCount, ref failCount);
// Main menu scene
CheckComponent<MainMenuController>(MainMenuName, "MainMenuController", report, ref passCount, ref failCount);
CheckField<MainMenuController>(MainMenuName, "_onSceneLoadRequest", report, ref passCount, ref failCount);
CheckField<MainMenuController>(MainMenuName, "_firstGameSceneKey", report, ref passCount, ref failCount, requireNonEmpty: true);
if (!IsInBuildSettings(MainMenuName))
{
failCount++;
report.Add($"[缺失] {MainMenuName} 未加入 Build Settings。");
}
else passCount++;
// Output
string summary = $"验证完成:{passCount} 项通过 / {failCount} 项需处理。";
if (failCount == 0)
Debug.Log($"[BootFlowWizard] ✅ {summary}");
else
{
foreach (string msg in report)
Debug.LogWarning($"[BootFlowWizard] {msg}");
Debug.LogWarning($"[BootFlowWizard] ⚠ {summary}");
}
_statusBar.text = summary;
_statusBar.style.color = failCount == 0
? new Color(0.4f, 0.9f, 0.4f)
: new Color(1f, 0.75f, 0.3f);
RefreshAllChecks();
}
// ── UI 辅助 ───────────────────────────────────────────────────────────
private VisualElement BuildSection(string title, Color headerColor)
{
var section = new VisualElement();
section.style.marginTop = 8;
section.style.marginLeft = 8;
section.style.marginRight = 8;
section.style.marginBottom = 0;
section.style.borderTopWidth = 1;
section.style.borderBottomWidth = 1;
section.style.borderLeftWidth = 1;
section.style.borderRightWidth = 1;
section.style.borderTopColor = new Color(0.25f, 0.25f, 0.25f);
section.style.borderBottomColor = new Color(0.25f, 0.25f, 0.25f);
section.style.borderLeftColor = new Color(0.25f, 0.25f, 0.25f);
section.style.borderRightColor = new Color(0.25f, 0.25f, 0.25f);
section.style.borderTopLeftRadius = 4;
section.style.borderTopRightRadius = 4;
section.style.borderBottomLeftRadius = 4;
section.style.borderBottomRightRadius = 4;
section.style.overflow = Overflow.Hidden;
var header = new Label(title);
header.style.backgroundColor = headerColor;
header.style.paddingLeft = 10;
header.style.paddingTop = 6;
header.style.paddingBottom = 6;
header.style.unityFontStyleAndWeight = FontStyle.Bold;
header.style.fontSize = 12;
header.style.color = new Color(0.92f, 0.92f, 0.92f);
section.Add(header);
var body = new VisualElement();
body.style.paddingLeft = 10;
body.style.paddingRight = 10;
body.style.paddingTop = 8;
body.style.paddingBottom = 10;
body.style.backgroundColor = new Color(0.18f, 0.18f, 0.18f);
body.name = "body";
section.Add(body);
_scroll.Add(section);
// Return body so callers add children to it
return body;
}
private static Label MakeDescription(string text)
{
var lbl = new Label(text);
lbl.style.whiteSpace = WhiteSpace.Normal;
lbl.style.color = new Color(0.55f, 0.55f, 0.55f);
lbl.style.fontSize = 11;
lbl.style.marginBottom = 6;
return lbl;
}
private static (VisualElement row, Label statusLabel) MakeCheckRow(string text, System.Func<bool> check)
{
var row = new VisualElement();
row.style.flexDirection = FlexDirection.Row;
row.style.alignItems = Align.Center;
row.style.marginBottom = 2;
bool ok = check();
var icon = new Label(ok ? "✅" : "⬜");
icon.style.width = 22;
icon.style.fontSize = 12;
var lbl = new Label(text);
lbl.style.flexGrow = 1;
lbl.style.fontSize = 11;
lbl.style.color = ok ? new Color(0.75f, 0.95f, 0.75f) : new Color(0.80f, 0.80f, 0.80f);
row.Add(icon);
row.Add(lbl);
return (row, icon);
}
private static void StylePrimaryButton(Button btn)
{
btn.style.backgroundColor = new Color(0.22f, 0.44f, 0.22f);
btn.style.color = Color.white;
btn.style.marginTop = 6;
btn.style.height = 26;
btn.style.borderTopLeftRadius = 3;
btn.style.borderTopRightRadius = 3;
btn.style.borderBottomLeftRadius = 3;
btn.style.borderBottomRightRadius = 3;
}
private static void StyleSecondaryButton(Button btn)
{
btn.style.backgroundColor = new Color(0.25f, 0.25f, 0.32f);
btn.style.color = new Color(0.85f, 0.85f, 0.95f);
btn.style.marginTop = 6;
btn.style.height = 26;
btn.style.borderTopLeftRadius = 3;
btn.style.borderTopRightRadius = 3;
btn.style.borderBottomLeftRadius = 3;
btn.style.borderBottomRightRadius = 3;
}
private static void StyleRow(VisualElement el, Color bg)
{
el.style.backgroundColor = bg;
}
private void RefreshAllChecks()
{
int done = 0;
foreach (var (lbl, fn) in _checks)
{
bool ok = fn();
lbl.text = ok ? "✅" : "⬜";
if (ok) done++;
}
if (_statusBar != null)
{
_statusBar.text = $"通过 {done} / {_checks.Count} 项检查";
_statusBar.style.color = done == _checks.Count
? new Color(0.4f, 0.9f, 0.4f)
: new Color(0.75f, 0.75f, 0.75f);
}
}
// ── 检查辅助 ──────────────────────────────────────────────────────────
private static T FindInLoadedScene<T>(string sceneName) where T : Component
{
for (int i = 0; i < UnityEngine.SceneManagement.SceneManager.sceneCount; i++)
{
var scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
if (!scene.isLoaded) continue;
if (!string.IsNullOrEmpty(sceneName) &&
!Path.GetFileNameWithoutExtension(scene.path).Equals(
sceneName, System.StringComparison.OrdinalIgnoreCase))
continue;
foreach (GameObject go in scene.GetRootGameObjects())
{
var comp = go.GetComponentInChildren<T>(true);
if (comp != null) return comp;
}
}
return null;
}
private static bool IsFieldBound(Object target, string fieldName)
{
if (target == null) return false;
var so = new SerializedObject(target);
var prop = so.FindProperty(fieldName);
if (prop == null) return false;
return prop.propertyType == SerializedPropertyType.ObjectReference
? prop.objectReferenceValue != null
: !string.IsNullOrEmpty(prop.stringValue);
}
private static bool IsStringFieldNonEmpty(Object target, string fieldName)
{
if (target == null) return false;
var so = new SerializedObject(target);
var prop = so.FindProperty(fieldName);
return prop != null && !string.IsNullOrEmpty(prop.stringValue);
}
private static bool IsInBuildSettings(string sceneName)
{
foreach (var scene in EditorBuildSettings.scenes)
{
if (Path.GetFileNameWithoutExtension(scene.path)
.Equals(sceneName, System.StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
private static void CheckComponent<T>(string sceneName, string label,
List<string> report, ref int pass, ref int fail) where T : Component
{
if (FindInLoadedScene<T>(sceneName) != null) pass++;
else { fail++; report.Add($"[缺失] {sceneName} 中未找到 {label}。请先打开该场景并运行脚手架。"); }
}
private static void CheckField<T>(string sceneName, string field,
List<string> report, ref int pass, ref int fail,
bool requireNonEmpty = false) where T : Component
{
var comp = FindInLoadedScene<T>(sceneName);
bool ok = requireNonEmpty
? IsStringFieldNonEmpty(comp, field)
: IsFieldBound(comp, field);
if (ok) pass++;
else { fail++; report.Add($"[未绑定] {typeof(T).Name}.{field}(在 {sceneName})。"); }
}
}
}

View File

@@ -8,7 +8,9 @@ using BaseGames.Core.Pool;
using BaseGames.Input;
using BaseGames.UI;
using BaseGames.UI.HUD;
using BaseGames.UI.MainMenu;
using BaseGames.UI.Menus;
using BaseGames.UI.Splash;
using BaseGames.World;
using PathBerserker2d;
using Unity.Cinemachine;
@@ -133,6 +135,38 @@ namespace BaseGames.Editor
GetOrAddComponent<Image>(respawnButtonGo);
Button respawnButton = GetOrAddComponent<Button>(respawnButtonGo);
// ── BootSequencer启动流程──────────────────────────────────────
GameObject bootSequencerGo = GetOrCreateChild(services, "BootSequencer").gameObject;
BootSequencer bootSequencer = GetOrAddComponent<BootSequencer>(bootSequencerGo);
AssignAsset(bootSequencer, "_onSplashStartRequest", report, false, "EVT_SplashStartRequest");
AssignAsset(bootSequencer, "_onSplashComplete", report, false, "EVT_SplashComplete");
AssignAsset(bootSequencer, "_onPreloadProgress", report, false, "EVT_LoadingProgressUpdated");
AssignReference(gameManager, "_bootSequencer", bootSequencer);
AssignAsset(gameManager, "_onSceneLoadRequest", report, false, "EVT_SceneLoadRequest");
AssignAsset(gameManager, "_onSceneLoaded", report, false, "EVT_SceneLoaded");
AssignAsset(sceneLoader, "_onLoadingStarted", report, false, "EVT_LoadingStarted");
AssignAsset(sceneLoader, "_onLoadingComplete", report, false, "EVT_LoadingComplete");
AssignAsset(sceneLoader, "_onLoadingProgressUpdated", report, false, "EVT_LoadingProgressUpdated");
// ── Canvas_Splash启动演出──────────────────────────────────────
GameObject splashCanvasGo = GetOrCreateCanvas(ui.transform, "Canvas_Splash", 100);
SplashScreenController splashCtrl = GetOrAddComponent<SplashScreenController>(splashCanvasGo);
AssignAsset(splashCtrl, "_onSplashStartRequest", report, false, "EVT_SplashStartRequest");
AssignAsset(splashCtrl, "_onSplashComplete", report, false, "EVT_SplashComplete");
// ── LoadingScreenManager加载遮罩──────────────────────────────
GameObject loadingCanvasGo = GetOrCreateCanvas(ui.transform, "Canvas_Loading", 99);
LoadingScreenManager loadingMgr = GetOrAddComponent<LoadingScreenManager>(loadingCanvasGo);
AssignAsset(loadingMgr, "_onLoadingStarted", report, false, "EVT_LoadingStarted");
AssignAsset(loadingMgr, "_onLoadingComplete", report, false, "EVT_LoadingComplete");
AssignAsset(loadingMgr, "_onLoadingProgressUpdated", report, false, "EVT_LoadingProgressUpdated");
loadingCanvasGo.SetActive(false);
report.Add("Canvas_Splash请将工作室 Logo CanvasGroup 赋给 _studioLogoGroup游戏标题 CanvasGroup 赋给 _gameTitleGroup。");
report.Add("Canvas_Loading请为 LoadingScreenManager 绑定 _progressBarSlider和 _loadingPanelGameObject。");
EnsureAudioSources(audioManagerGo, audioManager, report);
AssignReference(registrar, "_deathRespawnService", deathRespawnService);
@@ -141,8 +175,6 @@ namespace BaseGames.Editor
AssignReference(registrar, "_saveManager", gameSaveManager);
AssignReference(gameManager, "_settingsManager", settingsManager);
AssignReference(gameManager, "_deathRespawnService", deathRespawnService);
AssignReference(gameManager, "_sceneService", sceneService);
AssignAsset(gameManager, "_onPlayerDied", report, true, "EVT_PlayerDied");
AssignAsset(gameManager, "_onPauseRequested", report, false, "EVT_PauseRequested");
AssignAsset(gameManager, "_onResumeRequested", report, false, "EVT_ResumeRequested", "EVT_PauseResumed");
@@ -196,6 +228,76 @@ namespace BaseGames.Editor
MarkDirtyAndLog("Persistent 场景脚手架", root, report);
}
// ─────────────────────────────────────────────────────────────────────
// Scaffold Main Menu Scene
// ─────────────────────────────────────────────────────────────────────
/// <summary>
/// 在当前活动场景中生成完整的主菜单场景层级结构:
/// [MainMenu] → [Canvas_MainMenu] → 主按钮组 / SaveSlotPanel / SettingsPanel / CreditsPanel
/// 自动绑定所有已存在的相关事件频道 SO 资产。
/// </summary>
[MenuItem("BaseGames/Tools/Scaffold Main Menu Scene", priority = 202)]
public static void ScaffoldMainMenuScene()
{
var report = new List<string>();
EnsureEventChannelAssets(report);
// ── [MainMenu] 根节点 ─────────────────────────────────────────────
GameObject root = GetOrCreateRoot("[MainMenu]");
// ── Canvas_MainMenu排序层 10显示在 HUD 之上)────────────────
GameObject canvasGo = GetOrCreateCanvas(root.transform, "Canvas_MainMenu", 10);
// ── 主菜单控制器 ──────────────────────────────────────────────────
MainMenuController menuCtrl = GetOrAddComponent<MainMenuController>(canvasGo);
AssignAsset(menuCtrl, "_onGameStateChanged", report, false, "EVT_GameStateChanged", "EVT_GameState");
AssignAsset(menuCtrl, "_onSceneLoadRequest", report, false, "EVT_SceneLoadRequest");
AssignAsset(menuCtrl, "_onSlotConfirmed", report, false, "EVT_SlotConfirmed");
// ── 主按钮区域 ────────────────────────────────────────────────────
GameObject menuPanelGo = GetOrCreateChild(canvasGo.transform, "MenuPanel").gameObject;
GetOrAddComponent<VerticalLayoutGroup>(menuPanelGo);
GameObject btnNewGameGo = GetOrCreateButtonChild(menuPanelGo.transform, "Btn_NewGame", "新游戏");
GameObject btnContinueGo = GetOrCreateButtonChild(menuPanelGo.transform, "Btn_Continue", "继续");
GameObject btnSettingsGo = GetOrCreateButtonChild(menuPanelGo.transform, "Btn_Settings", "设置");
GameObject btnCreditsGo = GetOrCreateButtonChild(menuPanelGo.transform, "Btn_Credits", "制作团队");
GameObject btnQuitGo = GetOrCreateButtonChild(menuPanelGo.transform, "Btn_Quit", "退出");
AssignReference(menuCtrl, "_btnNewGame", btnNewGameGo.GetComponent<Button>());
AssignReference(menuCtrl, "_btnContinue", btnContinueGo.GetComponent<Button>());
AssignReference(menuCtrl, "_btnSettings", btnSettingsGo.GetComponent<Button>());
AssignReference(menuCtrl, "_btnCredits", btnCreditsGo.GetComponent<Button>());
AssignReference(menuCtrl, "_btnQuit", btnQuitGo.GetComponent<Button>());
AssignReference(menuCtrl, "_menuPanel", menuPanelGo);
// ── SaveSlotPanel ─────────────────────────────────────────────────
GameObject saveSlotPanelGo = GetOrCreateChild(canvasGo.transform, "SaveSlotPanel").gameObject;
saveSlotPanelGo.SetActive(false);
SaveSlotController saveSlotCtrl = GetOrAddComponent<SaveSlotController>(saveSlotPanelGo);
AssignAsset(saveSlotCtrl, "_onSlotConfirmed", report, false, "EVT_SlotConfirmed");
AssignReference(menuCtrl, "_saveSlotPanel", saveSlotPanelGo);
AssignReference(menuCtrl, "_saveSlotController", saveSlotCtrl);
// ── SettingsPanel ─────────────────────────────────────────────────
GameObject settingsPanelGo = GetOrCreateChild(canvasGo.transform, "SettingsPanel").gameObject;
settingsPanelGo.SetActive(false);
AssignReference(menuCtrl, "_settingsPanel", settingsPanelGo);
// ── CreditsPanel ──────────────────────────────────────────────────
GameObject creditsPanelGo = GetOrCreateChild(canvasGo.transform, "CreditsPanel").gameObject;
creditsPanelGo.SetActive(false);
AssignReference(menuCtrl, "_creditsPanel", creditsPanelGo);
report.Add("设置 MainMenuController._firstGameSceneKey 为第一个游戏场景的 Addressable Key字符串。");
report.Add("SaveSlotPanel 需要补充 3 个存档槽 Button 引用_slot0Btn / _slot1Btn / _slot2Btn。");
report.Add("建议为 MenuPanel 添加 RectTransform 入场动画所需的锚点配置,参考 MainMenuController._menuPanel 的偏移量。");
MarkDirtyAndLog("Main Menu 场景脚手架", root, report);
}
// ─────────────────────────────────────────────────────────────────────
// Scaffold Game Room
// ─────────────────────────────────────────────────────────────────────
@@ -403,6 +505,16 @@ namespace BaseGames.Editor
return canvasGo;
}
/// <summary>在指定父节点下创建一个带 Button 的菜单按钮子节点(幂等)。文本由美术后续补充。</summary>
private static GameObject GetOrCreateButtonChild(Transform parent, string name, string label)
{
GameObject go = GetOrCreateChild(parent, name).gameObject;
GetOrAddComponent<Image>(go);
GetOrAddComponent<Button>(go);
_ = label; // 占位:文本内容由美术在 Prefab/Scene 中设置
return go;
}
private static void AssignReference(Object target, string propertyName, Object value)
{
AssignReference(target, propertyName, value, null);

View File

@@ -24,6 +24,10 @@ namespace BaseGames.Editor
/// · EnemyProjectile ↔ Ground → 应碰撞(敌人投射物命中地形)
/// · PlayerHitBox ↔ PlayerHurtBox → 应忽略(玩家不自伤)
/// · PlayerProjectile ↔ EnemyProjectile → 应忽略子弹不互相碰撞Clash 系统单独处理)
/// · HazardHitBox ↔ PlayerHurtBox → 应碰撞(环境危险伤害玩家)
/// · HazardHitBox ↔ EnemyHurtBox → 应碰撞(环境危险伤害敌人,阵营中立)
/// · HazardHitBox ↔ PlayerHitBox → 应忽略(环境不触发拼刀)
/// · HazardHitBox ↔ EnemyHitBox → 应忽略(环境不触发拼刀)
/// </summary>
public static class Physics2DLayerReport
{
@@ -43,6 +47,10 @@ namespace BaseGames.Editor
new("EnemyProjectile", "Ground", true, "敌人投射物命中地形"),
new("PlayerHitBox", "PlayerHurtBox", false, "玩家不自伤"),
new("PlayerProjectile", "EnemyProjectile", false, "子弹不互相碰撞Clash 系统单独处理)"),
new("HazardHitBox", "PlayerHurtBox", true, "环境危险伤害玩家"),
new("HazardHitBox", "EnemyHurtBox", true, "环境危险伤害敌人(阵营中立)"),
new("HazardHitBox", "PlayerHitBox", false, "环境不触发拼刀"),
new("HazardHitBox", "EnemyHitBox", false, "环境不触发拼刀"),
};
// ─────────────────────────────────────────────────────────────────────

View File

@@ -0,0 +1,212 @@
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using BaseGames.Core;
using BaseGames.Core.Events;
namespace BaseGames.UI.MainMenu
{
/// <summary>
/// 主菜单 UI 控制器(挂载在 Scene_MainMenu 的根 Canvas 上)。
///
/// 面板结构(按 Inspector 绑定):
/// ├── MainButtonsPanel — 主按钮组(新游戏 / 继续 / 设置 / 制作团队 / 退出)
/// ├── SaveSlotPanel — 存档槽选择(新游戏 & 继续共用)
/// ├── SettingsPanel — 设置面板
/// └── CreditsPanel — 制作团队面板
///
/// 入场动画:主按钮组从下方滑入(代码驱动,无需 Animator
///
/// 流程:
/// 玩家选择存档槽SaveSlotController 发布 _onSlotConfirmed
/// → 关闭存档槽面板 → 发布 SceneLoadRequest目标游戏场景
/// → GameManager 响应,进入 LoadingScene 状态,显示加载画面,最终切换到 Gameplay。
/// </summary>
public class MainMenuController : MonoBehaviour
{
// ── 面板引用 ──────────────────────────────────────────────────────────
[Header("面板")]
[SerializeField] private CanvasGroup _mainButtonsGroup;
[SerializeField] private RectTransform _mainButtonsRect; // 用于滑入动画
[SerializeField] private GameObject _saveSlotPanel;
[SerializeField] private GameObject _settingsPanel;
[SerializeField] private GameObject _creditsPanel;
// ── 按钮引用 ──────────────────────────────────────────────────────────
[Header("主菜单按钮")]
[SerializeField] private Button _btnNewGame;
[SerializeField] private Button _btnContinue;
[SerializeField] private Button _btnSettings;
[SerializeField] private Button _btnCredits;
[SerializeField] private Button _btnQuit;
// ── 按钮(子面板关闭)────────────────────────────────────────────────
[Header("子面板关闭按钮(可选)")]
[SerializeField] private Button _btnCloseSaveSlot;
[SerializeField] private Button _btnCloseSettings;
[SerializeField] private Button _btnCloseCredits;
// ── 入场动画参数 ──────────────────────────────────────────────────────
[Header("入场动画")]
[Tooltip("按钮组初始偏移(像素,向下)")]
[SerializeField] private float _entrySlideOffset = 80f;
[Tooltip("入场动画持续时间(秒)")]
[SerializeField] private float _entryDuration = 0.55f;
// ── 游戏场景 ──────────────────────────────────────────────────────────
[Header("场景")]
[Tooltip("新游戏 / 继续后进入的第一个游戏场景Addressable Key")]
[SerializeField] private string _firstGameSceneKey = "Scene_Game_Chapter1";
// ── Event Channels ────────────────────────────────────────────────────
[Header("Event Channels - Listen")]
[SerializeField] private GameStateEventChannelSO _onGameStateChanged;
[Tooltip("SaveSlotController 完成选槽后发布(携带槽索引)")]
[SerializeField] private IntEventChannelSO _onSlotConfirmed;
[Header("Event Channels - Raise")]
[SerializeField] private SceneLoadRequestEventChannelSO _onSceneLoadRequest;
// ── 内部状态 ──────────────────────────────────────────────────────────
private readonly CompositeDisposable _subs = new();
private Vector2 _buttonsPanelOriginalPos;
// ── 生命周期 ──────────────────────────────────────────────────────────
private void Awake()
{
// 按钮绑定
_btnNewGame? .onClick.AddListener(OnNewGameClicked);
_btnContinue?.onClick.AddListener(OnContinueClicked);
_btnSettings?.onClick.AddListener(OnSettingsClicked);
_btnCredits? .onClick.AddListener(OnCreditsClicked);
_btnQuit? .onClick.AddListener(Application.Quit);
_btnCloseSaveSlot?.onClick.AddListener(() => SetPanel(_saveSlotPanel, false));
_btnCloseSettings?.onClick.AddListener(() => SetPanel(_settingsPanel, false));
_btnCloseCredits? .onClick.AddListener(() => SetPanel(_creditsPanel, false));
// 记录按钮组原始位置(供动画使用)
if (_mainButtonsRect != null)
_buttonsPanelOriginalPos = _mainButtonsRect.anchoredPosition;
// 初始状态:隐藏子面板,主按钮组不可见(等待入场动画)
SetPanel(_saveSlotPanel, false);
SetPanel(_settingsPanel, false);
SetPanel(_creditsPanel, false);
SetButtonsGroupVisible(false);
// 刷新"继续"按钮可用性(需要至少一个有效存档)
RefreshContinueButton();
}
private void OnEnable()
{
_onGameStateChanged?.Subscribe(HandleGameStateChanged).AddTo(_subs);
_onSlotConfirmed? .Subscribe(HandleSlotConfirmed).AddTo(_subs);
}
private void OnDisable() => _subs.Clear();
private void Start()
{
// 场景初始化完成后播放入场动画
StartCoroutine(PlayEntryAnimation());
}
// ── 入场动画 ─────────────────────────────────────────────────────────
private IEnumerator PlayEntryAnimation()
{
if (_mainButtonsGroup == null) yield break;
Vector2 startPos = _buttonsPanelOriginalPos - new Vector2(0f, _entrySlideOffset);
if (_mainButtonsRect != null)
_mainButtonsRect.anchoredPosition = startPos;
float elapsed = 0f;
while (elapsed < _entryDuration)
{
float t = Mathf.SmoothStep(0f, 1f, elapsed / _entryDuration);
_mainButtonsGroup.alpha = t;
if (_mainButtonsRect != null)
_mainButtonsRect.anchoredPosition = Vector2.Lerp(startPos, _buttonsPanelOriginalPos, t);
elapsed += Time.unscaledDeltaTime;
yield return null;
}
_mainButtonsGroup.alpha = 1f;
if (_mainButtonsRect != null)
_mainButtonsRect.anchoredPosition = _buttonsPanelOriginalPos;
_mainButtonsGroup.interactable = true;
_mainButtonsGroup.blocksRaycasts = true;
}
// ── 按钮回调 ─────────────────────────────────────────────────────────
private void OnNewGameClicked() => SetPanel(_saveSlotPanel, true);
private void OnContinueClicked() => SetPanel(_saveSlotPanel, true);
private void OnSettingsClicked() => SetPanel(_settingsPanel, true);
private void OnCreditsClicked() => SetPanel(_creditsPanel, true);
// ── 存档槽确认 ───────────────────────────────────────────────────────
private void HandleSlotConfirmed(int _)
{
SetPanel(_saveSlotPanel, false);
_onSceneLoadRequest?.Raise(new SceneLoadRequest
{
SceneName = _firstGameSceneKey,
TransitionType = TransitionType.Scene,
ShowLoadingScreen = true,
});
}
// ── 游戏状态响应 ─────────────────────────────────────────────────────
private void HandleGameStateChanged(GameStateId state)
{
bool isMainMenu = state == GameStates.MainMenu;
// 离开 MainMenu加载游戏中时锁定所有交互防止重复点击
if (_mainButtonsGroup != null)
{
_mainButtonsGroup.interactable = isMainMenu;
_mainButtonsGroup.blocksRaycasts = isMainMenu;
}
}
// ── 工具方法 ─────────────────────────────────────────────────────────
private void RefreshContinueButton()
{
if (_btnContinue == null) return;
var saveService = ServiceLocator.GetOrDefault<ISaveService>();
bool hasAny = saveService != null
&& (saveService.HasSave(0) || saveService.HasSave(1) || saveService.HasSave(2));
_btnContinue.interactable = hasAny;
}
private static void SetPanel(GameObject panel, bool active)
{
if (panel != null) panel.SetActive(active);
}
private void SetButtonsGroupVisible(bool visible)
{
if (_mainButtonsGroup == null) return;
_mainButtonsGroup.alpha = visible ? 1f : 0f;
_mainButtonsGroup.interactable = visible;
_mainButtonsGroup.blocksRaycasts = visible;
}
}
}

View File

@@ -1,5 +1,7 @@
using UnityEngine;
using UnityEngine.UI;
using BaseGames.Core;
using BaseGames.Core.Assets;
using BaseGames.Core.Events;
namespace BaseGames.UI
@@ -51,9 +53,9 @@ namespace BaseGames.UI
_uiManager.CloseTopPanel();
_onSceneLoadRequest?.Raise(new SceneLoadRequest
{
SceneName = "MainMenu",
SceneName = AddressKeys.SceneMainMenu,
TransitionType = TransitionType.Scene,
ShowLoadingScreen = true
ShowLoadingScreen = false,
});
}
}

View File

@@ -0,0 +1,138 @@
using System.Collections;
using UnityEngine;
using BaseGames.Core.Events;
namespace BaseGames.UI.Splash
{
/// <summary>
/// 游戏启动 Logo 演出控制器。挂载在 Persistent 场景的 Canvas_Splash 根节点。
///
/// 演出顺序:工作室 Logo 淡入 → 停留 → 淡出 → 游戏标题淡入 → 停留 → 黑幕整体淡出。
/// 任意按键 / 手柄键可跳过整段演出。
///
/// 与 Core 程序集完全解耦:
/// 监听 EVT_SplashStartRequestVoidEventChannelSO→ 开始演出。
/// 完成后发布 EVT_SplashCompleteVoidEventChannelSO→ 通知 BootSequencer 继续。
/// </summary>
public class SplashScreenController : MonoBehaviour
{
[Header("Canvas Groups")]
[Tooltip("整个 Splash 画布的根节点 CanvasGroup用于整体淡出黑幕")]
[SerializeField] private CanvasGroup _splashRoot;
[Tooltip("工作室 Logo CanvasGroup")]
[SerializeField] private CanvasGroup _studioLogoGroup;
[Tooltip("游戏标题 Logo CanvasGroup")]
[SerializeField] private CanvasGroup _gameTitleGroup;
[Header("时序(秒)")]
[SerializeField] private float _fadeInDuration = 0.8f;
[SerializeField] private float _holdDuration = 1.5f;
[SerializeField] private float _fadeOutDuration = 0.6f;
[SerializeField] private float _stageGapDuration = 0.3f;
[Header("Event Channels")]
[SerializeField] private VoidEventChannelSO _onSplashStartRequest; // Listen
[SerializeField] private VoidEventChannelSO _onSplashComplete; // Raise
private bool _skipRequested;
private readonly CompositeDisposable _subs = new();
// ── 生命周期 ─────────────────────────────────────────────────────────
private void Awake()
{
// 初始态:根节点完全不透明(充当开场黑幕),各 Logo 完全透明
SetAlpha(_splashRoot, 1f, blocksRaycasts: true);
SetAlpha(_studioLogoGroup, 0f, blocksRaycasts: false);
SetAlpha(_gameTitleGroup, 0f, blocksRaycasts: false);
}
private void OnEnable() => _onSplashStartRequest?.Subscribe(OnStartRequested).AddTo(_subs);
private void OnDisable() => _subs.Clear();
// ── 入口 ─────────────────────────────────────────────────────────────
private void OnStartRequested() => StartCoroutine(PlayAndNotify());
private IEnumerator PlayAndNotify()
{
yield return StartCoroutine(PlayAsync());
_onSplashComplete?.Raise();
}
// ── 演出主逻辑 ───────────────────────────────────────────────────────
private IEnumerator PlayAsync()
{
_skipRequested = false;
// Stage 1工作室 Logo
if (_studioLogoGroup != null)
{
yield return StartCoroutine(FadeGroup(_studioLogoGroup, 0f, 1f, _fadeInDuration));
yield return StartCoroutine(WaitOrSkip(_holdDuration));
yield return StartCoroutine(FadeGroup(_studioLogoGroup, 1f, 0f, _fadeOutDuration));
}
if (!_skipRequested)
yield return new WaitForSecondsRealtime(_stageGapDuration);
// Stage 2游戏标题
if (_gameTitleGroup != null)
{
yield return StartCoroutine(FadeGroup(_gameTitleGroup, 0f, 1f, _fadeInDuration));
yield return StartCoroutine(WaitOrSkip(_holdDuration));
yield return StartCoroutine(FadeGroup(_gameTitleGroup, 1f, 0f, _fadeOutDuration));
}
// 黑幕整体淡出(显露主菜单场景)
yield return StartCoroutine(FadeGroup(_splashRoot, 1f, 0f, _fadeOutDuration));
if (_splashRoot != null)
_splashRoot.blocksRaycasts = false;
gameObject.SetActive(false);
}
// ── Unity 输入(任意按键跳过)────────────────────────────────────────
private void Update()
{
if (Input.anyKeyDown)
_skipRequested = true;
}
// ── 内部工具 ─────────────────────────────────────────────────────────
private IEnumerator FadeGroup(CanvasGroup group, float from, float to, float duration)
{
if (group == null) yield break;
group.alpha = from;
float elapsed = 0f;
while (elapsed < duration && !_skipRequested)
{
group.alpha = Mathf.Lerp(from, to, elapsed / duration);
elapsed += Time.unscaledDeltaTime;
yield return null;
}
group.alpha = to;
}
private IEnumerator WaitOrSkip(float duration)
{
float elapsed = 0f;
while (elapsed < duration && !_skipRequested)
{
elapsed += Time.unscaledDeltaTime;
yield return null;
}
}
private static void SetAlpha(CanvasGroup group, float alpha, bool blocksRaycasts)
{
if (group == null) return;
group.alpha = alpha;
group.blocksRaycasts = blocksRaycasts;
}
}
}