154 lines
6.8 KiB
C#
154 lines
6.8 KiB
C#
using Interactions;
|
|
using PuzzleS;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace Editor
|
|
{
|
|
public class ItemPrefabEditorWindow : EditorWindow
|
|
{
|
|
private GameObject _selectedGameObject;
|
|
private InteractableBase _interactable;
|
|
private PickupItemData _pickupData;
|
|
private PuzzleStepSO _objectiveData;
|
|
private UnityEditor.Editor _soEditor;
|
|
private string _pickupSoFolderPath = "Assets/Data/Items";
|
|
private string _puzzleSoFolderPath = "Assets/Data/Puzzles";
|
|
|
|
private enum ItemType { None, Pickup, ItemSlot }
|
|
private ItemType _itemType = ItemType.None;
|
|
|
|
[MenuItem("AppleHills/Item Prefab Editor")]
|
|
public static void ShowWindow()
|
|
{
|
|
var window = GetWindow<ItemPrefabEditorWindow>("Item Prefab Editor");
|
|
window.minSize = new Vector2(400, 400);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
Selection.selectionChanged += Repaint;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
Selection.selectionChanged -= Repaint;
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
_selectedGameObject = null;
|
|
_interactable = null;
|
|
if (Selection.activeGameObject != null)
|
|
{
|
|
_selectedGameObject = Selection.activeGameObject;
|
|
_interactable = _selectedGameObject.GetComponent<InteractableBase>();
|
|
}
|
|
else if (Selection.activeObject is GameObject go)
|
|
{
|
|
_selectedGameObject = go;
|
|
_interactable = go.GetComponent<InteractableBase>();
|
|
}
|
|
|
|
if (_selectedGameObject == null || _interactable == null)
|
|
{
|
|
EditorGUILayout.HelpBox("Select a GameObject or prefab with an InteractableBase component to edit.", MessageType.Info);
|
|
return;
|
|
}
|
|
|
|
EditorGUILayout.LabelField("Editing: ", _selectedGameObject.name, EditorStyles.boldLabel);
|
|
EditorGUILayout.Space();
|
|
|
|
// Determine current type
|
|
bool hasPickup = _selectedGameObject.GetComponent<Pickup>() != null;
|
|
bool hasSlot = _selectedGameObject.GetComponent<ItemSlot>() != null;
|
|
if (hasSlot) _itemType = ItemType.ItemSlot;
|
|
else if (hasPickup) _itemType = ItemType.Pickup;
|
|
else _itemType = ItemType.None;
|
|
|
|
// Item type selection
|
|
var newType = (ItemType)EditorGUILayout.EnumPopup("Item Type", _itemType);
|
|
if (newType != _itemType)
|
|
{
|
|
// Remove both, then add selected
|
|
PrefabEditorUtility.RemoveComponent<Pickup>(_selectedGameObject);
|
|
PrefabEditorUtility.RemoveComponent<ItemSlot>(_selectedGameObject);
|
|
if (newType == ItemType.Pickup)
|
|
PrefabEditorUtility.AddOrGetComponent<Pickup>(_selectedGameObject);
|
|
else if (newType == ItemType.ItemSlot)
|
|
PrefabEditorUtility.AddOrGetComponent<ItemSlot>(_selectedGameObject);
|
|
_itemType = newType;
|
|
}
|
|
|
|
// ObjectiveStepBehaviour
|
|
bool hasObjective = _selectedGameObject.GetComponent<ObjectiveStepBehaviour>() != null;
|
|
bool addObjective = EditorGUILayout.Toggle("ObjectiveStepBehaviour", hasObjective);
|
|
if (addObjective && !hasObjective)
|
|
{
|
|
PrefabEditorUtility.AddOrGetComponent<ObjectiveStepBehaviour>(_selectedGameObject);
|
|
}
|
|
else if (!addObjective && hasObjective)
|
|
{
|
|
PrefabEditorUtility.RemoveComponent<ObjectiveStepBehaviour>(_selectedGameObject);
|
|
}
|
|
|
|
// Pickup Data (for Pickup or ItemSlot)
|
|
if (_itemType == ItemType.Pickup || _itemType == ItemType.ItemSlot)
|
|
{
|
|
var pickup = _selectedGameObject.GetComponent<Pickup>();
|
|
_pickupData = pickup.itemData;
|
|
EditorGUILayout.LabelField("Pickup Data:", EditorStyles.boldLabel);
|
|
_pickupData = (PickupItemData)EditorGUILayout.ObjectField("PickupItemData", _pickupData, typeof(PickupItemData), false);
|
|
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>(_selectedGameObject.name + "_pickup", _pickupSoFolderPath);
|
|
}
|
|
if (_pickupData != null)
|
|
{
|
|
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _pickupData);
|
|
pickup.itemData = _pickupData;
|
|
}
|
|
}
|
|
|
|
// Objective Data
|
|
if (addObjective)
|
|
{
|
|
var obj = _selectedGameObject.GetComponent<ObjectiveStepBehaviour>();
|
|
_objectiveData = obj.stepData;
|
|
EditorGUILayout.LabelField("Objective Data:", EditorStyles.boldLabel);
|
|
_objectiveData = (PuzzleStepSO)EditorGUILayout.ObjectField("PuzzleStepSO", _objectiveData, typeof(PuzzleStepSO), false);
|
|
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>(_selectedGameObject.name + "_puzzle", _puzzleSoFolderPath);
|
|
}
|
|
if (_objectiveData != null)
|
|
{
|
|
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _objectiveData);
|
|
obj.stepData = _objectiveData;
|
|
}
|
|
}
|
|
if (GUI.changed)
|
|
{
|
|
EditorUtility.SetDirty(_selectedGameObject);
|
|
PrefabUtility.RecordPrefabInstancePropertyModifications(_selectedGameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|