Added lots of examples for editor customization

This commit is contained in:
2026-06-23 18:33:11 +02:00
parent bbb7062c5b
commit 0512201ce0
58 changed files with 920 additions and 116 deletions
@@ -0,0 +1,38 @@
using UnityEditor;
using UnityEngine;
namespace Data.Editor
{
[CustomPropertyDrawer(typeof(Ability))]
public class AbilityDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position = EditorGUI.PrefixLabel(position, label);
// everything in one line
float totalWidth = position.width;
position.width = totalWidth * 0.75f;
EditorGUI.PropertyField(position, property.FindPropertyRelative("Name"), GUIContent.none);
position.x += position.width + totalWidth * 0.05f;
position.width = totalWidth * 0.2f;
EditorGUI.PropertyField(position, property.FindPropertyRelative("Value"), GUIContent.none);
// below each other without foldout
// position.height = EditorGUIUtility.singleLineHeight;
// EditorGUI.PropertyField(position, property.FindPropertyRelative("Name"), GUIContent.none);
//
// position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
// EditorGUI.PropertyField(position, property.FindPropertyRelative("Value"), GUIContent.none);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight; // draw everything in one line
// draw properties below each other without foldout
// return EditorGUIUtility.singleLineHeight * 2 + EditorGUIUtility.standardVerticalSpacing;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cc96966d439940cab8ee55f1803af147
timeCreated: 1782228309
@@ -0,0 +1,30 @@
using UnityEditor;
using UnityEngine;
namespace Data.Editor
{
[CustomPropertyDrawer(typeof(CharacterAttributes))]
public class CharacterAttributesDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
bool isNull = property.objectReferenceValue == null;
position = EditorGUI.PrefixLabel(position, label);
if (!isNull) position.width *= 0.75f;
EditorGUI.PropertyField(position, property, GUIContent.none);
if (isNull) return;
position.x += position.width;
position.width *= 0.3333333f;
if (GUI.Button(position, "Open"))
{
CharacterAttributesWindow window = EditorWindow.GetWindow<CharacterAttributesWindow>();
window.CharacterAttributesToEdit = property.objectReferenceValue as CharacterAttributes;
window.Show();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d56ef7d56a2f46f7af43982d7c693996
timeCreated: 1782231785
@@ -0,0 +1,21 @@
using UnityEditor;
using UnityEngine;
namespace Data.Editor
{
[CustomEditor(typeof(CharacterAttributes))]
public class CharacterAttributesEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Open Character Attributes Window"))
{
CharacterAttributesWindow window = (CharacterAttributesWindow)EditorWindow.GetWindow(typeof(CharacterAttributesWindow));
window.CharacterAttributesToEdit = target as CharacterAttributes;
window.Show();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 29b2341a96d04825a2dcc97cdce1f640
timeCreated: 1782232217
@@ -0,0 +1,96 @@
using UnityEditor;
using UnityEditor.VersionControl;
using UnityEngine;
namespace Data.Editor
{
public class CharacterAttributesWindow : EditorWindow
{
public CharacterAttributes CharacterAttributesToEdit;
// for an overview about the shortcuts
// https://docs.unity3d.com/6000.4/Documentation/ScriptReference/MenuItem.html
[MenuItem("Tools/Character Attributes #%&c")]
private static void ShowWindow()
{
CharacterAttributesWindow window = GetWindow<CharacterAttributesWindow>();
window.titleContent = new GUIContent("Character Attributes");
window.Show();
}
private void OnGUI()
{
// EditorGUILayout.BeginHorizontal();
//
// EditorGUILayout.LabelField("Label", EditorStyles.boldLabel, GUILayout.Width(100));
// EditorGUILayout.LabelField("Second Label", GUILayout.ExpandWidth(true));
//
// EditorGUILayout.BeginVertical();
//
// EditorGUILayout.LabelField("Another Label");
// EditorGUILayout.LabelField("Another Label");
// EditorGUILayout.LabelField("Another Label");
// EditorGUILayout.LabelField("Another Label");
// EditorGUILayout.TextField("Textfield");
//
// EditorGUILayout.EndVertical();
//
// EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField("Character Attributes", EditorStyles.boldLabel);
// show all project-wide character attribute assets
GUID[] characterAttributeGUIDs = AssetDatabase.FindAssetGUIDs("t:CharacterAttributes");
foreach (GUID characterAttributeGUID in characterAttributeGUIDs)
{
CharacterAttributes characterAttributes =
AssetDatabase.LoadAssetByGUID<CharacterAttributes>(characterAttributeGUID);
if (characterAttributes == null) continue;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(characterAttributes.Name);
if (GUILayout.Button("Edit")) CharacterAttributesToEdit = characterAttributes;
EditorGUILayout.EndHorizontal();
}
if (CharacterAttributesToEdit != null)
{
EditorGUILayout.Separator();
SerializedObject serializedObject = new SerializedObject(CharacterAttributesToEdit);
// whenever a serialized object is used, update and apply properties
serializedObject.Update();
// here you can change the serialized object
EditorGUILayout.PropertyField(serializedObject.FindProperty("Name"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("Health"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("Strength"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("Dexterity"));
serializedObject.ApplyModifiedProperties();
}
EditorGUILayout.Separator();
if (GUILayout.Button("Create"))
{
CharacterAttributes characterAttributes = CreateInstance<CharacterAttributes>();
string path = EditorUtility.SaveFilePanelInProject("Save Character Attributes", "New Character Attributes", "asset", "Save Character Attributes", "Assets/CharacterAttributes.asset");
if (!string.IsNullOrEmpty(path))
{
CharacterAttributesToEdit = characterAttributes;
AssetDatabase.CreateAsset(characterAttributes, path);
AssetDatabase.SaveAssets(); // always call this to be sure that the new assets is being saved
}
}
EditorGUILayout.EndVertical();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 91c41f34951643a4bfd4a2b3efa4e2d6
timeCreated: 1782229380