diff --git a/Assets/Data/Items/SoundBirdPuzzleItems/Headband.asset b/Assets/Data/Items/SoundBirdPuzzleItems/Headband.asset index 65f46116..6e53aa1f 100644 --- a/Assets/Data/Items/SoundBirdPuzzleItems/Headband.asset +++ b/Assets/Data/Items/SoundBirdPuzzleItems/Headband.asset @@ -13,5 +13,5 @@ MonoBehaviour: m_Name: Headband m_EditorClassIdentifier: itemName: Headband - description: Item dropped by Uncle Kunt + description: Item dropped by Uncle Kunt! mapSprite: {fileID: 743298078511449570, guid: 6f463983177b1404ca39fa222f03a3b2, type: 3} diff --git a/Assets/Editor/ItemPrefabEditor.cs b/Assets/Editor/ItemPrefabEditor.cs index 614b7609..f67d6f4e 100644 --- a/Assets/Editor/ItemPrefabEditor.cs +++ b/Assets/Editor/ItemPrefabEditor.cs @@ -1,76 +1,106 @@ using UnityEditor; using UnityEngine; -using System.IO; namespace Editor { - [CustomEditor(typeof(GameObject))] - public class ItemPrefabEditor : UnityEditor.Editor + public class ItemPrefabEditorWindow : EditorWindow { + private GameObject _selectedGameObject; + private Interactable _interactable; private PickupItemData _pickupData; private PuzzleStepSO _objectiveData; private UnityEditor.Editor _soEditor; private string _pickupSoFolderPath = "Assets/Data/Items"; private string _puzzleSoFolderPath = "Assets/Data/Puzzles"; - public override void OnInspectorGUI() + [MenuItem("Tools/Item Prefab Editor")] + public static void ShowWindow() { - GameObject go = (GameObject)target; - bool isItem = go.GetComponent() != null; - if (!isItem) + var window = GetWindow("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) { - DrawDefaultInspector(); + _selectedGameObject = Selection.activeGameObject; + _interactable = _selectedGameObject.GetComponent(); + } + else if (Selection.activeObject is GameObject go) + { + _selectedGameObject = go; + _interactable = go.GetComponent(); + } + + if (_selectedGameObject == null || _interactable == null) + { + EditorGUILayout.HelpBox("Select a GameObject or prefab with an Interactable component to edit.", MessageType.Info); return; } - EditorGUILayout.LabelField("Item Prefab Editor", EditorStyles.boldLabel); + + EditorGUILayout.LabelField("Editing: ", _selectedGameObject.name, EditorStyles.boldLabel); + EditorGUILayout.Space(); // Pickup - bool hasPickup = go.GetComponent() != null; + bool hasPickup = _selectedGameObject.GetComponent() != null; bool addPickup = EditorGUILayout.Toggle("Pickup", hasPickup); if (addPickup && !hasPickup) { - PrefabEditorUtility.AddOrGetComponent(go); + PrefabEditorUtility.AddOrGetComponent(_selectedGameObject); } else if (!addPickup && hasPickup) { - PrefabEditorUtility.RemoveComponent(go); + PrefabEditorUtility.RemoveComponent(_selectedGameObject); } // CombineWithBehavior - bool hasCombine = go.GetComponent() != null; + bool hasCombine = _selectedGameObject.GetComponent() != null; bool addCombine = EditorGUILayout.Toggle("CombineWithBehavior", hasCombine); if (addCombine && !hasCombine) { - PrefabEditorUtility.AddOrGetComponent(go); + PrefabEditorUtility.AddOrGetComponent(_selectedGameObject); } else if (!addCombine && hasCombine) { - PrefabEditorUtility.RemoveComponent(go); + PrefabEditorUtility.RemoveComponent(_selectedGameObject); } // SlotItemBehavior - bool hasSlot = go.GetComponent() != null; + bool hasSlot = _selectedGameObject.GetComponent() != null; bool addSlot = EditorGUILayout.Toggle("SlotItemBehavior", hasSlot); if (addSlot && !hasSlot) { - PrefabEditorUtility.AddOrGetComponent(go); + PrefabEditorUtility.AddOrGetComponent(_selectedGameObject); } else if (!addSlot && hasSlot) { - PrefabEditorUtility.RemoveComponent(go); + PrefabEditorUtility.RemoveComponent(_selectedGameObject); } // ObjectiveStepBehaviour - bool hasObjective = go.GetComponent() != null; + bool hasObjective = _selectedGameObject.GetComponent() != null; bool addObjective = EditorGUILayout.Toggle("ObjectiveStepBehaviour", hasObjective); if (addObjective && !hasObjective) { - PrefabEditorUtility.AddOrGetComponent(go); + PrefabEditorUtility.AddOrGetComponent(_selectedGameObject); } else if (!addObjective && hasObjective) { - PrefabEditorUtility.RemoveComponent(go); + PrefabEditorUtility.RemoveComponent(_selectedGameObject); } // Pickup Data if (addPickup) { - var pickup = go.GetComponent(); + var pickup = _selectedGameObject.GetComponent(); _pickupData = pickup.itemData; EditorGUILayout.LabelField("Pickup Data:", EditorStyles.boldLabel); _pickupData = (PickupItemData)EditorGUILayout.ObjectField("PickupItemData", _pickupData, typeof(PickupItemData), false); @@ -84,7 +114,7 @@ namespace Editor EditorGUILayout.EndHorizontal(); if (_pickupData == null && GUILayout.Button("Create New PickupItemData")) { - _pickupData = PrefabEditorUtility.CreateScriptableAsset(go.name + "_pickup", _pickupSoFolderPath); + _pickupData = PrefabEditorUtility.CreateScriptableAsset(_selectedGameObject.name + "_pickup", _pickupSoFolderPath); } if (_pickupData != null) { @@ -95,7 +125,7 @@ namespace Editor // Objective Data if (addObjective) { - var obj = go.GetComponent(); + var obj = _selectedGameObject.GetComponent(); _objectiveData = obj.stepData; EditorGUILayout.LabelField("Objective Data:", EditorStyles.boldLabel); _objectiveData = (PuzzleStepSO)EditorGUILayout.ObjectField("PuzzleStepSO", _objectiveData, typeof(PuzzleStepSO), false); @@ -109,7 +139,7 @@ namespace Editor EditorGUILayout.EndHorizontal(); if (_objectiveData == null && GUILayout.Button("Create New PuzzleStepSO")) { - _objectiveData = PrefabEditorUtility.CreateScriptableAsset(go.name + "_puzzle", _puzzleSoFolderPath); + _objectiveData = PrefabEditorUtility.CreateScriptableAsset(_selectedGameObject.name + "_puzzle", _puzzleSoFolderPath); } if (_objectiveData != null) { @@ -119,10 +149,9 @@ namespace Editor } if (GUI.changed) { - EditorUtility.SetDirty(go); - PrefabUtility.RecordPrefabInstancePropertyModifications(go); + EditorUtility.SetDirty(_selectedGameObject); + PrefabUtility.RecordPrefabInstancePropertyModifications(_selectedGameObject); } } } } -