add scriptable object examples

add editor window example
add attribute + drawer example
This commit is contained in:
2025-06-13 18:07:18 +02:00
parent 0ae837c57b
commit a6dbfb224f
52 changed files with 1460 additions and 533 deletions
@@ -0,0 +1,96 @@
using Data;
using UnityEditor;
using UnityEditor.VersionControl;
using UnityEngine;
namespace Core
{
public class CharacterAttributesWindow : EditorWindow
{
[MenuItem("Tools/Character Attributes #%^c")]
private static void ShowWindow()
{
var window = GetWindow<CharacterAttributesWindow>();
window.titleContent = new GUIContent("Character Attributes");
window.Show();
}
public CharacterAttributes CharacterAttributesToEdit;
private void OnGUI()
{
// EditorGUILayout.BeginHorizontal();
// EditorGUILayout.LabelField("Label", GUILayout.Width(100));
// EditorGUILayout.LabelField("Next Label", GUILayout.ExpandWidth(true));
//
// EditorGUILayout.BeginVertical();
//
// EditorGUILayout.LabelField("Another Label");
// EditorGUILayout.LabelField("Another Label");
// EditorGUILayout.LabelField("Another Label");
// EditorGUILayout.LabelField("Another Label");
//
// EditorGUILayout.EndVertical();
//
// EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();
// show all project-wide character attribute assets
string[] characterAttributesGUIDs = AssetDatabase.FindAssets("t:CharacterAttributes");
foreach (string characterAttributeGUID in characterAttributesGUIDs)
{
string path = AssetDatabase.GUIDToAssetPath(characterAttributeGUID);
CharacterAttributes characterAttributes = AssetDatabase.LoadAssetAtPath<CharacterAttributes>(path);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(characterAttributes.name);
// EditorGUILayout.LabelField($"Health: {characterAttributes.Health}");
// EditorGUILayout.LabelField($"Strength: {characterAttributes.Strength}");
// EditorGUILayout.LabelField($"Strength Multiplier: {characterAttributes.StrengthMultiplier}");
if (GUILayout.Button("Edit"))
{
CharacterAttributesToEdit = characterAttributes;
}
EditorGUILayout.EndHorizontal();
}
if (CharacterAttributesToEdit)
{
EditorGUILayout.Separator();
EditorGUILayout.BeginVertical();
SerializedObject serializedObject = new SerializedObject(CharacterAttributesToEdit);
serializedObject.Update();
EditorGUILayout.PropertyField(serializedObject.FindProperty("Health"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("Strength"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("StrengthMultiplier"));
serializedObject.ApplyModifiedProperties();
EditorGUILayout.EndVertical();
}
EditorGUILayout.Separator();
if (GUILayout.Button("Create"))
{
CharacterAttributes newCharacterAttributes = CreateInstance<CharacterAttributes>();
string path = EditorUtility.SaveFilePanelInProject("Save Character Attributes", "Character Attributes", "asset", "");
AssetDatabase.CreateAsset(newCharacterAttributes, path);
AssetDatabase.SaveAssets();
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 61b92e412196482db3a322c1e66a6c9f
timeCreated: 1749826501
@@ -1,3 +1,4 @@
using System;
using UnityEditor;
namespace Core
@@ -5,15 +6,28 @@ namespace Core
[CustomEditor(typeof(PlayerController))]
public class PlayerControllerEditor : Editor
{
private SerializedProperty _eventBrokerProperty;
private void OnEnable()
{
_eventBrokerProperty = serializedObject.FindProperty("eventBroker");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
// serializedObject.Update();
serializedObject.Update();
//
// // update your serialized properties
//
// serializedObject.ApplyModifiedProperties();
// warning info text if the event broker is not set
if (!_eventBrokerProperty.objectReferenceValue)
{
EditorGUILayout.HelpBox("Event Broker Property has to be set!", MessageType.Warning);
}
}
}
}