diff --git a/Assets/Plugins/Stats/Editor/StatsEditorHelper.cs b/Assets/Plugins/Stats/Editor/StatsEditorHelper.cs index 91bbfc0..8742d78 100644 --- a/Assets/Plugins/Stats/Editor/StatsEditorHelper.cs +++ b/Assets/Plugins/Stats/Editor/StatsEditorHelper.cs @@ -7,9 +7,12 @@ namespace Stats.Editor { public static class StatsEditorHelper { - public static string GetStatItemHeader (StatItem statItem, string label) + private const BindingFlags Flags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy; + + public static string GetStatItemHeader (StatItem statItem, SerializedProperty property) { - return $"{label}: {statItem.Base}"; + var itemName = GetItemName(property); + return statItem.StatType == null ? itemName : $"{itemName}: {statItem.Base}"; } public static string GetRuntimeStatHeader(IRuntimeStat runtimeStat) @@ -25,25 +28,25 @@ public static string GetRuntimeStatHeader(IRuntimeStat runtimeStat) return $"{name}: {value - modifiersValue} {modifiersText}"; } - public static string GetAttributeItemHeader(AttributeItem attributeItem, string label) + public static string GetAttributeItemHeader(AttributeItem attributeItem, SerializedProperty property) { - float startPercent = attributeItem.StartPercent; + var startPercent = attributeItem.StartPercent; + var itemName = GetItemName(property); - return $"{label}: {Math.Round(startPercent * 100, 1)}%"; + return attributeItem.AttributeType == null ? itemName : $"{itemName}: {Math.Round(startPercent * 100, 1)}%"; } public static string GetRuntimeAttributeHeader(IRuntimeAttribute runtimeAttribute) { - string name = runtimeAttribute.AttributeType.name; - float value = runtimeAttribute.Value; - float maxValue = runtimeAttribute.MaxValue; + var name = runtimeAttribute.AttributeType.name; + var value = runtimeAttribute.Value; + var maxValue = runtimeAttribute.MaxValue; return $"{name}: {value} / {maxValue}"; } public static T GetValue(SerializedProperty property) { - BindingFlags bindingFlags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy; object obj = property.serializedObject.targetObject; string[] path = property.propertyPath.Split('.'); @@ -60,7 +63,7 @@ public static T GetValue(SerializedProperty property) FieldInfo field = null; while (field == null) { - field = type.GetField(path[i], bindingFlags); + field = type.GetField(path[i], Flags); if (field == null) { @@ -75,24 +78,35 @@ public static T GetValue(SerializedProperty property) return (T)obj; } - public static string GetLabel(SerializedProperty property) + private static string GetItemName(SerializedProperty property) { if (property.name == "data" && property.depth > 0 && property.displayName.Contains("Element")) { - return GetTypeName(property); + return GetItemTypeName(property); } return property.displayName; } - private static string GetTypeName(SerializedProperty property) + private static string GetItemTypeName(SerializedProperty property) { + var label = ""; + if (typeof(T) == typeof(AttributeItem)) { - return GetValue(property).AttributeType.name; + AttributeType type = GetValue(property).AttributeType; + + label = type == null ? "Select Attribute" : type.name; + } + + if (typeof(T) == typeof(StatItem)) + { + StatType type = GetValue(property).StatType; + + label = type == null ? "Select Stat" : type.name; } - return typeof(T) == typeof(StatItem) ? GetValue(property).StatType.name : ""; + return label; } } } \ No newline at end of file diff --git a/Assets/Plugins/Stats/Editor/TraitsEditor.cs b/Assets/Plugins/Stats/Editor/TraitsEditor.cs index 178b13b..667dbc2 100644 --- a/Assets/Plugins/Stats/Editor/TraitsEditor.cs +++ b/Assets/Plugins/Stats/Editor/TraitsEditor.cs @@ -1,6 +1,6 @@ -using System.Reflection; +using System; using UnityEditor; -using UnityEditor.UIElements; +using UnityEngine; using UnityEngine.UIElements; namespace Stats.Editor diff --git a/Assets/Plugins/Stats/Editor/VisualElements/AttributeItemElement.cs b/Assets/Plugins/Stats/Editor/VisualElements/AttributeItemElement.cs index 2c66932..5a2b70d 100644 --- a/Assets/Plugins/Stats/Editor/VisualElements/AttributeItemElement.cs +++ b/Assets/Plugins/Stats/Editor/VisualElements/AttributeItemElement.cs @@ -138,10 +138,7 @@ private void RepaintPercent(bool isShow) private void UpdateHeaderText() { - AttributeType attributeValue = _oldAttributeType; - _foldout.text = !attributeValue ? "Select Attribute" - : StatsEditorHelper.GetAttributeItemHeader(_attributeItem, - StatsEditorHelper.GetLabel(_property)); + _foldout.text = StatsEditorHelper.GetAttributeItemHeader(_attributeItem, _property); } } } \ No newline at end of file diff --git a/Assets/Plugins/Stats/Editor/VisualElements/StatItemElement.cs b/Assets/Plugins/Stats/Editor/VisualElements/StatItemElement.cs index 6e53f50..24474b2 100644 --- a/Assets/Plugins/Stats/Editor/VisualElements/StatItemElement.cs +++ b/Assets/Plugins/Stats/Editor/VisualElements/StatItemElement.cs @@ -187,9 +187,7 @@ private void ResetValue() private void UpdateHeaderText() { - _foldout.text = _stat.objectReferenceValue == null ? - "Select Stat" : - StatsEditorHelper.GetStatItemHeader(_statItem, StatsEditorHelper.GetLabel(_property)); + _foldout.text = StatsEditorHelper.GetStatItemHeader(_statItem, _property); } } } \ No newline at end of file diff --git a/Assets/Plugins/Stats/Runtime/Core/AttributeItem.cs b/Assets/Plugins/Stats/Runtime/Core/AttributeItem.cs index e2b5f37..e2d2ca2 100644 --- a/Assets/Plugins/Stats/Runtime/Core/AttributeItem.cs +++ b/Assets/Plugins/Stats/Runtime/Core/AttributeItem.cs @@ -11,7 +11,7 @@ public sealed class AttributeItem [SerializeField] private bool _changeStartPercent; [SerializeField] [Range(0f, 1f)] private float _startPercent = 1f; - public AttributeType AttributeType => _attributeType; + public AttributeType AttributeType => _attributeType? _attributeType : null; public float MinValue => _attributeType ? _attributeType.MinValue : 0f; public StatType MaxValueType => _attributeType ? _attributeType.MaxValueType : null; diff --git a/Assets/Plugins/Stats/Runtime/Core/StatItem.cs b/Assets/Plugins/Stats/Runtime/Core/StatItem.cs index 9841add..c4b1ff4 100644 --- a/Assets/Plugins/Stats/Runtime/Core/StatItem.cs +++ b/Assets/Plugins/Stats/Runtime/Core/StatItem.cs @@ -14,7 +14,7 @@ public sealed class StatItem [SerializeField] private bool _changeFormula; [SerializeField] private StatFormula _formula; - public StatType StatType => _stat.Type; + public StatType StatType => _stat? _stat.Type : null; public float Base {