#if UNITY_EDITOR using UnityEngine; using UnityEditor; using System.IO; using MazeGen; namespace MazeGen.Editor { /// /// Provides context menu items for creating Weapon and Armor assets in the Unity Editor. /// Assets are created in Resources folders for easy runtime loading via Resources.Load/LoadAll. /// public class ItemCreationMenu { private const string WeaponsResourcePath = "Assets/Resources/Items/Weapons/"; private const string ArmorResourcePath = "Assets/Resources/Items/Armor/"; private const string ConsumableResourcePath = "Assets/Resources/Items/Consumables/"; [MenuItem("Assets/Create/MazeGen/Weapon", priority = 20)] public static void CreateWeapon() { CreateItemAsset(WeaponsResourcePath, "NewWeapon.asset"); } [MenuItem("Assets/Create/MazeGen/Armor", priority = 20)] public static void CreateArmor() { CreateItemAsset(ArmorResourcePath, "NewArmor.asset"); } [MenuItem("Assets/Create/MazeGen/Consumable", priority = 20)] public static void CreateConsumable() { CreateItemAsset(ConsumableResourcePath, "NewConsumable.asset"); } /// /// Generic method to create and save an item asset. /// Creates the resource folder if it doesn't exist, then shows a save dialog. /// private static void CreateItemAsset(string defaultPath, string defaultName) where T : Item { // Ensure the directory exists if (!Directory.Exists(defaultPath)) { Directory.CreateDirectory(defaultPath); AssetDatabase.Refresh(); } // Show save dialog string savePath = EditorUtility.SaveFilePanelInProject( $"Create New {typeof(T).Name}", defaultName, "asset", "Choose a location and name for the new asset", defaultPath ); if (string.IsNullOrEmpty(savePath)) return; // User cancelled // Create the asset T newItem = ScriptableObject.CreateInstance(); newItem.name = Path.GetFileNameWithoutExtension(savePath); // Save it AssetDatabase.CreateAsset(newItem, savePath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); // Select it in the Project window EditorGUIUtility.PingObject(newItem); Debug.Log($"Created {typeof(T).Name}: {savePath}"); } } } #endif