MD111 / Assets /Editor /CoreCleaner.cs
Dido599999's picture
Upload missing building/character/environment assets + refresh stale files (part 4)
443cb74 verified
Raw History Blame Contribute Delete
2.59 kB
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
/// <summary>
/// CoreCleaner - cleans the boot scene so it contains ONLY what a player needs:
/// Kart (the McLaren) + Ground + Main Camera + Directional Light + volume
/// Everything else that the old agent dumped at the origin (a giant orange box,
/// sci-fi wall pieces, rocks) is moved into Assets/Scenes/Core/_removed/
/// instead of being deleted, so nothing is ever lost.
///
/// Menu: Tools > Clean Core Scene
/// Batch: Unity.exe -batchmode -quit -projectPath .. -executeMethod CoreCleaner.Run
/// </summary>
public static class CoreCleaner
{
const string Core = "Assets/Scenes/Core/Core.unity";
const string Parked = "Assets/Scenes/Core/_parked";
// roots we KEEP
static readonly HashSet<string> Keep = new HashSet<string>
{
"Kart", "Ground", "Main Camera", "Directional Light", "Global Volume"
};
public static void Run()
{
if (!File.Exists(Core)) { Debug.LogError("[CoreCleaner] " + Core + " missing"); return; }
var scene = EditorSceneManager.OpenScene(Core, OpenSceneMode.Single);
var roots = new List<GameObject>(scene.GetRootGameObjects());
int parked = 0;
var names = new List<string>();
foreach (var go in roots)
{
if (Keep.Contains(go.name)) { names.Add("KEEP " + go.name); continue; }
// everything else -> parked folder as a prefab (nothing is deleted)
if (!AssetDatabase.IsValidFolder(Parked))
AssetDatabase.CreateFolder("Assets/Scenes/Core", "_parked");
int rend = go.GetComponentsInChildren<MeshRenderer>(true).Length;
string file = Parked + "/" + go.name + ".prefab";
int i = 1;
while (AssetDatabase.LoadAssetAtPath<GameObject>(file) != null)
{
file = Parked + "/" + go.name + "_" + i + ".prefab";
i++;
}
PrefabUtility.SaveAsPrefabAsset(go, file);
names.Add("PARK " + go.name + " (renderers=" + rend + ") -> " + Path.GetFileName(file));
Object.DestroyImmediate(go);
parked++;
}
EditorSceneManager.MarkSceneDirty(scene);
EditorSceneManager.SaveScene(scene);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
foreach (var n in names) Debug.Log("[CoreCleaner] " + n);
Debug.Log("[CoreCleaner] DONE. kept=" + (names.Count - parked) + " parked=" + parked);
}
}