Created
March 27, 2018 09:23
-
-
Save sebtoun/65cb455eddd2fea00a11614483c84d50 to your computer and use it in GitHub Desktop.
Conditonaly hide some field in Unity inspector, base on other field's value (from http://www.brechtos.com/hiding-or-disabling-inspector-properties-using-propertydrawers-within-unity-5/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
//Original version of the ConditionalHideAttribute created by Brecht Lecluyse (www.brechtos.com) | |
//Modified by: - | |
[ AttributeUsage( AttributeTargets.Field ) ] | |
public class ConditionalHideAttribute : PropertyAttribute | |
{ | |
public readonly string ConditionalSourceField; | |
public string ConditionalSourceField2 = ""; | |
public bool HideInInspector = false; | |
public bool Inverse = false; | |
// Use this for initialization | |
public ConditionalHideAttribute( string conditionalSourceField ) | |
{ | |
ConditionalSourceField = conditionalSourceField; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
//Original version of the ConditionalHideAttribute created by Brecht Lecluyse (www.brechtos.com) | |
//Modified by: - | |
[ CustomPropertyDrawer( typeof( ConditionalHideAttribute ) ) ] | |
public class ConditionalHidePropertyDrawer : PropertyDrawer | |
{ | |
public override void OnGUI( Rect position, SerializedProperty property, GUIContent label ) | |
{ | |
var condHAtt = (ConditionalHideAttribute) attribute; | |
var enabled = GetConditionalHideAttributeResult( condHAtt, property ); | |
var wasEnabled = GUI.enabled; | |
GUI.enabled = enabled; | |
if ( !condHAtt.HideInInspector || enabled ) | |
{ | |
EditorGUI.PropertyField( position, property, label, true ); | |
} | |
GUI.enabled = wasEnabled; | |
} | |
public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) | |
{ | |
var condHAtt = (ConditionalHideAttribute) attribute; | |
var enabled = GetConditionalHideAttributeResult( condHAtt, property ); | |
if ( !condHAtt.HideInInspector || enabled ) | |
{ | |
return EditorGUI.GetPropertyHeight( property, label ); | |
} | |
else | |
{ | |
//The property is not being drawn | |
//We want to undo the spacing added before and after the property | |
return -EditorGUIUtility.standardVerticalSpacing; | |
} | |
} | |
private bool GetConditionalHideAttributeResult( ConditionalHideAttribute condHAtt, SerializedProperty property ) | |
{ | |
var enabled = true; | |
var propertyPath = | |
property.propertyPath; //returns the property path of the property we want to apply the attribute to | |
string conditionPath; | |
if ( !string.IsNullOrEmpty( condHAtt.ConditionalSourceField ) ) | |
{ | |
//Get the full relative property path of the sourcefield so we can have nested hiding | |
conditionPath = | |
propertyPath.Replace( property.name, | |
condHAtt.ConditionalSourceField ); //changes the path to the conditionalsource property path | |
var sourcePropertyValue = property.serializedObject.FindProperty( conditionPath ); | |
if ( sourcePropertyValue != null ) | |
{ | |
enabled = CheckPropertyType( sourcePropertyValue ); | |
} | |
else | |
{ | |
//Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); | |
} | |
} | |
if ( !string.IsNullOrEmpty( condHAtt.ConditionalSourceField2 ) ) | |
{ | |
conditionPath = | |
propertyPath.Replace( property.name, | |
condHAtt.ConditionalSourceField2 ); //changes the path to the conditionalsource property path | |
var sourcePropertyValue2 = property.serializedObject.FindProperty( conditionPath ); | |
if ( sourcePropertyValue2 != null ) | |
{ | |
enabled = enabled && CheckPropertyType( sourcePropertyValue2 ); | |
} | |
else | |
{ | |
//Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); | |
} | |
} | |
if ( condHAtt.Inverse ) enabled = !enabled; | |
return enabled; | |
} | |
private bool CheckPropertyType( SerializedProperty sourcePropertyValue ) | |
{ | |
switch ( sourcePropertyValue.propertyType ) | |
{ | |
case SerializedPropertyType.Boolean: | |
return sourcePropertyValue.boolValue; | |
case SerializedPropertyType.ObjectReference: | |
return sourcePropertyValue.objectReferenceValue != null; | |
default: | |
Debug.LogError( "Data type of the property used for conditional hiding [" + | |
sourcePropertyValue.propertyType + "] is currently not supported" ); | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks this helped me out a lot (: