Skip to content

Instantly share code, notes, and snippets.

@pacoelayudante
Last active October 1, 2019 20:59
Show Gist options
  • Save pacoelayudante/d124805ed3581c4bdc05eecfa9c3bcea to your computer and use it in GitHub Desktop.
Save pacoelayudante/d124805ed3581c4bdc05eecfa9c3bcea to your computer and use it in GitHub Desktop.
Este decorator es una forma facil y rapida de que los fields se oculten en el inspector dependiendo del valor de otros fields sin tener que hacer un Editor
// No esta funcionando como se espera al usar arrays (muestra array y oculta los children mas bien)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class OcultadorAttribute : PropertyAttribute
{
string nombreData;
int intMin, intMax;
float floatMin, floatMax;
bool xorInverter;
#if UNITY_EDITOR
public SerializedProperty GetProperty(SerializedProperty propHermana)
{
return propHermana.FindPropertyRelative(nombreData);
}
public SerializedProperty GetProperty(SerializedObject objContenedor)
{
return objContenedor.FindProperty(nombreData);
}
public bool Comp(SerializedProperty prop)
{
if(prop==null)return true;//El default es true porqu si todo falla queremos que la cosa sea visible
if (prop.propertyType == SerializedPropertyType.Boolean) return prop.boolValue ^ xorInverter;
else if (prop.propertyType == SerializedPropertyType.Enum) return Comp(prop.enumValueIndex) ^ xorInverter;
else if (prop.propertyType == SerializedPropertyType.Integer) return Comp(prop.intValue) ^ xorInverter;
else if (prop.propertyType == SerializedPropertyType.Float) return Comp(prop.floatValue) ^ xorInverter;
else if (prop.propertyType == SerializedPropertyType.String) return Comp(prop.stringValue) ^ xorInverter;
else if (prop.propertyType == SerializedPropertyType.ObjectReference) return (prop.objectReferenceValue!=null) ^ xorInverter;
return true;//El default es true porqu si todo falla queremos que la cosa sea visible
}
bool Comp(int valor)
{
if (valor >= intMin) return valor <= intMax;
else return false;
}
bool Comp(float valor)
{
if (valor >= floatMin) return valor <= floatMax;
else return false;
}
bool Comp(string valor)
{
return !string.IsNullOrEmpty(valor);
}
#endif
public OcultadorAttribute(string nombreData, bool negarValorBool = false)
{
this.nombreData = nombreData;
this.xorInverter = negarValorBool;
}
public OcultadorAttribute(string nombreData, int valInt, bool negarValorBool = false)
{
this.nombreData = nombreData;
this.intMin = this.intMax = valInt;
this.xorInverter = negarValorBool;
}
public OcultadorAttribute(string nombreData, float valFloat, bool negarValorBool = false)
{
this.nombreData = nombreData;
this.floatMin = this.floatMax = valFloat;
this.xorInverter = negarValorBool;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(OcultadorAttribute))]
public class OcultadorAttributeDrawer : PropertyDrawer
{
float Visibilidad
{
get
{
if (visibilidad == null) return 1f;
else return visibilidad.target?1f:0f;
}
}
OcultadorAttribute ocultador;
SerializedProperty propControl;
UnityEditor.AnimatedValues.AnimBool visibilidad;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (ocultador == null)
{
ocultador = attribute as OcultadorAttribute;
propControl=ocultador.GetProperty(property.serializedObject);
visibilidad=new UnityEditor.AnimatedValues.AnimBool(ocultador.Comp(propControl));
// visibilidad.valueChanged +=
}
else visibilidad.target=ocultador.Comp(propControl);
if(visibilidad.target)EditorGUI.PropertyField(position, property);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property) * Visibilidad;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment