| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #if UNITY_EDITOR
- using UnityEngine;
- using UnityEditor;
- using System.IO;
- using MazeGen;
- namespace MazeGen.Editor
- {
- /// <summary>
- /// 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.
- /// </summary>
- 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<Weapon>(WeaponsResourcePath, "NewWeapon.asset");
- }
- [MenuItem("Assets/Create/MazeGen/Armor", priority = 20)]
- public static void CreateArmor()
- {
- CreateItemAsset<Armor>(ArmorResourcePath, "NewArmor.asset");
- }
- [MenuItem("Assets/Create/MazeGen/Consumable", priority = 20)]
- public static void CreateConsumable()
- {
- CreateItemAsset<Consumable>(ConsumableResourcePath, "NewConsumable.asset");
- }
- /// <summary>
- /// Generic method to create and save an item asset.
- /// Creates the resource folder if it doesn't exist, then shows a save dialog.
- /// </summary>
- private static void CreateItemAsset<T>(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<T>();
- 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
|