using System; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEditor.Build; using UnityEditor.SceneManagement; using UnityEngine; public static class SetupBuildSettings { const string CoreScene = "Assets/Scenes/Core/Core.unity"; const string WorldFolder = "Assets/Scenes/World"; [MenuItem("Tools/Setup Chunk Scenes in Build Settings")] public static void AddChunkScenesToBuildSettings() { // 2026-09-29 — ROOT-CAUSE FIX. // The previous version of this menu hard-coded 12 paths // (Scene_01_Sea_South.unity ... Scene_12_Sea_North.unity) of which 11 do // not exist on disk any more. Running it replaced the verified 122-scene // build list (Core + 121 Chunk_*.unity) with 11 dangling entries, and // RunAll() called it too, so "Tools/Run All Optimizations" was a // one-click landmine. Scenes are now discovered from disk and a path is // never written unless the file actually exists. var discovered = DiscoverScenes(); if (discovered.Count == 0) { Debug.LogError("[SetupBuildSettings] No scenes found under " + WorldFolder + " - build list left UNCHANGED."); return; } var existing = EditorBuildSettings.scenes ?? new EditorBuildSettingsScene[0]; // Keep the entries we already have, in their current order (index 0 must // stay Core.unity) and with their current enabled flag preserved. var kept = new List(); var keptPaths = new List(); var dropped = new List(); foreach (var s in existing) { var p = s == null ? null : s.path; if (!string.IsNullOrEmpty(p) && File.Exists(p)) { if (!keptPaths.Contains(p)) { kept.Add(s); keptPaths.Add(p); } } else { dropped.Add(string.IsNullOrEmpty(p) ? "" : p); } } // Add only genuinely missing scenes; never touch order we already have. var added = new List(); foreach (var p in discovered) if (!keptPaths.Contains(p)) added.Add(p); if (dropped.Count == 0 && added.Count == 0) { Debug.Log("[SetupBuildSettings] Build list already valid: " + kept.Count + " scenes (Core at index " + IndexOf(keptPaths, CoreScene) + "). Nothing changed."); return; } if (!keptPaths.Contains(CoreScene) && File.Exists(CoreScene)) { kept.Insert(0, new EditorBuildSettingsScene(CoreScene, true)); keptPaths.Insert(0, CoreScene); } foreach (var p in added) { kept.Add(new EditorBuildSettingsScene(p, true)); keptPaths.Add(p); } // Final safety net: refuse to save a list containing a non-existent file. foreach (var s in kept) if (s == null || string.IsNullOrEmpty(s.path) || !File.Exists(s.path)) { Debug.LogError("[SetupBuildSettings] Refusing to save - dangling entry '" + (s == null ? "" : s.path) + "'. Build list left UNCHANGED."); return; } EditorBuildSettings.scenes = kept.ToArray(); AssetDatabase.SaveAssets(); Debug.Log("[SetupBuildSettings] Build settings updated: " + kept.Count + " scenes" + " (dropped " + dropped.Count + " dangling, added " + added.Count + " missing)." + (dropped.Count > 0 ? " Dropped: " + string.Join(", ", dropped.ToArray()) : "") + (added.Count > 0 ? " Added: " + string.Join(", ", added.ToArray()) : "")); } static List DiscoverScenes() { var list = new List(); if (File.Exists(CoreScene)) list.Add(CoreScene); var chunks = new List(); foreach (var guid in AssetDatabase.FindAssets("t:Scene", new[] { WorldFolder })) { var p = AssetDatabase.GUIDToAssetPath(guid); if (string.IsNullOrEmpty(p) || !File.Exists(p)) continue; if (!chunks.Contains(p)) chunks.Add(p); } chunks.Sort(StringComparer.Ordinal); list.AddRange(chunks); return list; } static int IndexOf(List paths, string wanted) { for (int i = 0; i < paths.Count; i++) if (string.Equals(paths[i], wanted, StringComparison.OrdinalIgnoreCase)) return i; return -1; } [MenuItem("Tools/Optimize Quality Settings for Performance")] public static void OptimizeQualitySettings() { // Get current quality settings var settings = QualitySettings.GetQualitySettings(); // We can't easily modify QualitySettings.asset from script, but we can log what to change Debug.Log("[OptimizeQualitySettings] Manual changes needed in QualitySettings.asset:"); Debug.Log(" - Set shadowDistance to 30 (was 40)"); Debug.Log(" - Set shadowResolution to 0 (Low) for Mobile/PC"); Debug.Log(" - Set shadowCascades to 2 (was 4)"); Debug.Log(" - Set terrainPixelError to 5 (was 1)"); Debug.Log(" - Set terrainDetailDistance to 60 (was 80)"); Debug.Log(" - Set terrainTreeDistance to 1000 (was 5000)"); Debug.Log(" - Set lodBias to 1.5 (was 1/2)"); Debug.Log(" - Disable softParticles, realtimeReflectionProbes"); Debug.Log(" - Set vSyncCount to 1 (was 0)"); Debug.Log(" - Set antiAliasing to 1 (was 0 - off)"); } [MenuItem("Tools/Optimize URP Asset for Performance")] public static void OptimizeURPAsset() { Debug.Log("[OptimizeURPAsset] Manual changes needed in PC_RPAsset.asset:"); Debug.Log(" - m_ShadowDistance: 30 (was 50)"); Debug.Log(" - m_MainLightShadowmapResolution: 1024 (was 2048)"); Debug.Log(" - m_AdditionalLightsShadowmapResolution: 1024 (was 2048)"); Debug.Log(" - m_ShadowCascadeCount: 2 (was 4)"); Debug.Log(" - m_AdditionalLightsPerObjectLimit: 2 (was 4)"); Debug.Log(" - m_SoftShadowsSupported: 0 (was 1)"); Debug.Log(" - m_SupportsHDR: 0 (was 1) - if not needed"); Debug.Log(" - m_MSAA: 0 (was 1 - 2x MSAA)"); Debug.Log(" - m_RequireDepthTexture: 0 (was 1 - if not used)"); Debug.Log(" - m_RequireOpaqueTexture: 0 (was 1 - if not used)"); Debug.Log(" - Disable SSAO (ScreenSpaceAmbientOcclusion feature)"); } [MenuItem("Tools/Run All Optimizations")] public static void RunAll() { AddChunkScenesToBuildSettings(); OptimizeQualitySettings(); OptimizeURPAsset(); Debug.Log("[SetupBuildSettings] All optimization steps logged. Apply manually in Inspector."); } }