Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.meta
*.meta
21 changes: 21 additions & 0 deletions Demos/MultiConditionDemo.cs
Original file line number Diff line number Diff line change
@@ -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<string> str = new List<string>("awd", "dghj");
}
51 changes: 40 additions & 11 deletions DrawIfAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
using UnityEngine;
using System;
using System.Collections.Generic;

/// <summary>
/// Draws the field/property ONLY if the copared property compared by the comparison type with the value of comparedValue returns true.
/// </summary>
[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<string> propertyNameList;
public List<object> comparedValueList;


public string ComparedPropertyName { get; private set; }
public List<string> 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;


/// <summary>
Expand All @@ -32,12 +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;
// ComparisonType = comparisonType;
// DisablingType = disablingType;
// multible = true;
// }
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<string> comparedPropertyNameList = new List<string>();
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;
}
}
41 changes: 29 additions & 12 deletions Editor/DrawIfPropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using UnityEditor;
using UnityEngine;
using Utilities;
using Exceptions;
using DrawIfExtension;

[CustomPropertyDrawer(typeof(DrawIfAttribute))]
public class DrawIfPropertyDrawer : PropertyDrawer
Expand All @@ -27,17 +25,36 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
drawIf = attribute as DrawIfAttribute;

bool conditionMet = false;
//if (!drawIf.multible) {
//}
//else {
if (!drawIf.multible) {
// Get the compared field
comparedField = property.serializedObject.FindProperty(drawIf.ComparedPropertyName);
// Get the value of the compared field.
object comparedFieldValue = comparedField.GetValue<object>();
// Is the condition met? Should the field be drawn?
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<object>();
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<object>();
state = DrawIfExtension.DrawIfExtension.DrawIfConditionCheck(property, comparedField, comparedFieldValue, drawIf.ComparedValue, drawIf.ComparisonType);
if (!state) { break; }
}
}

//}
// Get the compared field
comparedField = property.serializedObject.FindProperty(drawIf.ComparedPropertyName);
// Get the value of the compared field.
object comparedFieldValue = comparedField.GetValue<object>();
// Is the condition met? Should the field be drawn?
conditionMet = DrawIfExtension.DrawIfExtension.DrawIfConditionCheck(property, comparedField, comparedFieldValue, drawIf.ComparedValue, drawIf.ComparisonType);
conditionMet = state;
}



// The height of the property should be defaulted to the default height.
Expand Down