[Util] Add the possiblity to also edit items via the custom editor window

This commit is contained in:
Michal Pikulski
2025-09-10 14:45:25 +02:00
parent ba56f1ec9c
commit 9579a8ef57
5 changed files with 214 additions and 73 deletions

View File

@@ -43,18 +43,7 @@ namespace Editor
EditorGUILayout.SelectableLabel(_saveFolderPath, EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
if (GUILayout.Button("Select...", GUILayout.Width(80)))
{
string selected = EditorUtility.OpenFolderPanel("Select Save Folder", Path.Combine(Application.dataPath, "Prefabs/Items"), "");
if (!string.IsNullOrEmpty(selected))
{
if (selected.Replace("\\", "/").Contains(Application.dataPath.Replace("\\", "/")))
{
_saveFolderPath = "Assets" + selected.Replace("\\", "/").Substring(Application.dataPath.Replace("\\", "/").Length);
}
else
{
EditorUtility.DisplayDialog("Invalid Folder", "Please select a folder inside the Assets directory.", "OK");
}
}
_saveFolderPath = PrefabEditorUtility.SelectFolder(_saveFolderPath, "Prefabs/Items");
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
@@ -75,27 +64,16 @@ namespace Editor
EditorGUILayout.SelectableLabel(_pickupSoFolderPath, EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
if (GUILayout.Button("Select...", GUILayout.Width(80)))
{
string selected = EditorUtility.OpenFolderPanel("Select Pickup Data Folder", Path.Combine(Application.dataPath, "Data/Items"), "");
if (!string.IsNullOrEmpty(selected))
{
if (selected.Replace("\\", "/").Contains(Application.dataPath.Replace("\\", "/")))
{
_pickupSoFolderPath = "Assets" + selected.Replace("\\", "/").Substring(Application.dataPath.Replace("\\", "/").Length);
}
else
{
EditorUtility.DisplayDialog("Invalid Folder", "Please select a folder inside the Assets directory.", "OK");
}
}
_pickupSoFolderPath = PrefabEditorUtility.SelectFolder(_pickupSoFolderPath, "Data/Items");
}
EditorGUILayout.EndHorizontal();
if (_pickupData == null && GUILayout.Button("Create New PickupItemData"))
{
_pickupData = CreateScriptableAsset<PickupItemData>(_prefabName + "_pickupSO", _pickupSoFolderPath);
_pickupData = PrefabEditorUtility.CreateScriptableAsset<PickupItemData>(_prefabName + "_pickup", _pickupSoFolderPath);
}
if (_pickupData != null)
{
DrawScriptableObjectEditor(_pickupData);
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _pickupData);
}
}
if (_addObjective)
@@ -108,27 +86,16 @@ namespace Editor
EditorGUILayout.SelectableLabel(_puzzleSoFolderPath, EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight));
if (GUILayout.Button("Select...", GUILayout.Width(80)))
{
string selected = EditorUtility.OpenFolderPanel("Select Puzzle Data Folder", Path.Combine(Application.dataPath, "Data/Puzzles"), "");
if (!string.IsNullOrEmpty(selected))
{
if (selected.Replace("\\", "/").Contains(Application.dataPath.Replace("\\", "/")))
{
_puzzleSoFolderPath = "Assets" + selected.Replace("\\", "/").Substring(Application.dataPath.Replace("\\", "/").Length);
}
else
{
EditorUtility.DisplayDialog("Invalid Folder", "Please select a folder inside the Assets directory.", "OK");
}
}
_puzzleSoFolderPath = PrefabEditorUtility.SelectFolder(_puzzleSoFolderPath, "Data/Puzzles");
}
EditorGUILayout.EndHorizontal();
if (_objectiveData == null && GUILayout.Button("Create New PuzzleStepSO"))
{
_objectiveData = CreateScriptableAsset<PuzzleStepSO>(_prefabName + "_puzzleSO", _puzzleSoFolderPath);
_objectiveData = PrefabEditorUtility.CreateScriptableAsset<PuzzleStepSO>(_prefabName + "_puzzle", _puzzleSoFolderPath);
}
if (_objectiveData != null)
{
DrawScriptableObjectEditor(_objectiveData);
PrefabEditorUtility.DrawScriptableObjectEditor(ref _soEditor, _objectiveData);
}
}
GUILayout.FlexibleSpace();
@@ -173,38 +140,5 @@ namespace Editor
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("Prefab Created", $"Prefab saved to {prefabPath}", "OK");
}
private T CreateScriptableAsset<T>(string assetName, string folderPath) where T : ScriptableObject
{
// Ensure folder exists
if (!AssetDatabase.IsValidFolder(folderPath))
{
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), folderPath));
AssetDatabase.Refresh();
}
string folder = !string.IsNullOrEmpty(folderPath) ? folderPath : "Assets";
string path = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(folder, assetName + ".asset"));
var asset = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(asset, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Selection.activeObject = asset;
return asset;
}
private string SanitizeFileName(string fileName)
{
foreach (char c in Path.GetInvalidFileNameChars())
fileName = fileName.Replace(c, '_');
return fileName;
}
private void DrawScriptableObjectEditor(ScriptableObject so)
{
if (so == null) return;
if (_soEditor == null || _soEditor.target != so)
_soEditor = UnityEditor.Editor.CreateEditor(so);
if (_soEditor != null)
_soEditor.OnInspectorGUI();
}
}
}