Files

74 lines
2.4 KiB
C#

using System.Collections.Generic;
using BriarQueen.Data.Assets;
using BriarQueen.Framework.Registries;
using UnityEditor;
using UnityEngine;
namespace BriarQueen.Editor.Drawers.Registries
{
[CustomEditor(typeof(AssetRegistry))]
public class AssetRegistryFill : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
EditorGUILayout.Space();
if (GUILayout.Button("Discover AssetEntry Assets"))
{
Populate((AssetRegistry)target);
}
}
private static void Populate(AssetRegistry registry)
{
string[] guids = AssetDatabase.FindAssets("t:AssetEntry");
var scenes = new List<AssetEntry>();
var levels = new List<AssetEntry>();
var items = new List<AssetEntry>();
var ui = new List<AssetEntry>();
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
var entry = AssetDatabase.LoadAssetAtPath<AssetEntry>(path);
if (entry == null)
continue;
switch (entry.EntryType)
{
case AssetEntry.AssetEntryType.Scene:
scenes.Add(entry);
break;
case AssetEntry.AssetEntryType.Level:
levels.Add(entry);
break;
case AssetEntry.AssetEntryType.Item:
items.Add(entry);
break;
case AssetEntry.AssetEntryType.UI:
ui.Add(entry);
break;
}
}
scenes.Sort((a, b) => string.CompareOrdinal(a.name, b.name));
levels.Sort((a, b) => string.CompareOrdinal(a.name, b.name));
items.Sort((a, b) => string.CompareOrdinal(a.name, b.name));
ui.Sort((a, b) => string.CompareOrdinal(a.name, b.name));
Undo.RecordObject(registry, "Discover AssetEntry Assets");
registry.SetDiscoveredEntries(scenes, levels, items, ui);
EditorUtility.SetDirty(registry);
AssetDatabase.SaveAssets();
Debug.Log($"[AssetRegistry] Discovery complete. Scenes: {scenes.Count}, Levels: {levels.Count}, Items: {items.Count}, UI: {ui.Count}");
}
}
}