75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using Ashwild.Inventory;
|
|
|
|
namespace Ashwild.EditorTools
|
|
{
|
|
/// <summary>
|
|
/// Editor tool that (re)builds the ItemDatabase by scanning the project for every ItemData
|
|
/// asset and writing them, in a stable order, into Resources/ItemDatabase. Run it whenever
|
|
/// items are added or removed so their network ids stay in sync across builds.
|
|
/// </summary>
|
|
public static class ItemDatabaseBuilder
|
|
{
|
|
#region Constants
|
|
|
|
private const string ResourcesFolder = "Assets/GAME/Resources";
|
|
private const string DatabasePath = "Assets/GAME/Resources/ItemDatabase.asset";
|
|
|
|
#endregion
|
|
|
|
#region Menu
|
|
|
|
/// <summary>
|
|
/// Finds all ItemData assets and stores them in the (auto-created) ItemDatabase.
|
|
/// </summary>
|
|
[MenuItem("Tools/Ashwild/Rebuild Item Database")]
|
|
public static void Rebuild()
|
|
{
|
|
ItemDatabase database = GetOrCreateDatabase();
|
|
|
|
string[] guids = AssetDatabase.FindAssets("t:ItemData");
|
|
List<ItemData> items = new List<ItemData>(guids.Length);
|
|
for (int i = 0; i < guids.Length; i++)
|
|
{
|
|
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
|
|
ItemData item = AssetDatabase.LoadAssetAtPath<ItemData>(path);
|
|
if (item != null) items.Add(item);
|
|
}
|
|
|
|
// Stable order by asset name so ids don't shuffle between rebuilds.
|
|
items.Sort((a, b) => string.CompareOrdinal(a.name, b.name));
|
|
|
|
database.EditorSetItems(items.ToArray());
|
|
EditorUtility.SetDirty(database);
|
|
AssetDatabase.SaveAssets();
|
|
|
|
Debug.Log($"[ItemDatabaseBuilder] Registered {items.Count} items into {DatabasePath}.", database);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Internal Helpers
|
|
|
|
/// <summary>
|
|
/// Loads the ItemDatabase, creating it (and the Resources folder) if missing.
|
|
/// </summary>
|
|
private static ItemDatabase GetOrCreateDatabase()
|
|
{
|
|
ItemDatabase database = AssetDatabase.LoadAssetAtPath<ItemDatabase>(DatabasePath);
|
|
if (database != null) return database;
|
|
|
|
if (!AssetDatabase.IsValidFolder(ResourcesFolder))
|
|
AssetDatabase.CreateFolder("Assets/GAME", "Resources");
|
|
|
|
database = ScriptableObject.CreateInstance<ItemDatabase>();
|
|
AssetDatabase.CreateAsset(database, DatabasePath);
|
|
AssetDatabase.SaveAssets();
|
|
return database;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|