Added lots of examples for editor customization
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user