Move the prefab editor to a dedicated tool window

This commit is contained in:
Michal Pikulski
2025-09-10 15:53:31 +02:00
parent 9579a8ef57
commit 5d81a2acfc
2 changed files with 58 additions and 29 deletions

View File

@@ -13,5 +13,5 @@ MonoBehaviour:
m_Name: Headband m_Name: Headband
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemName: Headband itemName: Headband
description: Item dropped by Uncle Kunt description: Item dropped by Uncle Kunt!
mapSprite: {fileID: 743298078511449570, guid: 6f463983177b1404ca39fa222f03a3b2, type: 3} mapSprite: {fileID: 743298078511449570, guid: 6f463983177b1404ca39fa222f03a3b2, type: 3}

View File

@@ -1,76 +1,106 @@
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using System.IO;
namespace Editor namespace Editor
{ {
[CustomEditor(typeof(GameObject))] public class ItemPrefabEditorWindow : EditorWindow
public class ItemPrefabEditor : UnityEditor.Editor
{ {
private GameObject _selectedGameObject;
private Interactable _interactable;
private PickupItemData _pickupData; private PickupItemData _pickupData;
private PuzzleStepSO _objectiveData; private PuzzleStepSO _objectiveData;
private UnityEditor.Editor _soEditor; private UnityEditor.Editor _soEditor;
private string _pickupSoFolderPath = "Assets/Data/Items"; private string _pickupSoFolderPath = "Assets/Data/Items";
private string _puzzleSoFolderPath = "Assets/Data/Puzzles"; private string _puzzleSoFolderPath = "Assets/Data/Puzzles";
public override void OnInspectorGUI() [MenuItem("Tools/Item Prefab Editor")]
public static void ShowWindow()
{ {
GameObject go = (GameObject)target; var window = GetWindow<ItemPrefabEditorWindow>("Item Prefab Editor");
bool isItem = go.GetComponent<Interactable>() != null; window.minSize = new Vector2(400, 400);
if (!isItem) }
private void OnEnable()
{
Selection.selectionChanged += Repaint;
}
private void OnDisable()
{
Selection.selectionChanged -= Repaint;
}
private void OnGUI()
{
_selectedGameObject = null;
_interactable = null;
if (Selection.activeGameObject != null)
{ {
DrawDefaultInspector(); _selectedGameObject = Selection.activeGameObject;
_interactable = _selectedGameObject.GetComponent<Interactable>();
}
else if (Selection.activeObject is GameObject go)
{
_selectedGameObject = go;
_interactable = go.GetComponent<Interactable>();
}
if (_selectedGameObject == null || _interactable == null)
{
EditorGUILayout.HelpBox("Select a GameObject or prefab with an Interactable component to edit.", MessageType.Info);
return; return;
} }
EditorGUILayout.LabelField("Item Prefab Editor", EditorStyles.boldLabel);
EditorGUILayout.LabelField("Editing: ", _selectedGameObject.name, EditorStyles.boldLabel);
EditorGUILayout.Space();
// Pickup // Pickup
bool hasPickup = go.GetComponent<Pickup>() != null; bool hasPickup = _selectedGameObject.GetComponent<Pickup>() != null;
bool addPickup = EditorGUILayout.Toggle("Pickup", hasPickup); bool addPickup = EditorGUILayout.Toggle("Pickup", hasPickup);
if (addPickup && !hasPickup) if (addPickup && !hasPickup)
{ {
PrefabEditorUtility.AddOrGetComponent<Pickup>(go); PrefabEditorUtility.AddOrGetComponent<Pickup>(_selectedGameObject);
} }
else if (!addPickup && hasPickup) else if (!addPickup && hasPickup)
{ {
PrefabEditorUtility.RemoveComponent<Pickup>(go); PrefabEditorUtility.RemoveComponent<Pickup>(_selectedGameObject);
} }
// CombineWithBehavior // CombineWithBehavior
bool hasCombine = go.GetComponent<CombineWithBehavior>() != null; bool hasCombine = _selectedGameObject.GetComponent<CombineWithBehavior>() != null;
bool addCombine = EditorGUILayout.Toggle("CombineWithBehavior", hasCombine); bool addCombine = EditorGUILayout.Toggle("CombineWithBehavior", hasCombine);
if (addCombine && !hasCombine) if (addCombine && !hasCombine)
{ {
PrefabEditorUtility.AddOrGetComponent<CombineWithBehavior>(go); PrefabEditorUtility.AddOrGetComponent<CombineWithBehavior>(_selectedGameObject);
} }
else if (!addCombine && hasCombine) else if (!addCombine && hasCombine)
{ {
PrefabEditorUtility.RemoveComponent<CombineWithBehavior>(go); PrefabEditorUtility.RemoveComponent<CombineWithBehavior>(_selectedGameObject);
} }
// SlotItemBehavior // SlotItemBehavior
bool hasSlot = go.GetComponent<SlotItemBehavior>() != null; bool hasSlot = _selectedGameObject.GetComponent<SlotItemBehavior>() != null;
bool addSlot = EditorGUILayout.Toggle("SlotItemBehavior", hasSlot); bool addSlot = EditorGUILayout.Toggle("SlotItemBehavior", hasSlot);
if (addSlot && !hasSlot) if (addSlot && !hasSlot)
{ {
PrefabEditorUtility.AddOrGetComponent<SlotItemBehavior>(go); PrefabEditorUtility.AddOrGetComponent<SlotItemBehavior>(_selectedGameObject);
} }
else if (!addSlot && hasSlot) else if (!addSlot && hasSlot)
{ {
PrefabEditorUtility.RemoveComponent<SlotItemBehavior>(go); PrefabEditorUtility.RemoveComponent<SlotItemBehavior>(_selectedGameObject);
} }
// ObjectiveStepBehaviour // ObjectiveStepBehaviour
bool hasObjective = go.GetComponent<ObjectiveStepBehaviour>() != null; bool hasObjective = _selectedGameObject.GetComponent<ObjectiveStepBehaviour>() != null;
bool addObjective = EditorGUILayout.Toggle("ObjectiveStepBehaviour", hasObjective); bool addObjective = EditorGUILayout.Toggle("ObjectiveStepBehaviour", hasObjective);
if (addObjective && !hasObjective) if (addObjective && !hasObjective)
{ {
PrefabEditorUtility.AddOrGetComponent<ObjectiveStepBehaviour>(go); PrefabEditorUtility.AddOrGetComponent<ObjectiveStepBehaviour>(_selectedGameObject);
} }
else if (!addObjective && hasObjective) else if (!addObjective && hasObjective)
{ {
PrefabEditorUtility.RemoveComponent<ObjectiveStepBehaviour>(go); PrefabEditorUtility.RemoveComponent<ObjectiveStepBehaviour>(_selectedGameObject);
} }
// Pickup Data // Pickup Data
if (addPickup) if (addPickup)
{ {
var pickup = go.GetComponent<Pickup>(); var pickup = _selectedGameObject.GetComponent<Pickup>();
_pickupData = pickup.itemData; _pickupData = pickup.itemData;
EditorGUILayout.LabelField("Pickup Data:", EditorStyles.boldLabel); EditorGUILayout.LabelField("Pickup Data:", EditorStyles.boldLabel);
_pickupData = (PickupItemData)EditorGUILayout.ObjectField("PickupItemData", _pickupData, typeof(PickupItemData), false); _pickupData = (PickupItemData)EditorGUILayout.ObjectField("PickupItemData", _pickupData, typeof(PickupItemData), false);
@@ -84,7 +114,7 @@ namespace Editor
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
if (_pickupData == null && GUILayout.Button("Create New PickupItemData")) if (_pickupData == null && GUILayout.Button("Create New PickupItemData"))
{ {
_pickupData = PrefabEditorUtility.CreateScriptableAsset<PickupItemData>(go.name + "_pickup", _pickupSoFolderPath); _pickupData = PrefabEditorUtility.CreateScriptableAsset<PickupItemData>(_selectedGameObject.name + "_pickup", _pickupSoFolderPath);
} }
if (_pickupData != null) if (_pickupData != null)
{ {
@@ -95,7 +125,7 @@ namespace Editor
// Objective Data // Objective Data
if (addObjective) if (addObjective)
{ {
var obj = go.GetComponent<ObjectiveStepBehaviour>(); var obj = _selectedGameObject.GetComponent<ObjectiveStepBehaviour>();
_objectiveData = obj.stepData; _objectiveData = obj.stepData;
EditorGUILayout.LabelField("Objective Data:", EditorStyles.boldLabel); EditorGUILayout.LabelField("Objective Data:", EditorStyles.boldLabel);
_objectiveData = (PuzzleStepSO)EditorGUILayout.ObjectField("PuzzleStepSO", _objectiveData, typeof(PuzzleStepSO), false); _objectiveData = (PuzzleStepSO)EditorGUILayout.ObjectField("PuzzleStepSO", _objectiveData, typeof(PuzzleStepSO), false);
@@ -109,7 +139,7 @@ namespace Editor
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
if (_objectiveData == null && GUILayout.Button("Create New PuzzleStepSO")) if (_objectiveData == null && GUILayout.Button("Create New PuzzleStepSO"))
{ {
_objectiveData = PrefabEditorUtility.CreateScriptableAsset<PuzzleStepSO>(go.name + "_puzzle", _puzzleSoFolderPath); _objectiveData = PrefabEditorUtility.CreateScriptableAsset<PuzzleStepSO>(_selectedGameObject.name + "_puzzle", _puzzleSoFolderPath);
} }
if (_objectiveData != null) if (_objectiveData != null)
{ {
@@ -119,10 +149,9 @@ namespace Editor
} }
if (GUI.changed) if (GUI.changed)
{ {
EditorUtility.SetDirty(go); EditorUtility.SetDirty(_selectedGameObject);
PrefabUtility.RecordPrefabInstancePropertyModifications(go); PrefabUtility.RecordPrefabInstancePropertyModifications(_selectedGameObject);
} }
} }
} }
} }