143 lines
6.5 KiB
C#
143 lines
6.5 KiB
C#
using UnityEditor;
|
|
using UnityEngine;
|
|
using System.IO;
|
|
using Interactions;
|
|
using PuzzleS;
|
|
|
|
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 _addPickup;
|
|
private bool _addCombineWith;
|
|
private bool _addSlot;
|
|
private bool _addObjective;
|
|
|
|
private PickupItemData _pickupData;
|
|
private PuzzleStepSO _objectiveData;
|
|
private UnityEditor.Editor _soEditor;
|
|
|
|
[MenuItem("Tools/Prefab Creator")]
|
|
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)))
|
|
{
|
|
_saveFolderPath = PrefabEditorUtility.SelectFolder(_saveFolderPath, "Prefabs/Items");
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
EditorGUILayout.Space();
|
|
EditorGUILayout.LabelField("Add Components:", EditorStyles.boldLabel);
|
|
_addPickup = EditorGUILayout.Toggle("Pickup", _addPickup);
|
|
_addCombineWith = EditorGUILayout.Toggle("CombineWithBehavior", _addCombineWith);
|
|
_addSlot = EditorGUILayout.Toggle("SlotItemBehavior", _addSlot);
|
|
_addObjective = EditorGUILayout.Toggle("ObjectiveStepBehaviour", _addObjective);
|
|
EditorGUILayout.Space();
|
|
|
|
if (_addPickup)
|
|
{
|
|
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)))
|
|
{
|
|
_pickupSoFolderPath = PrefabEditorUtility.SelectFolder(_pickupSoFolderPath, "Data/Items");
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
if (_pickupData == null && GUILayout.Button("Create New PickupItemData"))
|
|
{
|
|
_pickupData = PrefabEditorUtility.CreateScriptableAsset<PickupItemData>(_prefabName + "_pickup", _pickupSoFolderPath);
|
|
}
|
|
if (_pickupData != null)
|
|
{
|
|
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _pickupData);
|
|
}
|
|
}
|
|
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)))
|
|
{
|
|
_puzzleSoFolderPath = PrefabEditorUtility.SelectFolder(_puzzleSoFolderPath, "Data/Puzzles");
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
if (_objectiveData == null && GUILayout.Button("Create New PuzzleStepSO"))
|
|
{
|
|
_objectiveData = PrefabEditorUtility.CreateScriptableAsset<PuzzleStepSO>(_prefabName + "_puzzle", _puzzleSoFolderPath);
|
|
}
|
|
if (_objectiveData != null)
|
|
{
|
|
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _objectiveData);
|
|
}
|
|
}
|
|
GUILayout.FlexibleSpace();
|
|
GUI.enabled = !string.IsNullOrEmpty(_prefabName) && !string.IsNullOrEmpty(_saveFolderPath);
|
|
if (GUILayout.Button("Create Prefab"))
|
|
{
|
|
CreatePrefab();
|
|
}
|
|
GUI.enabled = true;
|
|
}
|
|
|
|
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>();
|
|
if (_addPickup)
|
|
{
|
|
var pickup = go.AddComponent<Pickup>();
|
|
pickup.itemData = _pickupData;
|
|
}
|
|
if (_addSlot)
|
|
{
|
|
go.AddComponent<ItemSlot>();
|
|
}
|
|
if (_addObjective)
|
|
{
|
|
var obj = go.AddComponent<ObjectiveStepBehaviour>();
|
|
obj.stepData = _objectiveData;
|
|
}
|
|
string prefabPath = Path.Combine(_saveFolderPath, _prefabName + ".prefab").Replace("\\", "/");
|
|
PrefabUtility.SaveAsPrefabAsset(go, prefabPath);
|
|
DestroyImmediate(go);
|
|
AssetDatabase.Refresh();
|
|
EditorUtility.DisplayDialog("Prefab Created", $"Prefab saved to {prefabPath}", "OK");
|
|
}
|
|
}
|
|
}
|