| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #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 MonsterCreationMenu
- {
- private const string MonsterResourcePath = "Assets/Resources/Characters/Monsters/";
- [MenuItem("Assets/Create/MazeGen/Characters/Monster", priority = 20)]
- public static void CreateMonster()
- {
- CreateMonsterAsset<Monster>(MonsterResourcePath, "NewMonster.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 CreateMonsterAsset<T>(string defaultPath, string defaultName) where T : Monster
- {
- // 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
|