Files
megakoop/Editor/UIToolkitCleanup.cs
2025-10-05 18:18:12 +02:00

85 lines
3.2 KiB
C#

#if UNITY_EDITOR
using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
#if UNITY_2021_1_OR_NEWER
using UnityEngine.UIElements; // for UIDocument
#endif
namespace MegaKoop.EditorTools
{
public static class UIToolkitCleanup
{
[MenuItem("Tools/MegaKoop/Cleanup UI Toolkit Assets (Backup & Remove)", priority = 50)]
public static void CleanupUIToolkit()
{
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
string backupRoot = $"Assets/_Backup_UIToolkit_{timestamp}";
Directory.CreateDirectory(backupRoot);
// Collect UXML/USS assets
var all = AssetDatabase.GetAllAssetPaths();
var toMove = all.Where(p => p.EndsWith(".uxml", StringComparison.OrdinalIgnoreCase) || p.EndsWith(".uss", StringComparison.OrdinalIgnoreCase)).ToList();
// Also move common PanelSettings assets by name
toMove.AddRange(all.Where(p => Path.GetFileName(p).ToLower().Contains("panelsettings") && p.EndsWith(".asset", StringComparison.OrdinalIgnoreCase)));
// Move known UI Toolkit scripts (optional)
var uiToolkitScripts = new List<string>
{
"Assets/UI/Scripts/MainMenuController.cs",
"Assets/UI/Scripts/MultiplayerLobbyController.cs",
"Assets/UI/Scripts/PanelSettingsSetup.cs",
"Assets/UI/SimplePanelSettings.cs",
"Assets/UI/Editor/CreatePanelSettings.cs"
};
foreach (var path in toMove.Distinct())
{
string dest = Path.Combine(backupRoot, Path.GetFileName(path)).Replace('\\','/');
var err = AssetDatabase.MoveAsset(path, dest);
if (!string.IsNullOrEmpty(err))
{
Debug.LogWarning($"[Cleanup] Move failed for {path}: {err}");
}
}
foreach (var path in uiToolkitScripts)
{
if (File.Exists(path))
{
string dest = Path.Combine(backupRoot, Path.GetFileName(path)).Replace('\\','/');
var err = AssetDatabase.MoveAsset(path, dest);
if (!string.IsNullOrEmpty(err))
{
Debug.LogWarning($"[Cleanup] Move failed for {path}: {err}");
}
}
}
AssetDatabase.Refresh();
Debug.Log($"[UIToolkitCleanup] Moved {toMove.Count} assets to {backupRoot}.");
// Remove UIDocument components from all open scenes
int removed = 0;
#if UNITY_2021_1_OR_NEWER
foreach (var go in GameObject.FindObjectsOfType<GameObject>())
{
var doc = go.GetComponent<UIDocument>();
if (doc != null)
{
Undo.DestroyObjectImmediate(doc);
removed++;
}
}
#endif
Debug.Log($"[UIToolkitCleanup] Removed {removed} UIDocument components from the current scene(s).\nIf some scenes are not open, open them and re-run this to strip UIDocuments there as well.");
}
}
}
#endif