74 lines
2.6 KiB
C#
74 lines
2.6 KiB
C#
using System.IO;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using Ashwild.Building;
|
|
|
|
namespace Ashwild.EditorTools
|
|
{
|
|
/// <summary>
|
|
/// Editor-only factory that creates BuildableData assets for the Ashwild Database window.
|
|
/// Kept separate from the window so buildable authoring stays a single-purpose, testable helper —
|
|
/// mirrors RecipeAssetFactory for recipes and ItemAssetFactory for items.
|
|
/// </summary>
|
|
public static class BuildableAssetFactory
|
|
{
|
|
#region Constants
|
|
|
|
private const string BuildablesFolder = "Assets/GAME/ScriptableObjects/Buildables";
|
|
|
|
#endregion
|
|
|
|
#region Buildable Asset
|
|
|
|
/// <summary>
|
|
/// Creates a fresh BuildableData asset under the buildables folder with a unique name, seeding
|
|
/// its display name so the new asset is immediately coherent in the menu list. Returns the asset
|
|
/// for selection, or null if the folder cannot be made.
|
|
/// </summary>
|
|
public static BuildableData CreateBuildable(string desiredName)
|
|
{
|
|
if (!EnsureFolder(BuildablesFolder)) return null;
|
|
|
|
string safeName = string.IsNullOrWhiteSpace(desiredName) ? "NewBuildable" : desiredName.Trim();
|
|
string assetPath = AssetDatabase.GenerateUniqueAssetPath($"{BuildablesFolder}/{safeName}.asset");
|
|
|
|
BuildableData buildable = ScriptableObject.CreateInstance<BuildableData>();
|
|
AssetDatabase.CreateAsset(buildable, assetPath);
|
|
|
|
SerializedObject so = new SerializedObject(buildable);
|
|
so.FindProperty("displayName").stringValue = safeName;
|
|
so.ApplyModifiedPropertiesWithoutUndo();
|
|
|
|
EditorUtility.SetDirty(buildable);
|
|
AssetDatabase.SaveAssets();
|
|
return buildable;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Internal Helpers
|
|
|
|
/// <summary>
|
|
/// Ensures a project-relative asset folder exists, creating any missing segments. Returns
|
|
/// false (and logs) when the path cannot be created.
|
|
/// </summary>
|
|
private static bool EnsureFolder(string folder)
|
|
{
|
|
if (AssetDatabase.IsValidFolder(folder)) return true;
|
|
|
|
string parent = Path.GetDirectoryName(folder).Replace('\\', '/');
|
|
string leaf = Path.GetFileName(folder);
|
|
if (!EnsureFolder(parent))
|
|
{
|
|
Debug.LogError($"[BuildableAssetFactory] Could not create folder '{folder}'.");
|
|
return false;
|
|
}
|
|
|
|
AssetDatabase.CreateFolder(parent, leaf);
|
|
return AssetDatabase.IsValidFolder(folder);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|