2025-09-10 13:47:59 +02:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using System.IO;
|
2025-09-11 13:00:26 +02:00
|
|
|
|
using Interactions;
|
|
|
|
|
|
using PuzzleS;
|
2025-09-10 13:47:59 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Editor
|
|
|
|
|
|
{
|
|
|
|
|
|
public class PrefabCreatorWindow : EditorWindow
|
|
|
|
|
|
{
|
|
|
|
|
|
private string _prefabName = "NewPrefab";
|
|
|
|
|
|
private string _saveFolderPath = "Assets/Prefabs/Items";
|
|
|
|
|
|
private string _pickupSoFolderPath = "Assets/Data/Items";
|
|
|
|
|
|
private string _puzzleSoFolderPath = "Assets/Data/Puzzles";
|
|
|
|
|
|
private bool _addObjective;
|
|
|
|
|
|
|
|
|
|
|
|
private PickupItemData _pickupData;
|
|
|
|
|
|
private PuzzleStepSO _objectiveData;
|
|
|
|
|
|
private UnityEditor.Editor _soEditor;
|
|
|
|
|
|
|
2025-09-11 13:15:43 +02:00
|
|
|
|
private enum ItemType { None, Pickup, ItemSlot }
|
|
|
|
|
|
private ItemType _itemType = ItemType.None;
|
|
|
|
|
|
|
|
|
|
|
|
private bool _createNext = false;
|
|
|
|
|
|
|
2025-09-24 13:33:43 +00:00
|
|
|
|
[MenuItem("AppleHills/Item Prefab Creator")]
|
2025-09-10 13:47:59 +02:00
|
|
|
|
public static void ShowWindow()
|
|
|
|
|
|
{
|
|
|
|
|
|
var window = GetWindow<PrefabCreatorWindow>("Prefab Creator");
|
|
|
|
|
|
window.minSize = new Vector2(400, 400);
|
|
|
|
|
|
// Set default paths if not already set
|
|
|
|
|
|
if (string.IsNullOrEmpty(window._saveFolderPath))
|
|
|
|
|
|
window._saveFolderPath = "Assets/Prefabs/Items";
|
|
|
|
|
|
if (string.IsNullOrEmpty(window._pickupSoFolderPath))
|
|
|
|
|
|
window._pickupSoFolderPath = "Assets/Data/Items";
|
|
|
|
|
|
if (string.IsNullOrEmpty(window._puzzleSoFolderPath))
|
|
|
|
|
|
window._puzzleSoFolderPath = "Assets/Data/Puzzles";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnGUI()
|
|
|
|
|
|
{
|
|
|
|
|
|
EditorGUILayout.LabelField("Prefab Creator", EditorStyles.boldLabel);
|
|
|
|
|
|
_prefabName = EditorGUILayout.TextField("Prefab Name", _prefabName);
|
|
|
|
|
|
// Prefab save folder
|
|
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
|
|
EditorGUILayout.PrefixLabel("Save Folder");
|
|
|
|
|
|
EditorGUILayout.SelectableLabel(_saveFolderPath, EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
|
|
|
|
|
|
if (GUILayout.Button("Select...", GUILayout.Width(80)))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_saveFolderPath = PrefabEditorUtility.SelectFolder(_saveFolderPath, "Prefabs/Items");
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
|
|
EditorGUILayout.LabelField("Add Components:", EditorStyles.boldLabel);
|
2025-09-11 13:15:43 +02:00
|
|
|
|
|
|
|
|
|
|
// Item type selection
|
|
|
|
|
|
var newType = (ItemType)EditorGUILayout.EnumPopup("Item Type", _itemType);
|
|
|
|
|
|
if (newType != _itemType)
|
|
|
|
|
|
{
|
|
|
|
|
|
_itemType = newType;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool addObjective = EditorGUILayout.Toggle("ObjectiveStepBehaviour", _addObjective);
|
|
|
|
|
|
_addObjective = addObjective;
|
2025-09-10 13:47:59 +02:00
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
|
|
|
2025-09-11 13:15:43 +02:00
|
|
|
|
// Pickup Data (for Pickup or ItemSlot)
|
|
|
|
|
|
if (_itemType == ItemType.Pickup || _itemType == ItemType.ItemSlot)
|
2025-09-10 13:47:59 +02:00
|
|
|
|
{
|
|
|
|
|
|
EditorGUILayout.LabelField("Pickup Data:", EditorStyles.boldLabel);
|
|
|
|
|
|
_pickupData = (PickupItemData)EditorGUILayout.ObjectField("PickupItemData", _pickupData, typeof(PickupItemData), false);
|
|
|
|
|
|
// Pickup SO save folder
|
|
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
|
|
EditorGUILayout.PrefixLabel("Save To");
|
|
|
|
|
|
EditorGUILayout.SelectableLabel(_pickupSoFolderPath, EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
|
|
|
|
|
|
if (GUILayout.Button("Select...", GUILayout.Width(80)))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_pickupSoFolderPath = PrefabEditorUtility.SelectFolder(_pickupSoFolderPath, "Data/Items");
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
|
|
if (_pickupData == null && GUILayout.Button("Create New PickupItemData"))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_pickupData = PrefabEditorUtility.CreateScriptableAsset<PickupItemData>(_prefabName + "_pickup", _pickupSoFolderPath);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
if (_pickupData != null)
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _pickupData);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-11 13:15:43 +02:00
|
|
|
|
// Objective Data
|
2025-09-10 13:47:59 +02:00
|
|
|
|
if (_addObjective)
|
|
|
|
|
|
{
|
|
|
|
|
|
EditorGUILayout.LabelField("Objective Data:", EditorStyles.boldLabel);
|
|
|
|
|
|
_objectiveData = (PuzzleStepSO)EditorGUILayout.ObjectField("PuzzleStepSO", _objectiveData, typeof(PuzzleStepSO), false);
|
|
|
|
|
|
// Puzzle SO save folder
|
|
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
|
|
EditorGUILayout.PrefixLabel("Save To");
|
|
|
|
|
|
EditorGUILayout.SelectableLabel(_puzzleSoFolderPath, EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
|
|
|
|
|
|
if (GUILayout.Button("Select...", GUILayout.Width(80)))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_puzzleSoFolderPath = PrefabEditorUtility.SelectFolder(_puzzleSoFolderPath, "Data/Puzzles");
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
|
|
if (_objectiveData == null && GUILayout.Button("Create New PuzzleStepSO"))
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
_objectiveData = PrefabEditorUtility.CreateScriptableAsset<PuzzleStepSO>(_prefabName + "_puzzle", _puzzleSoFolderPath);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
if (_objectiveData != null)
|
|
|
|
|
|
{
|
2025-09-10 14:45:25 +02:00
|
|
|
|
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _objectiveData);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
GUILayout.FlexibleSpace();
|
2025-09-11 13:15:43 +02:00
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
2025-09-10 13:47:59 +02:00
|
|
|
|
GUI.enabled = !string.IsNullOrEmpty(_prefabName) && !string.IsNullOrEmpty(_saveFolderPath);
|
2025-09-11 13:15:43 +02:00
|
|
|
|
if (GUILayout.Button("Create Prefab", GUILayout.Height(28)))
|
2025-09-10 13:47:59 +02:00
|
|
|
|
{
|
|
|
|
|
|
CreatePrefab();
|
|
|
|
|
|
}
|
2025-09-11 13:15:43 +02:00
|
|
|
|
_createNext = GUILayout.Toggle(_createNext, "Create Next", GUILayout.Width(100), GUILayout.Height(28));
|
2025-09-10 13:47:59 +02:00
|
|
|
|
GUI.enabled = true;
|
2025-09-11 13:15:43 +02:00
|
|
|
|
EditorGUILayout.EndHorizontal();
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CreatePrefab()
|
|
|
|
|
|
{
|
|
|
|
|
|
var go = new GameObject(_prefabName);
|
|
|
|
|
|
go.AddComponent<Interactable>();
|
|
|
|
|
|
go.AddComponent<BoxCollider>();
|
|
|
|
|
|
int interactableLayer = LayerMask.NameToLayer("Interactable");
|
|
|
|
|
|
if (interactableLayer != -1)
|
|
|
|
|
|
go.layer = interactableLayer;
|
|
|
|
|
|
go.AddComponent<SpriteRenderer>();
|
2025-09-11 13:15:43 +02:00
|
|
|
|
if (_itemType == ItemType.Pickup)
|
2025-09-10 13:47:59 +02:00
|
|
|
|
{
|
|
|
|
|
|
var pickup = go.AddComponent<Pickup>();
|
|
|
|
|
|
pickup.itemData = _pickupData;
|
|
|
|
|
|
}
|
2025-09-11 13:15:43 +02:00
|
|
|
|
else if (_itemType == ItemType.ItemSlot)
|
2025-09-10 13:47:59 +02:00
|
|
|
|
{
|
2025-09-11 13:15:43 +02:00
|
|
|
|
var slot = go.AddComponent<ItemSlot>();
|
|
|
|
|
|
slot.itemData = _pickupData;
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
if (_addObjective)
|
|
|
|
|
|
{
|
|
|
|
|
|
var obj = go.AddComponent<ObjectiveStepBehaviour>();
|
|
|
|
|
|
obj.stepData = _objectiveData;
|
|
|
|
|
|
}
|
|
|
|
|
|
string prefabPath = Path.Combine(_saveFolderPath, _prefabName + ".prefab").Replace("\\", "/");
|
2025-09-11 13:15:43 +02:00
|
|
|
|
var prefab = PrefabUtility.SaveAsPrefabAsset(go, prefabPath);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
DestroyImmediate(go);
|
|
|
|
|
|
AssetDatabase.Refresh();
|
2025-09-11 13:15:43 +02:00
|
|
|
|
Selection.activeObject = prefab;
|
|
|
|
|
|
EditorGUIUtility.PingObject(prefab);
|
2025-09-10 13:47:59 +02:00
|
|
|
|
EditorUtility.DisplayDialog("Prefab Created", $"Prefab saved to {prefabPath}", "OK");
|
2025-09-11 13:15:43 +02:00
|
|
|
|
if (_createNext)
|
|
|
|
|
|
{
|
|
|
|
|
|
_prefabName = "NewPrefab";
|
|
|
|
|
|
_pickupData = null;
|
|
|
|
|
|
_objectiveData = null;
|
|
|
|
|
|
_itemType = ItemType.None;
|
|
|
|
|
|
_addObjective = false;
|
|
|
|
|
|
_soEditor = null;
|
|
|
|
|
|
GUI.FocusControl(null);
|
|
|
|
|
|
Repaint();
|
|
|
|
|
|
}
|
2025-09-10 13:47:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|