using UnityEditor; using UnityEngine; using PathBerserker2d; namespace BaseGames.Editor { /// /// 快捷键:BaseGames → Tools → Bake All NavSurfaces(Ctrl+Shift+B) /// 烘焙当前场景中所有 PathBerserker2d NavSurface 的导航网格。 /// 等效于在每个 NavSurface Inspector 中逐一点击 "Bake"。 /// public static class NavSurfaceBakeShortcut { [MenuItem("BaseGames/Tools/Bake All NavSurfaces %#b", priority = 100)] public static void BakeAll() { var surfaces = Object.FindObjectsByType(FindObjectsSortMode.None); if (surfaces.Length == 0) { Debug.Log("[NavSurfaceBake] 当前场景没有找到 NavSurface 组件。"); return; } int count = 0; foreach (var surface in surfaces) { if (surface == null) continue; surface.StartBakeJob(); EditorApplication.update -= MakeWatcher(surface); EditorApplication.update += MakeWatcher(surface); count++; } Debug.Log($"[NavSurfaceBake] 开始烘焙 {count} 个 NavSurface……"); } [MenuItem("BaseGames/Tools/Bake All NavSurfaces %#b", validate = true)] private static bool BakeAllValidate() { // 仅在非 Play Mode 时可用(NavSurface.Bake 仅支持编辑器模式) return !Application.isPlaying; } // ── 每个 NavSurface 独立监听烘焙完成 ────────────────────────────── private static EditorApplication.CallbackFunction MakeWatcher(NavSurface surface) { EditorApplication.CallbackFunction watcher = null; watcher = () => { if (surface == null || surface.BakeJob == null) { EditorApplication.update -= watcher; return; } if (surface.BakeJob.IsFinished) { EditorApplication.update -= watcher; EditorUtility.SetDirty(surface); Debug.Log($"[NavSurfaceBake] ✓ {surface.name} 烘焙完成({surface.BakeJob.TotalBakeTime} ms)"); } }; return watcher; } } }