From 3c71565cc5095fa8fa2719d5dc8ae38599addffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Matkowski?= Date: Tue, 9 Nov 2021 17:24:05 +0100 Subject: [PATCH 1/8] Minor refactor changes --- .../Scripts/Serialization/SerializedType.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Assets/Editor Toolbox/Scripts/Serialization/SerializedType.cs b/Assets/Editor Toolbox/Scripts/Serialization/SerializedType.cs index 204a66c8..2b61f25f 100644 --- a/Assets/Editor Toolbox/Scripts/Serialization/SerializedType.cs +++ b/Assets/Editor Toolbox/Scripts/Serialization/SerializedType.cs @@ -16,6 +16,12 @@ public static string GetClassReference(Type type) } + [SerializeField] + private string classReference; + + private Type type; + + /// /// Initializes a new instance of the class. /// @@ -46,6 +52,11 @@ public SerializedType(Type type) } + public override string ToString() + { + return Type != null ? Type.FullName : $"(None)"; + } + void ISerializationCallbackReceiver.OnAfterDeserialize() { if (!string.IsNullOrEmpty(classReference)) @@ -69,11 +80,6 @@ void ISerializationCallbackReceiver.OnBeforeSerialize() { } - [SerializeField] - private string classReference; - - private Type type; - /// /// Gets or sets type of class reference. /// @@ -101,7 +107,5 @@ public Type Type public static implicit operator Type(SerializedType typeReference) => typeReference.Type; public static implicit operator SerializedType(Type type) => new SerializedType(type); - - public override string ToString() => Type != null ? Type.FullName : $"(None)"; } } \ No newline at end of file From c9dd548244f6cdcb97ae28eed0f8d00c74b6d87e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Matkowski?= Date: Tue, 9 Nov 2021 17:24:44 +0100 Subject: [PATCH 2/8] Add SerializedDateTime and associated drawer --- .../Regular/SerializedDateTimeDrawer.cs | 31 ++++++++++ .../Regular/SerializedDateTimeDrawer.cs.meta | 11 ++++ .../Serialization/SerializedDateTime.cs | 62 +++++++++++++++++++ .../Serialization/SerializedDateTime.cs.meta | 11 ++++ 4 files changed, 115 insertions(+) create mode 100644 Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedDateTimeDrawer.cs create mode 100644 Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedDateTimeDrawer.cs.meta create mode 100644 Assets/Editor Toolbox/Scripts/Serialization/SerializedDateTime.cs create mode 100644 Assets/Editor Toolbox/Scripts/Serialization/SerializedDateTime.cs.meta diff --git a/Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedDateTimeDrawer.cs b/Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedDateTimeDrawer.cs new file mode 100644 index 00000000..c988cc80 --- /dev/null +++ b/Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedDateTimeDrawer.cs @@ -0,0 +1,31 @@ +using System; +using UnityEditor; +using UnityEngine; + +namespace Toolbox.Editor.Drawers +{ + [CustomPropertyDrawer(typeof(SerializedDateTime))] + public class SerializedDateTimeDrawer : PropertyDrawerBase + { + protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label) + { + label = EditorGUI.BeginProperty(position, label, property); + var fieldPosition = EditorGUI.PrefixLabel(position, label); + var ticksProperty = property.FindPropertyRelative("ticks"); + DateTime dateTime = new DateTime(ticksProperty.longValue); + EditorGUI.BeginChangeCheck(); + var dateTimeString = EditorGUI.DelayedTextField(fieldPosition, dateTime.ToString()); + if (EditorGUI.EndChangeCheck()) + { + if (DateTime.TryParse(dateTimeString, out var newDateTime)) + { + ticksProperty.serializedObject.Update(); + ticksProperty.longValue = newDateTime.Ticks; + ticksProperty.serializedObject.ApplyModifiedProperties(); + } + } + + EditorGUI.EndProperty(); + } + } +} \ No newline at end of file diff --git a/Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedDateTimeDrawer.cs.meta b/Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedDateTimeDrawer.cs.meta new file mode 100644 index 00000000..42fdcc66 --- /dev/null +++ b/Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedDateTimeDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 60ef036d3d91c89449984e72c6c31d99 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor Toolbox/Scripts/Serialization/SerializedDateTime.cs b/Assets/Editor Toolbox/Scripts/Serialization/SerializedDateTime.cs new file mode 100644 index 00000000..24bd9f6d --- /dev/null +++ b/Assets/Editor Toolbox/Scripts/Serialization/SerializedDateTime.cs @@ -0,0 +1,62 @@ +using System; + +namespace UnityEngine +{ + /// + /// Reference to a class with support for Unity serialization. + /// + [Serializable] + public sealed class SerializedDateTime : ISerializationCallbackReceiver + { + [SerializeField] + private long ticks; + + private DateTime dateTime; + + + public SerializedDateTime() : this(0) + { } + + public SerializedDateTime(long ticks) : this(new DateTime(ticks)) + { } + + public SerializedDateTime(DateTime dateTime) + { + DateTime = dateTime; + } + + + void ISerializationCallbackReceiver.OnAfterDeserialize() + { + dateTime = new DateTime(ticks); + } + + void ISerializationCallbackReceiver.OnBeforeSerialize() + { } + + + public DateTime DateTime + { + get => dateTime; + set + { + dateTime = value; + ticks = dateTime.Ticks; + } + } + + + public static implicit operator DateTime(SerializedDateTime sdt) + { + return sdt.DateTime; + } + + public static implicit operator SerializedDateTime(DateTime dt) + { + return new SerializedDateTime + { + ticks = dt.Ticks + }; + } + } +} \ No newline at end of file diff --git a/Assets/Editor Toolbox/Scripts/Serialization/SerializedDateTime.cs.meta b/Assets/Editor Toolbox/Scripts/Serialization/SerializedDateTime.cs.meta new file mode 100644 index 00000000..8d244f7e --- /dev/null +++ b/Assets/Editor Toolbox/Scripts/Serialization/SerializedDateTime.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d9de638ab820e984ca7619c5ca591497 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 01ffbe54447919f158c3700604b90e54feeac8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Matkowski?= Date: Thu, 11 Nov 2021 13:53:33 +0100 Subject: [PATCH 3/8] Add DynamicRangeAttribute&Drawer --- .../DynamicRangeAttributeDrawer.cs | 71 +++++++++++++++++++ .../DynamicRangeAttributeDrawer.cs.meta | 11 +++ Assets/Editor Toolbox/EditorSettings.asset | 1 + .../DynamicRangeAttribute.cs | 18 +++++ .../DynamicRangeAttribute.cs.meta | 11 +++ 5 files changed, 112 insertions(+) create mode 100644 Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs create mode 100644 Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs.meta create mode 100644 Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs create mode 100644 Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs.meta diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs new file mode 100644 index 00000000..70aeec65 --- /dev/null +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs @@ -0,0 +1,71 @@ +using System; + +using UnityEditor; +using UnityEngine; + +namespace Toolbox.Editor.Drawers +{ + public class DynamicRangeAttributeDrawer : ToolboxSelfPropertyDrawer + { + protected override void OnGuiSafe(SerializedProperty property, GUIContent label, DynamicRangeAttribute attribute) + { + //TODO: base drawer or utility for min max value convertions + var minValueSource = attribute.MinValueSource; + var maxValueSource = attribute.MaxValueSource; + if (!ValueExtractionHelper.TryGetValue(minValueSource, property, out var minValueCandidate, out _) || + !ValueExtractionHelper.TryGetValue(maxValueSource, property, out var maxValueCandidate, out _)) + { + ToolboxEditorLog.MemberNotFoundWarning(attribute, property, + string.Format("{0} or {1}", minValueSource, maxValueSource)); + base.OnGuiSafe(property, label, attribute); + return; + } + + float minValue; + float maxValue; + try + { + minValue = Convert.ToSingle(minValueCandidate); + maxValue = Convert.ToSingle(maxValueCandidate); + } + catch (Exception e) when (e is InvalidCastException || e is FormatException) + { + ToolboxEditorLog.AttributeUsageWarning(attribute, property, + string.Format("Invalid source types, cannot convert them to {0}", typeof(float))); + base.OnGuiSafe(property, label, attribute); + return; + } + + ToolboxEditorGui.BeginProperty(property, ref label, out var position); + EditorGUI.BeginChangeCheck(); + switch (property.propertyType) + { + case SerializedPropertyType.Integer: + var intValue = EditorGUI.IntSlider(position, label, property.intValue, (int)minValue, (int)maxValue); + if (EditorGUI.EndChangeCheck()) + { + property.intValue = intValue; + } + break; + case SerializedPropertyType.Float: + var floatValue = EditorGUI.Slider(position, label, property.floatValue, minValue, maxValue); + if (EditorGUI.EndChangeCheck()) + { + property.floatValue = floatValue; + } + break; + default: + break; + } + + ToolboxEditorGui.CloseProperty(); + } + + + public override bool IsPropertyValid(SerializedProperty property) + { + return property.propertyType == SerializedPropertyType.Integer || + property.propertyType == SerializedPropertyType.Float; + } + } +} \ No newline at end of file diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs.meta b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs.meta new file mode 100644 index 00000000..2b1065e0 --- /dev/null +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 78f04ac5c49f18748abb09d3f3961dd9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor Toolbox/EditorSettings.asset b/Assets/Editor Toolbox/EditorSettings.asset index 17284136..5f171830 100644 --- a/Assets/Editor Toolbox/EditorSettings.asset +++ b/Assets/Editor Toolbox/EditorSettings.asset @@ -79,6 +79,7 @@ MonoBehaviour: - classReference: Toolbox.Editor.Drawers.ShowWarningIfAttributeDrawer, Toolbox-Editor selfPropertyDrawerHandlers: - classReference: Toolbox.Editor.Drawers.DynamicMinMaxSliderAttributeDrawer, Toolbox-Editor + - classReference: Toolbox.Editor.Drawers.DynamicRangeAttributeDrawer, Toolbox-Editor - classReference: Toolbox.Editor.Drawers.IgnoreParentAttributeDrawer, Toolbox-Editor - classReference: Toolbox.Editor.Drawers.InLineEditorAttributeDrawer, Toolbox-Editor - classReference: Toolbox.Editor.Drawers.RegexValueAttributeDrawer, Toolbox-Editor diff --git a/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs new file mode 100644 index 00000000..5eeb51e7 --- /dev/null +++ b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs @@ -0,0 +1,18 @@ +using System; + +namespace UnityEngine +{ + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] + public class DynamicRangeAttribute : ToolboxSelfPropertyAttribute + { + public DynamicRangeAttribute(string minValueSource, string maxValueSource) + { + MinValueSource = minValueSource; + MaxValueSource = maxValueSource; + } + + public string MinValueSource { get; private set; } + + public string MaxValueSource { get; private set; } + } +} \ No newline at end of file diff --git a/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs.meta b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs.meta new file mode 100644 index 00000000..32e09c45 --- /dev/null +++ b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 480c312e580c3624898041e3b9e0f7f0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From fe3a2ae064148a13b354b05a180d394bfa4e0b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Matkowski?= Date: Sun, 14 Nov 2021 01:28:07 +0100 Subject: [PATCH 4/8] Fix reload check for non-disposed Editors --- Assets/Editor Toolbox/Editor/Utilities/InspectorUtility.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Assets/Editor Toolbox/Editor/Utilities/InspectorUtility.cs b/Assets/Editor Toolbox/Editor/Utilities/InspectorUtility.cs index 45547bb2..b0906b40 100644 --- a/Assets/Editor Toolbox/Editor/Utilities/InspectorUtility.cs +++ b/Assets/Editor Toolbox/Editor/Utilities/InspectorUtility.cs @@ -27,13 +27,13 @@ static InspectorUtility() } + private static int lastCachedEditorId; private static Editor lastCachedEditor; private static readonly Stack cachedEditors = new Stack(); private static void OnBeginEditor(Editor editor) { - lastCachedEditor = editor; cachedEditors.Push(editor); } @@ -53,8 +53,10 @@ private static void OnCloseEditor(Editor editor) private static void CheckReloads(Editor editor) { //NOTE: it means that last Editor was null or disposed, anyway we probably want to reload drawers-related cache - if (lastCachedEditor == null) + if (lastCachedEditor == null || lastCachedEditorId != lastCachedEditor.GetInstanceID()) { + lastCachedEditor = editor; + lastCachedEditorId = editor.GetInstanceID(); OnEditorReload?.Invoke(); } } From 528966cc1463df4608677700a1224c4ea59ea612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Matkowski?= Date: Tue, 30 Nov 2021 19:48:39 +0100 Subject: [PATCH 5/8] Minor refactor changes; update examples --- .../DynamicMinMaxBaseDrawer.cs | 43 +++++++++++++++++++ .../DynamicMinMaxBaseDrawer.cs.meta | 11 +++++ .../DynamicMinMaxSliderAttributeDrawer.cs | 30 +------------ .../DynamicRangeAttributeDrawer.cs | 35 ++------------- .../DynamicMinMaxBaseAttribute.cs | 15 +++++++ .../DynamicMinMaxBaseAttribute.cs.meta | 11 +++++ .../DynamicMinMaxSliderAttribute.cs | 13 ++---- .../DynamicRangeAttribute.cs | 13 ++---- Assets/Examples/Scenes/SampleScene.unity | 7 +++ Assets/Examples/ScriptableObjects.meta | 8 ---- .../New Sample Scriptable Object.asset | 16 ------- .../New Sample Scriptable Object.asset.meta | 8 ---- Assets/Examples/Scripts/SampleBehaviour1.cs | 4 ++ Assets/Examples/Scripts/SampleBehaviour2.cs | 9 ++++ 14 files changed, 111 insertions(+), 112 deletions(-) create mode 100644 Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxBaseDrawer.cs create mode 100644 Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxBaseDrawer.cs.meta create mode 100644 Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxBaseAttribute.cs create mode 100644 Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxBaseAttribute.cs.meta delete mode 100644 Assets/Examples/ScriptableObjects.meta delete mode 100644 Assets/Examples/ScriptableObjects/New Sample Scriptable Object.asset delete mode 100644 Assets/Examples/ScriptableObjects/New Sample Scriptable Object.asset.meta diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxBaseDrawer.cs b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxBaseDrawer.cs new file mode 100644 index 00000000..5af00420 --- /dev/null +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxBaseDrawer.cs @@ -0,0 +1,43 @@ +using System; + +using UnityEditor; +using UnityEngine; + +namespace Toolbox.Editor.Drawers +{ + public abstract class DynamicMinMaxBaseDrawer : ToolboxSelfPropertyDrawer where T : DynamicMinMaxBaseAttribute + { + protected override void OnGuiSafe(SerializedProperty property, GUIContent label, T attribute) + { + var minValueSource = attribute.MinValueSource; + var maxValueSource = attribute.MaxValueSource; + if (!ValueExtractionHelper.TryGetValue(minValueSource, property, out var minValueCandidate, out _) || + !ValueExtractionHelper.TryGetValue(maxValueSource, property, out var maxValueCandidate, out _)) + { + ToolboxEditorLog.MemberNotFoundWarning(attribute, property, + string.Format("{0} or {1}", minValueSource, maxValueSource)); + base.OnGuiSafe(property, label, attribute); + return; + } + + float minValue; + float maxValue; + try + { + minValue = Convert.ToSingle(minValueCandidate); + maxValue = Convert.ToSingle(maxValueCandidate); + } + catch (Exception e) when (e is InvalidCastException || e is FormatException) + { + ToolboxEditorLog.AttributeUsageWarning(attribute, property, + string.Format("Invalid source types, cannot convert them to {0}", typeof(float))); + base.OnGuiSafe(property, label, attribute); + return; + } + + OnGuiSafe(property, label, minValue, maxValue); + } + + protected abstract void OnGuiSafe(SerializedProperty property, GUIContent label, float minValue, float maxValue); + } +} \ No newline at end of file diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxBaseDrawer.cs.meta b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxBaseDrawer.cs.meta new file mode 100644 index 00000000..b9743cb7 --- /dev/null +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxBaseDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bf58cb4edac6acb468624e6f9f5ed9c1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxSliderAttributeDrawer.cs b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxSliderAttributeDrawer.cs index 6f85773b..bd10fbe7 100644 --- a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxSliderAttributeDrawer.cs +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxSliderAttributeDrawer.cs @@ -1,38 +1,12 @@ -using System; - using UnityEditor; using UnityEngine; namespace Toolbox.Editor.Drawers { - public class DynamicMinMaxSliderAttributeDrawer : ToolboxSelfPropertyDrawer + public class DynamicMinMaxSliderAttributeDrawer : DynamicMinMaxBaseDrawer { - protected override void OnGuiSafe(SerializedProperty property, GUIContent label, DynamicMinMaxSliderAttribute attribute) + protected override void OnGuiSafe(SerializedProperty property, GUIContent label, float minValue, float maxValue) { - var minValueSource = attribute.MinValueSource; - var maxValueSource = attribute.MaxValueSource; - if (!ValueExtractionHelper.TryGetValue(minValueSource, property, out var minValueCandidate, out _) || - !ValueExtractionHelper.TryGetValue(maxValueSource, property, out var maxValueCandidate, out _)) - { - ToolboxEditorLog.MemberNotFoundWarning(attribute, property, - string.Format("{0} or {1}", minValueSource, maxValueSource)); - base.OnGuiSafe(property, label, attribute); - return; - } - - var maxValue = 0.0f; - var minValue = 0.0f; - try - { - minValue = Convert.ToSingle(minValueCandidate); - maxValue = Convert.ToSingle(maxValueCandidate); - } - catch (Exception e) when (e is InvalidCastException || e is FormatException) - { - ToolboxEditorLog.AttributeUsageWarning(attribute, property, - string.Format("Invalid source types, cannot convert them to {0}", typeof(float))); - } - var xValue = property.vector2Value.x; var yValue = property.vector2Value.y; ToolboxEditorGui.BeginProperty(property, ref label, out var position); diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs index 70aeec65..a5367190 100644 --- a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicRangeAttributeDrawer.cs @@ -1,41 +1,12 @@ -using System; - -using UnityEditor; +using UnityEditor; using UnityEngine; namespace Toolbox.Editor.Drawers { - public class DynamicRangeAttributeDrawer : ToolboxSelfPropertyDrawer + public class DynamicRangeAttributeDrawer : DynamicMinMaxBaseDrawer { - protected override void OnGuiSafe(SerializedProperty property, GUIContent label, DynamicRangeAttribute attribute) + protected override void OnGuiSafe(SerializedProperty property, GUIContent label, float minValue, float maxValue) { - //TODO: base drawer or utility for min max value convertions - var minValueSource = attribute.MinValueSource; - var maxValueSource = attribute.MaxValueSource; - if (!ValueExtractionHelper.TryGetValue(minValueSource, property, out var minValueCandidate, out _) || - !ValueExtractionHelper.TryGetValue(maxValueSource, property, out var maxValueCandidate, out _)) - { - ToolboxEditorLog.MemberNotFoundWarning(attribute, property, - string.Format("{0} or {1}", minValueSource, maxValueSource)); - base.OnGuiSafe(property, label, attribute); - return; - } - - float minValue; - float maxValue; - try - { - minValue = Convert.ToSingle(minValueCandidate); - maxValue = Convert.ToSingle(maxValueCandidate); - } - catch (Exception e) when (e is InvalidCastException || e is FormatException) - { - ToolboxEditorLog.AttributeUsageWarning(attribute, property, - string.Format("Invalid source types, cannot convert them to {0}", typeof(float))); - base.OnGuiSafe(property, label, attribute); - return; - } - ToolboxEditorGui.BeginProperty(property, ref label, out var position); EditorGUI.BeginChangeCheck(); switch (property.propertyType) diff --git a/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxBaseAttribute.cs b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxBaseAttribute.cs new file mode 100644 index 00000000..75f8cb00 --- /dev/null +++ b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxBaseAttribute.cs @@ -0,0 +1,15 @@ +namespace UnityEngine +{ + public abstract class DynamicMinMaxBaseAttribute : ToolboxSelfPropertyAttribute + { + protected DynamicMinMaxBaseAttribute(string minValueSource, string maxValueSource) + { + MinValueSource = minValueSource; + MaxValueSource = maxValueSource; + } + + public string MinValueSource { get; protected set; } + + public string MaxValueSource { get; protected set; } + } +} \ No newline at end of file diff --git a/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxBaseAttribute.cs.meta b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxBaseAttribute.cs.meta new file mode 100644 index 00000000..ab1da8a1 --- /dev/null +++ b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxBaseAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1331e01e8d9360b4fa7e468e65b94aef +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxSliderAttribute.cs b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxSliderAttribute.cs index e457dfb1..5dd7a597 100644 --- a/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxSliderAttribute.cs +++ b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxSliderAttribute.cs @@ -3,16 +3,9 @@ namespace UnityEngine { [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] - public class DynamicMinMaxSliderAttribute : ToolboxSelfPropertyAttribute + public class DynamicMinMaxSliderAttribute : DynamicMinMaxBaseAttribute { - public DynamicMinMaxSliderAttribute(string minValueSource, string maxValueSource) - { - MinValueSource = minValueSource; - MaxValueSource = maxValueSource; - } - - public string MinValueSource { get; private set; } - - public string MaxValueSource { get; private set; } + public DynamicMinMaxSliderAttribute(string minValueSource, string maxValueSource) : base(minValueSource, maxValueSource) + { } } } \ No newline at end of file diff --git a/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs index 5eeb51e7..ff707d44 100644 --- a/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs +++ b/Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicRangeAttribute.cs @@ -3,16 +3,9 @@ namespace UnityEngine { [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] - public class DynamicRangeAttribute : ToolboxSelfPropertyAttribute + public class DynamicRangeAttribute : DynamicMinMaxBaseAttribute { - public DynamicRangeAttribute(string minValueSource, string maxValueSource) - { - MinValueSource = minValueSource; - MaxValueSource = maxValueSource; - } - - public string MinValueSource { get; private set; } - - public string MaxValueSource { get; private set; } + public DynamicRangeAttribute(string minValueSource, string maxValueSource) : base(minValueSource, maxValueSource) + { } } } \ No newline at end of file diff --git a/Assets/Examples/Scenes/SampleScene.unity b/Assets/Examples/Scenes/SampleScene.unity index 5b8246eb..156339f7 100644 --- a/Assets/Examples/Scenes/SampleScene.unity +++ b/Assets/Examples/Scenes/SampleScene.unity @@ -508,6 +508,11 @@ MonoBehaviour: - 0 - 0 - 0 + var39: 0 + a1: -1 + b1: 5.5 + var40: 1.33 + var41: {x: 0, y: 1.9792935} --- !u!4 &661896459 Transform: m_ObjectHideFlags: 2 @@ -707,6 +712,8 @@ MonoBehaviour: buildIndex: 0 bigNumber: 12345678 currency: 20.41 + dateTime: + ticks: 631466100000000000 --- !u!4 &959025299 Transform: m_ObjectHideFlags: 2 diff --git a/Assets/Examples/ScriptableObjects.meta b/Assets/Examples/ScriptableObjects.meta deleted file mode 100644 index 52278b0e..00000000 --- a/Assets/Examples/ScriptableObjects.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 32e5d91ef36bc4f4e99287968703a090 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Examples/ScriptableObjects/New Sample Scriptable Object.asset b/Assets/Examples/ScriptableObjects/New Sample Scriptable Object.asset deleted file mode 100644 index 24c2c0e8..00000000 --- a/Assets/Examples/ScriptableObjects/New Sample Scriptable Object.asset +++ /dev/null @@ -1,16 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 5d1935b7b3931904e91b76d6e7e5e0f4, type: 3} - m_Name: New Sample Scriptable Object - m_EditorClassIdentifier: - var1: 0 - var2: 0 diff --git a/Assets/Examples/ScriptableObjects/New Sample Scriptable Object.asset.meta b/Assets/Examples/ScriptableObjects/New Sample Scriptable Object.asset.meta deleted file mode 100644 index 585e74e5..00000000 --- a/Assets/Examples/ScriptableObjects/New Sample Scriptable Object.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: ace1a268e169a634ba1e83ff652ea5fb -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 11400000 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Examples/Scripts/SampleBehaviour1.cs b/Assets/Examples/Scripts/SampleBehaviour1.cs index d4e87756..4f57a1bb 100644 --- a/Assets/Examples/Scripts/SampleBehaviour1.cs +++ b/Assets/Examples/Scripts/SampleBehaviour1.cs @@ -203,6 +203,10 @@ public class SampleClass2 [FormattedNumber("c")] public float currency; + [Label("28", skinStyle: SkinStyle.Box)] + + public SerializedDateTime dateTime; + #if UNITY_EDITOR private void OnValidate() { diff --git a/Assets/Examples/Scripts/SampleBehaviour2.cs b/Assets/Examples/Scripts/SampleBehaviour2.cs index b9d593d4..7fadaf3e 100644 --- a/Assets/Examples/Scripts/SampleBehaviour2.cs +++ b/Assets/Examples/Scripts/SampleBehaviour2.cs @@ -202,4 +202,13 @@ public int GetValue() public int var39; public string MessageSource => string.Format("Dynamic Message Source. {0} = {1}", nameof(var39), var39); + + [Label("17", skinStyle: SkinStyle.Box)] + + public float a1 = -1; + public float b1 = 5.5f; + [DynamicRange(nameof(a1), nameof(b1))] + public float var40; + [DynamicMinMaxSlider(nameof(a1), nameof(b1))] + public Vector2 var41; } \ No newline at end of file From 6a82b599499121f67162909a85844c19661ff747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Matkowski?= Date: Tue, 30 Nov 2021 19:53:56 +0100 Subject: [PATCH 6/8] Ensure the "Edit/Expand" button is always enabled in the InLineEditorAttributeDrawer --- .../PropertySelfDrawers/InLineEditorAttributeDrawer.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/InLineEditorAttributeDrawer.cs b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/InLineEditorAttributeDrawer.cs index d95f7082..20b24d40 100644 --- a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/InLineEditorAttributeDrawer.cs +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/InLineEditorAttributeDrawer.cs @@ -52,7 +52,10 @@ private Editor GetTargetsEditor(SerializedProperty property, InLineEditorAttribu private bool GetInspectorToggle(SerializedProperty property) { - return GUILayout.Toggle(property.isExpanded, Style.foldoutContent, Style.foldoutStyle, Style.foldoutOptions); + using (new DisabledScope(true)) + { + return GUILayout.Toggle(property.isExpanded, Style.foldoutContent, Style.foldoutStyle, Style.foldoutOptions); + } } private void DrawEditor(Editor editor, InLineEditorAttribute attribute) From a6b8d0175e835ffbe7fae7736757353267078ef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Matkowski?= Date: Tue, 30 Nov 2021 20:20:55 +0100 Subject: [PATCH 7/8] Fix relative path creation when manualy creating settings file --- Assets/Editor Toolbox/Editor/ToolboxManager.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Assets/Editor Toolbox/Editor/ToolboxManager.cs b/Assets/Editor Toolbox/Editor/ToolboxManager.cs index 253f25b2..0b70176e 100644 --- a/Assets/Editor Toolbox/Editor/ToolboxManager.cs +++ b/Assets/Editor Toolbox/Editor/ToolboxManager.cs @@ -1,4 +1,5 @@ -using UnityEditor; +using System.IO; +using UnityEditor; using UnityEngine; namespace Toolbox.Editor @@ -36,8 +37,6 @@ private static void ManageProjectCore(IToolboxProjectSettings settings) return; } - var validateData = !IsInitialized; - //enable/disable the core GUI function ToolboxEditorProject.IsOverlayAllowed = settings.UseToolboxProject; @@ -160,7 +159,7 @@ void ReintializeProvider() //rebuild the settings provider right after initialization provider.OnDeactivate(); - provider.OnActivate("", null); + provider.OnActivate(string.Empty, null); } provider.guiHandler = (searchContext) => @@ -168,7 +167,7 @@ void ReintializeProvider() if (globalSettingsEditor == null || globalSettingsEditor.serializedObject.targetObject == null) { EditorGUILayout.Space(); - EditorGUILayout.LabelField("Cannot find " + settingsType + " file located in this Project"); + EditorGUILayout.LabelField(string.Format("Cannot find {0} file located in this Project", settingsType)); EditorGUILayout.Space(); if (GUILayout.Button("Create a new settings file")) @@ -182,9 +181,8 @@ void ReintializeProvider() return; } - var relativePath = locationPath - .Substring(locationPath - .IndexOf("Assets/")) + "/" + settingsType + ".asset"; + var assetName = string.Format("{0}.asset", settingsType); + var relativePath = Path.Combine(FileUtil.GetProjectRelativePath(locationPath), assetName); AssetDatabase.CreateAsset(settingsInstance, relativePath); AssetDatabase.SaveAssets(); From 88f002f1a16b7420f1c945bc4cd7da9d0333e921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Matkowski?= Date: Tue, 30 Nov 2021 20:28:48 +0100 Subject: [PATCH 8/8] Update documentation --- Assets/Editor Toolbox/CHANGELOG.md | 11 +++++++++++ Assets/Editor Toolbox/README.md | 4 ++++ Assets/Editor Toolbox/package.json | 2 +- README.md | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Assets/Editor Toolbox/CHANGELOG.md b/Assets/Editor Toolbox/CHANGELOG.md index 0c07bd31..cdbb1971 100644 --- a/Assets/Editor Toolbox/CHANGELOG.md +++ b/Assets/Editor Toolbox/CHANGELOG.md @@ -1,3 +1,14 @@ +## 0.10.6 [30.11.2021] + +### Added: +- DynamicRangeSliderAttribute +- SerializedDateTime + +### Changed: +- Fix disposing inlined Editors in ScriptableObjects-based Inspectors +- Fix disabling "Edit" button when inlined Editor is disabled +- Fix relative path when ToolboxEditorSettings is created manually + ## 0.10.4 [27.10.2021] ### Added: diff --git a/Assets/Editor Toolbox/README.md b/Assets/Editor Toolbox/README.md index d256ba47..6093297a 100644 --- a/Assets/Editor Toolbox/README.md +++ b/Assets/Editor Toolbox/README.md @@ -602,6 +602,10 @@ public void Usage() #endif ``` +#### SerializedDateTime + +Allows to serialize DateTime. + ![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/dictionary1.png) ![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/dictionary2.png) diff --git a/Assets/Editor Toolbox/package.json b/Assets/Editor Toolbox/package.json index 89dd8f40..d44641a4 100644 --- a/Assets/Editor Toolbox/package.json +++ b/Assets/Editor Toolbox/package.json @@ -1,7 +1,7 @@ { "name": "com.arimger.editor-toolbox", "displayName": "Editor Toolbox", - "version": "0.10.4", + "version": "0.10.6", "unity": "2018.1", "description": "Tools, custom attributes, drawers, hierarchy overlay, and other extensions for the Unity Editor.", "keywords": [ diff --git a/README.md b/README.md index d256ba47..6093297a 100644 --- a/README.md +++ b/README.md @@ -602,6 +602,10 @@ public void Usage() #endif ``` +#### SerializedDateTime + +Allows to serialize DateTime. + ![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/dictionary1.png) ![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/dictionary2.png)