[Util] Add the possiblity to also edit items via the custom editor window
This commit is contained in:
128
Assets/Editor/ItemPrefabEditor.cs
Normal file
128
Assets/Editor/ItemPrefabEditor.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
|
||||
namespace Editor
|
||||
{
|
||||
[CustomEditor(typeof(GameObject))]
|
||||
public class ItemPrefabEditor : UnityEditor.Editor
|
||||
{
|
||||
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()
|
||||
{
|
||||
GameObject go = (GameObject)target;
|
||||
bool isItem = go.GetComponent<Interactable>() != null;
|
||||
if (!isItem)
|
||||
{
|
||||
DrawDefaultInspector();
|
||||
return;
|
||||
}
|
||||
EditorGUILayout.LabelField("Item Prefab Editor", EditorStyles.boldLabel);
|
||||
// Pickup
|
||||
bool hasPickup = go.GetComponent<Pickup>() != null;
|
||||
bool addPickup = EditorGUILayout.Toggle("Pickup", hasPickup);
|
||||
if (addPickup && !hasPickup)
|
||||
{
|
||||
PrefabEditorUtility.AddOrGetComponent<Pickup>(go);
|
||||
}
|
||||
else if (!addPickup && hasPickup)
|
||||
{
|
||||
PrefabEditorUtility.RemoveComponent<Pickup>(go);
|
||||
}
|
||||
// CombineWithBehavior
|
||||
bool hasCombine = go.GetComponent<CombineWithBehavior>() != null;
|
||||
bool addCombine = EditorGUILayout.Toggle("CombineWithBehavior", hasCombine);
|
||||
if (addCombine && !hasCombine)
|
||||
{
|
||||
PrefabEditorUtility.AddOrGetComponent<CombineWithBehavior>(go);
|
||||
}
|
||||
else if (!addCombine && hasCombine)
|
||||
{
|
||||
PrefabEditorUtility.RemoveComponent<CombineWithBehavior>(go);
|
||||
}
|
||||
// SlotItemBehavior
|
||||
bool hasSlot = go.GetComponent<SlotItemBehavior>() != null;
|
||||
bool addSlot = EditorGUILayout.Toggle("SlotItemBehavior", hasSlot);
|
||||
if (addSlot && !hasSlot)
|
||||
{
|
||||
PrefabEditorUtility.AddOrGetComponent<SlotItemBehavior>(go);
|
||||
}
|
||||
else if (!addSlot && hasSlot)
|
||||
{
|
||||
PrefabEditorUtility.RemoveComponent<SlotItemBehavior>(go);
|
||||
}
|
||||
// ObjectiveStepBehaviour
|
||||
bool hasObjective = go.GetComponent<ObjectiveStepBehaviour>() != null;
|
||||
bool addObjective = EditorGUILayout.Toggle("ObjectiveStepBehaviour", hasObjective);
|
||||
if (addObjective && !hasObjective)
|
||||
{
|
||||
PrefabEditorUtility.AddOrGetComponent<ObjectiveStepBehaviour>(go);
|
||||
}
|
||||
else if (!addObjective && hasObjective)
|
||||
{
|
||||
PrefabEditorUtility.RemoveComponent<ObjectiveStepBehaviour>(go);
|
||||
}
|
||||
// Pickup Data
|
||||
if (addPickup)
|
||||
{
|
||||
var pickup = go.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>(go.name + "_pickup", _pickupSoFolderPath);
|
||||
}
|
||||
if (_pickupData != null)
|
||||
{
|
||||
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _pickupData);
|
||||
pickup.itemData = _pickupData;
|
||||
}
|
||||
}
|
||||
// Objective Data
|
||||
if (addObjective)
|
||||
{
|
||||
var obj = go.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>(go.name + "_puzzle", _puzzleSoFolderPath);
|
||||
}
|
||||
if (_objectiveData != null)
|
||||
{
|
||||
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _objectiveData);
|
||||
obj.stepData = _objectiveData;
|
||||
}
|
||||
}
|
||||
if (GUI.changed)
|
||||
{
|
||||
EditorUtility.SetDirty(go);
|
||||
PrefabUtility.RecordPrefabInstancePropertyModifications(go);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user