add scriptable object examples
add editor window example add attribute + drawer example
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9af0d2fa264f4cc6bdd8c09bf2b52e57
|
||||
timeCreated: 1749829795
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a2a86f13a318489c95e31e662cf4ccf8
|
||||
timeCreated: 1749829930
|
||||
@@ -0,0 +1,35 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Attributes.Editor
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(RequireAttribute))]
|
||||
public class RequireAttributeDrawer : PropertyDrawer
|
||||
{
|
||||
private readonly string HELPBOX_CONTENT = "Property has to be set!";
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
bool isNull = !property.objectReferenceValue;
|
||||
|
||||
if (isNull) position.height = EditorGUIUtility.singleLineHeight;
|
||||
|
||||
EditorGUI.PropertyField(position, property, label);
|
||||
|
||||
if (isNull)
|
||||
{
|
||||
position.y += position.height + EditorGUIUtility.standardVerticalSpacing;
|
||||
position.height = EditorStyles.helpBox.CalcHeight(new GUIContent(HELPBOX_CONTENT),
|
||||
EditorGUIUtility.currentViewWidth) + 10.0f;
|
||||
EditorGUI.HelpBox(position, HELPBOX_CONTENT, MessageType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (property.objectReferenceValue) return EditorGUIUtility.singleLineHeight;
|
||||
return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing + EditorStyles.helpBox.CalcHeight(new GUIContent(HELPBOX_CONTENT), EditorGUIUtility.currentViewWidth) + 10.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e966da3a6c3e4487ba279991b988a582
|
||||
timeCreated: 1749829943
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Attributes
|
||||
{
|
||||
public class RequireAttribute : PropertyAttribute
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4b36ca3d21740d4ab5a0e19d2622dfb
|
||||
timeCreated: 1749829828
|
||||
@@ -0,0 +1,13 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
[CreateAssetMenu(fileName = "CharacterAttributes", menuName = "Data/Character Attributes", order = 0)]
|
||||
public class CharacterAttributes : ScriptableObject
|
||||
{
|
||||
public string Name;
|
||||
public int Health;
|
||||
public int Strength;
|
||||
public float StrengthMultiplier;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b61abb9f0ac44c59b3b2fd2042e9ace6
|
||||
timeCreated: 1749826252
|
||||
@@ -0,0 +1,28 @@
|
||||
using Core;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
[CustomPropertyDrawer(typeof(CharacterAttributes))]
|
||||
public class CharacterAttributesDrawer : PropertyDrawer
|
||||
{
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
position = EditorGUI.PrefixLabel(position, label);
|
||||
|
||||
position.width *= 0.75f;
|
||||
EditorGUI.PropertyField(position, property, GUIContent.none);
|
||||
|
||||
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: 484559cbb76043e19a4cacc8b753bebf
|
||||
timeCreated: 1749829051
|
||||
@@ -0,0 +1,23 @@
|
||||
using Core;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
[CustomEditor(typeof(CharacterAttributes))]
|
||||
public class CharacterAttributesEditor : Editor
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
base.OnInspectorGUI();
|
||||
|
||||
if (GUILayout.Button("Open Character Attributes Editor"))
|
||||
{
|
||||
CharacterAttributesWindow window = EditorWindow.GetWindow<CharacterAttributesWindow>();
|
||||
window.CharacterAttributesToEdit = target as CharacterAttributes;
|
||||
window.Show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 272269d36d454d6cbd79b57adc9f2692
|
||||
timeCreated: 1749828536
|
||||
@@ -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: fd75fdc6380d499a861c0efdba916acc
|
||||
timeCreated: 1749823882
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cbb6edf533104c27a71d6e1c16addd3e
|
||||
timeCreated: 1749825253
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c31ec81529dc4f3fb828508c80171ff6
|
||||
timeCreated: 1749825893
|
||||
@@ -0,0 +1,22 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Events
|
||||
{
|
||||
[CustomEditor(typeof(EventBroker))]
|
||||
public class EventBrokerEditor : 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: 12b0037e847a41a1b18c0ef2643e47a8
|
||||
timeCreated: 1749825911
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Events
|
||||
{
|
||||
[CreateAssetMenu(fileName = "EventBroker", menuName = "Events/Event Broker", order = 0)]
|
||||
public class EventBroker : ScriptableObject
|
||||
{
|
||||
public event Action PlayerDied;
|
||||
|
||||
public void RaisePlayerDied() => PlayerDied?.Invoke();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c52cbf4a02ec44f3a6b94e25b84593d9
|
||||
timeCreated: 1749825311
|
||||
@@ -1,34 +1,47 @@
|
||||
using System;
|
||||
using Attributes;
|
||||
using Data;
|
||||
using Events;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Events")]
|
||||
[SerializeField] private EventBroker eventBroker;
|
||||
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float speed;
|
||||
[SerializeField] private float rotationSpeed;
|
||||
|
||||
[Header("Values")]
|
||||
[SerializeField] private float maxHealth = 100.0f;
|
||||
// [SerializeField] private float maxHealth = 100.0f;
|
||||
[SerializeField] private CharacterAttributes characterAttributes;
|
||||
[SerializeField, Require] private FloatValue maxHealth;
|
||||
[SerializeField, Require] private FloatValue health;
|
||||
|
||||
[Header("References")]
|
||||
[SerializeField] private Image healthBarFillingImage;
|
||||
// [Header("References")]
|
||||
// [SerializeField] private Image healthBarFillingImage;
|
||||
|
||||
[Header("Abilities")] [SerializeField] private Ability ability;
|
||||
[SerializeField] private Ability[] abilities;
|
||||
|
||||
public float _currentHealth; // stupid public value for testing
|
||||
// public float _currentHealth; // stupid public value for testing
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_currentHealth = maxHealth;
|
||||
// _currentHealth = maxHealth;
|
||||
|
||||
// set health
|
||||
health.Value = maxHealth.Value;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
healthBarFillingImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,
|
||||
Mathf.Clamp01(_currentHealth / maxHealth) * 200.0f);
|
||||
// healthBarFillingImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,
|
||||
// Mathf.Clamp01(_currentHealth / maxHealth) * 200.0f);
|
||||
|
||||
if (health.Value <= 0.0f) eventBroker.RaisePlayerDied();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 91606e4cfd524defb445a3e187076a33
|
||||
timeCreated: 1749824195
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
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 health;
|
||||
|
||||
[Header("References")]
|
||||
[SerializeField] private Image fillingImage;
|
||||
[SerializeField] private TextMeshProUGUI deadText;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
fillingImage.type = Image.Type.Filled;
|
||||
fillingImage.fillMethod = Image.FillMethod.Horizontal;
|
||||
deadText.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
eventBroker.PlayerDied += OnPlayerDied;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
eventBroker.PlayerDied -= OnPlayerDied;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
fillingImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,
|
||||
Mathf.Clamp01(health.Value / maxHealth.Value) * 200.0f);
|
||||
}
|
||||
|
||||
private void OnPlayerDied()
|
||||
{
|
||||
fillingImage.fillAmount = 0.0f;
|
||||
fillingImage.color = Color.red;
|
||||
deadText.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a98df6e31c94df993b83fcfbf7111e6
|
||||
timeCreated: 1749824203
|
||||
Reference in New Issue
Block a user