I created a ConditionalShow attribute, Which basically checks if the other variable in class is true or not, If it is true then draw’s property otherwise doesn’t draw it. But some time the other field also needs another condition, I want to check if condition for the field which is now condition for another field is true or not? if it is false then this field should not be shown.
Here is my code:
// Custom Attribute Class
public class ConditionalShowAttribute : PropertyAttribute
{
public string ConditionalField = "";
public ConditionalShowAttribute(string ConditionalField)
{
this.ConditionalField = ConditionalField;
}
}
// Custom Property Drawer Class
public class ConditionalShowPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect Position, SerializedProperty Property, GUIContent Label)
{
ConditionalShowAttribute EditCondition = (ConditionalShowAttribute)attribute;
string PropertyPath = Property.propertyPath;
string ConditionShowPath = PropertyPath.Replace(Property.name, ConditionalShow.ConditionalField);
SerializedProperty SourceProperty = Property.serializedObject.FindProperty(ConditionalShowPath);
bool WasEnabled = GUI.enabled;
/* Here I need to check if SourceProperty has a ConditionalShowAttribute or not.
* If SourceProperty Has a ConditionalShow attribute and it is false
* then this property should not shown. */
if (SourcePropertyValue != null && SourceProperty.boolValue)
{
EditorGUI.PropertyField(Position, Property, Label, true);
}
GUI.enabled = WasEnabled;
}
}
// Sample Class
public class Sample : Monobehavior
{
public bool condition = false;
// if value for condition is equal to true then will be shown.
[ConditionalShow("condition")]
public bool variable1 = true;
[ConditionalShow("variable1")]
public string variable2 = "SomeValue";
}