Added lots of examples for editor customization
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3584f915c56743079c64ff8e5a01d40c
|
||||
timeCreated: 1782226871
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d54ab03c1c0c44d791dc4924077e6721
|
||||
timeCreated: 1782227128
|
||||
@@ -0,0 +1,35 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Attributes.Editor
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(RequiredAttribute))]
|
||||
public class RequiredAttributeDrawer : PropertyDrawer
|
||||
{
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
bool isNull = property.objectReferenceValue == null;
|
||||
|
||||
if (isNull) position.height = EditorGUIUtility.singleLineHeight;
|
||||
|
||||
// draw property itself
|
||||
EditorGUI.PropertyField(position, property, label);
|
||||
|
||||
// draw error if property is null
|
||||
if (!isNull) return;
|
||||
|
||||
position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
position.height += 10.0f;
|
||||
EditorGUI.HelpBox(position, "Property has to be set!", MessageType.Error);
|
||||
}
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (property.objectReferenceValue == null)
|
||||
{
|
||||
return EditorGUIUtility.singleLineHeight * 2 + EditorGUIUtility.standardVerticalSpacing + 10.0f;
|
||||
}
|
||||
return EditorGUIUtility.singleLineHeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6e50e9fdc60c4539822a5697fb9ca70f
|
||||
timeCreated: 1782227142
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class RequiredAttribute : PropertyAttribute
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 24b0639b8cb74a8199a77a007236bce1
|
||||
timeCreated: 1782226882
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 998a8db57ee2457188c2c9885f46354b
|
||||
timeCreated: 1782224148
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
[Serializable]
|
||||
public class Ability
|
||||
{
|
||||
public string Name;
|
||||
public float Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aa5b1ad84d0e43608052c6a8eaf2168f
|
||||
timeCreated: 1782228009
|
||||
@@ -0,0 +1,13 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
[CreateAssetMenu(fileName = "CharacterAttributes", menuName = "Data/Character Attribute", order = 0)]
|
||||
public class CharacterAttributes : ScriptableObject
|
||||
{
|
||||
public string Name;
|
||||
public int Health;
|
||||
public int Strength;
|
||||
public int Dexterity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4f21d365f6b74c6eae7bc9c0431c2695
|
||||
timeCreated: 1782229191
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ab00c23a3d5e424285496daaa587c857
|
||||
timeCreated: 1782228299
|
||||
@@ -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
|
||||
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
[CreateAssetMenu(fileName = "FloatValue", menuName = "Data/Values/Float", order = 0)]
|
||||
public class FloatValue : ScriptableObject
|
||||
{
|
||||
public float Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e94c4e9b6648485ca46c67afaab715ac
|
||||
timeCreated: 1782224168
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 04cd260b7b7b4433beec3903ba437de4
|
||||
timeCreated: 1782225082
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94ea6f71efde4bd4ae34bde539c2c68c
|
||||
timeCreated: 1782225959
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Events.Editor
|
||||
{
|
||||
[CustomEditor(typeof(EventBroker))]
|
||||
public class EventBrokerEditor : UnityEditor.Editor
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
base.OnInspectorGUI();
|
||||
|
||||
if (!EditorApplication.isPlaying) return;
|
||||
|
||||
if (GUILayout.Button("Player Died"))
|
||||
{
|
||||
(target as EventBroker)?.RaisePlayerDied();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea036d9930184985a5a989eeb914e5fe
|
||||
timeCreated: 1782226034
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Events
|
||||
{
|
||||
[CreateAssetMenu(fileName = "EventBroker", menuName = "Events/Event Broker")]
|
||||
public class EventBroker : ScriptableObject
|
||||
{
|
||||
public event Action PlayerDied;
|
||||
|
||||
public void RaisePlayerDied() => PlayerDied?.Invoke();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c8e9a4f877141c4b1f3e248fcef8803
|
||||
timeCreated: 1782225093
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3597a6e4b534b9baf57981a436382ba
|
||||
timeCreated: 1782224267
|
||||
@@ -0,0 +1,49 @@
|
||||
using Attributes;
|
||||
using Data;
|
||||
using Events;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Player
|
||||
{
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Events")]
|
||||
[SerializeField, Required] private EventBroker eventBroker;
|
||||
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float speed = 5f;
|
||||
[SerializeField] private float rotationSpeed = 60f;
|
||||
|
||||
[Header("Values")]
|
||||
// [SerializeField] private float maxHealth = 100f;
|
||||
[SerializeField, Required] private FloatValue maxHealth;
|
||||
[SerializeField, Required] private FloatValue currentHealth;
|
||||
[SerializeField] private CharacterAttributes characterAttributes;
|
||||
|
||||
// [Header("References")]
|
||||
// [SerializeField] private Image healthBarFillingImage;
|
||||
|
||||
[Header("Abilities")]
|
||||
[SerializeField] private Ability ability;
|
||||
[SerializeField] private Ability[] abilities;
|
||||
|
||||
|
||||
// private float _currentHealth;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// _currentHealth = maxHealth;
|
||||
|
||||
// set health
|
||||
currentHealth.Value = maxHealth.Value;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// healthBarFillingImage.fillAmount = _currentHealth / maxHealth;
|
||||
|
||||
if (currentHealth.Value <= 0.0f) eventBroker.RaisePlayerDied();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float speed = 5f;
|
||||
[SerializeField] private float rotationSpeed = 60f;
|
||||
|
||||
[Header("Values")]
|
||||
[SerializeField] private float maxHealth = 100f;
|
||||
|
||||
[Header("References")]
|
||||
[SerializeField] private Image healthBarFillingImage;
|
||||
|
||||
private float _currentHealth;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_currentHealth = maxHealth;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
healthBarFillingImage.fillAmount = _currentHealth / maxHealth;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c22ee53ed204a81a5a83315733d1656
|
||||
timeCreated: 1782224277
|
||||
@@ -0,0 +1,52 @@
|
||||
using Data;
|
||||
using Events;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class HealthBar : MonoBehaviour
|
||||
{
|
||||
[Header("Events")]
|
||||
[SerializeField] private EventBroker eventBroker;
|
||||
|
||||
[Header("Values")]
|
||||
[SerializeField] private FloatValue maxHealth;
|
||||
[SerializeField] private FloatValue currentHealth;
|
||||
|
||||
[Header("References")]
|
||||
[SerializeField] private Image fillingImage;
|
||||
[SerializeField] private TextMeshProUGUI deadMessageText;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
fillingImage.type = Image.Type.Filled;
|
||||
fillingImage.fillMethod = Image.FillMethod.Horizontal;
|
||||
fillingImage.fillOrigin = 0;
|
||||
deadMessageText.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
eventBroker.PlayerDied += OnPlayerDied;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
eventBroker.PlayerDied -= OnPlayerDied;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
fillingImage.fillAmount = currentHealth.Value / maxHealth.Value;
|
||||
}
|
||||
|
||||
private void OnPlayerDied()
|
||||
{
|
||||
fillingImage.fillAmount = 0.0f;
|
||||
currentHealth.Value = 0.0f;
|
||||
deadMessageText.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d9616fe18084f1bb99a58e30eea20e1
|
||||
timeCreated: 1782224286
|
||||
Reference in New Issue
Block a user