From 00370a2eb8e4195da46a6d93d67ccd72400c616f Mon Sep 17 00:00:00 2001 From: CTN Date: Sat, 17 Jul 2021 13:26:25 +0200 Subject: [PATCH 1/3] Multi-Conditions Feature This is where I add the option to have multiple conditions and values. --- DrawIfAttribute.cs | 15 +++++++-------- Editor/DrawIfPropertyDrawer.cs | 23 ++++++++++++----------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/DrawIfAttribute.cs b/DrawIfAttribute.cs index 16d637e..8aab0eb 100644 --- a/DrawIfAttribute.cs +++ b/DrawIfAttribute.cs @@ -32,12 +32,11 @@ public DrawIfAttribute(string comparedPropertyName, object comparedValue, Compar DisablingType = disablingType; multible = false; } - //public DrawIfAttribute(string[] comparedPropertyName, object[] comparedValue, ComparisonType comparisonType = ComparisonType.Equals, DisablingType disablingType = DisablingType.DontDraw) - // { - // ComparedPropertyNames = comparedPropertyName; - // ComparedValues = comparedValue; - // ComparisonType = comparisonType; - // DisablingType = disablingType; - // multible = true; - // } + public DrawIfAttribute(string[] comparedPropertyName, object[] comparedValue, ComparisonType comparisonType = ComparisonType.Equals, DisablingType disablingType = DisablingType.DontDraw) { + ComparedPropertyNames = comparedPropertyName; + ComparedValues = comparedValue; + ComparisonType = comparisonType; + DisablingType = disablingType; + multible = true; + } } \ No newline at end of file diff --git a/Editor/DrawIfPropertyDrawer.cs b/Editor/DrawIfPropertyDrawer.cs index 49aabba..27a9a78 100644 --- a/Editor/DrawIfPropertyDrawer.cs +++ b/Editor/DrawIfPropertyDrawer.cs @@ -27,17 +27,18 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten drawIf = attribute as DrawIfAttribute; bool conditionMet = false; - //if (!drawIf.multible) { - //} - //else { - - //} - // Get the compared field - comparedField = property.serializedObject.FindProperty(drawIf.ComparedPropertyName); - // Get the value of the compared field. - object comparedFieldValue = comparedField.GetValue(); - // Is the condition met? Should the field be drawn? - conditionMet = DrawIfExtension.DrawIfExtension.DrawIfConditionCheck(property, comparedField, comparedFieldValue, drawIf.ComparedValue, drawIf.ComparisonType); + if (!drawIf.multible) { + // Get the compared field + comparedField = property.serializedObject.FindProperty(drawIf.ComparedPropertyName); + // Get the value of the compared field. + object comparedFieldValue = comparedField.GetValue(); + // Is the condition met? Should the field be drawn? + conditionMet = DrawIfExtension.DrawIfExtension.DrawIfConditionCheck(property, comparedField, comparedFieldValue, drawIf.ComparedValue, drawIf.ComparisonType); + } + else { + + } + // The height of the property should be defaulted to the default height. From faeb91cb1556c0d53658b4c4545c590ec2d48aed Mon Sep 17 00:00:00 2001 From: CTN Date: Sat, 17 Jul 2021 13:29:07 +0200 Subject: [PATCH 2/3] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6f4242a..cec0bf4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .meta +*.meta From 965bedfb5faf59a189da1003ae52e4e4c3f004a0 Mon Sep 17 00:00:00 2001 From: CTN Date: Sat, 17 Jul 2021 14:39:26 +0200 Subject: [PATCH 3/3] testing the possibilities --- Demos/MultiConditionDemo.cs | 21 +++++++++++++++++ DrawIfAttribute.cs | 42 +++++++++++++++++++++++++++++----- Editor/DrawIfPropertyDrawer.cs | 22 +++++++++++++++--- 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 Demos/MultiConditionDemo.cs diff --git a/Demos/MultiConditionDemo.cs b/Demos/MultiConditionDemo.cs new file mode 100644 index 0000000..fa47090 --- /dev/null +++ b/Demos/MultiConditionDemo.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using UnityEngine; + +public class MultiConditionDemo : MonoBehaviour { + [Header("List")] + public bool x; + public bool y; + public bool z; + + [DrawIf("x", true)] public float xF = 0.23f; + //[DrawIf("x", "y", true, ComparisonType.Equals)] public Vector2 xyV2 = new Vector2(0.842f, 2.45f); + [DrawIf("z", true)] public Vector3 xyzV3 = new Vector3(3.47f, 93.33f, 5.245f); + + [Header("Array")] + public bool x2; + public bool y2; + + [DrawIf("x2,y2", true, false)] public Vector2 xyV2Array = new Vector2(0.842f, 2.45f); + + //public List str = new List("awd", "dghj"); +} diff --git a/DrawIfAttribute.cs b/DrawIfAttribute.cs index 8aab0eb..52df7d1 100644 --- a/DrawIfAttribute.cs +++ b/DrawIfAttribute.cs @@ -1,5 +1,6 @@ using UnityEngine; using System; +using System.Collections.Generic; /// /// Draws the field/property ONLY if the copared property compared by the comparison type with the value of comparedValue returns true. @@ -7,14 +8,20 @@ [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)] public class DrawIfAttribute : PropertyAttribute { - public string ComparedPropertyName { get; private set; } - public string[] ComparedPropertyNames { get; private set; } + public List propertyNameList; + public List comparedValueList; + + + public string ComparedPropertyName { get; private set; } + public List ComparedPropertyNameList { get; private set; } + public string[] ComparedPropertyNameArray { get; private set; } public object ComparedValue { get; private set; } - public object[] ComparedValues { get; private set; } + public object[] ComparedValueArray { get; private set; } public ComparisonType ComparisonType { get; private set; } public DisablingType DisablingType { get; private set; } public bool multible; + public bool isList; /// @@ -32,11 +39,34 @@ public DrawIfAttribute(string comparedPropertyName, object comparedValue, Compar DisablingType = disablingType; multible = false; } - public DrawIfAttribute(string[] comparedPropertyName, object[] comparedValue, ComparisonType comparisonType = ComparisonType.Equals, DisablingType disablingType = DisablingType.DontDraw) { - ComparedPropertyNames = comparedPropertyName; - ComparedValues = comparedValue; + public DrawIfAttribute(string comparedPropertyName1, string comparedPropertyName2, object comparedValue, ComparisonType comparisonType = ComparisonType.Equals, DisablingType disablingType = DisablingType.DontDraw, bool multi = true) { + + //string rawPropertyNames = comparedPropertyNames.Replace(" && ", ","); + //string[] propertyNames = rawPropertyNames.Split(','); + + List comparedPropertyNameList = new List(); + comparedPropertyNameList.Add(comparedPropertyName1); + comparedPropertyNameList.Add(comparedPropertyName2); + //foreach (string name in propertyNames) { + // comparedPropertyNameList.Add(name); + //} + + ComparedPropertyNameList = comparedPropertyNameList; + ComparedValue = comparedValue; + ComparisonType = comparisonType; + DisablingType = disablingType; + multible = true; + isList = true; + } + public DrawIfAttribute(string comparedPropertyName, object comparedValue, bool isList, ComparisonType comparisonType = ComparisonType.Equals, DisablingType disablingType = DisablingType.DontDraw, bool multi = true) { + + string[] comparedPropertyNameArray = comparedPropertyName.Split(','); + + ComparedPropertyNameArray = comparedPropertyNameArray; + ComparedValue = comparedValue; ComparisonType = comparisonType; DisablingType = disablingType; multible = true; + isList = false; } } \ No newline at end of file diff --git a/Editor/DrawIfPropertyDrawer.cs b/Editor/DrawIfPropertyDrawer.cs index 27a9a78..0a3e9f0 100644 --- a/Editor/DrawIfPropertyDrawer.cs +++ b/Editor/DrawIfPropertyDrawer.cs @@ -1,8 +1,6 @@ using UnityEditor; using UnityEngine; using Utilities; -using Exceptions; -using DrawIfExtension; [CustomPropertyDrawer(typeof(DrawIfAttribute))] public class DrawIfPropertyDrawer : PropertyDrawer @@ -36,7 +34,25 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten conditionMet = DrawIfExtension.DrawIfExtension.DrawIfConditionCheck(property, comparedField, comparedFieldValue, drawIf.ComparedValue, drawIf.ComparisonType); } else { - + bool state = false; + if (drawIf.isList) { + foreach (string field in drawIf.ComparedPropertyNameList) { + comparedField = property.serializedObject.FindProperty(field); + object comparedFieldValue = comparedField.GetValue(); + state = DrawIfExtension.DrawIfExtension.DrawIfConditionCheck(property, comparedField, comparedFieldValue, drawIf.ComparedValue, drawIf.ComparisonType); + if (!state) { break; } + } + } + else { + foreach (string field in drawIf.ComparedPropertyNameArray) { + comparedField = property.serializedObject.FindProperty(field); + object comparedFieldValue = comparedField.GetValue(); + state = DrawIfExtension.DrawIfExtension.DrawIfConditionCheck(property, comparedField, comparedFieldValue, drawIf.ComparedValue, drawIf.ComparisonType); + if (!state) { break; } + } + } + + conditionMet = state; }