Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. // No esta funcionando como se espera al usar arrays (muestra array y oculta los children mas bien)
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #endif
  9.  
  10. public class OcultadorAttribute : PropertyAttribute
  11. {
  12. string nombreData;
  13. int intMin, intMax;
  14. float floatMin, floatMax;
  15. bool xorInverter;
  16. #if UNITY_EDITOR
  17. public SerializedProperty GetProperty(SerializedProperty propHermana)
  18. {
  19. return propHermana.FindPropertyRelative(nombreData);
  20. }
  21. public SerializedProperty GetProperty(SerializedObject objContenedor)
  22. {
  23. return objContenedor.FindProperty(nombreData);
  24. }
  25. public bool Comp(SerializedProperty prop)
  26. {
  27. if(prop==null)return true;//El default es true porqu si todo falla queremos que la cosa sea visible
  28. if (prop.propertyType == SerializedPropertyType.Boolean) return prop.boolValue ^ xorInverter;
  29. else if (prop.propertyType == SerializedPropertyType.Enum) return Comp(prop.enumValueIndex) ^ xorInverter;
  30. else if (prop.propertyType == SerializedPropertyType.Integer) return Comp(prop.intValue) ^ xorInverter;
  31. else if (prop.propertyType == SerializedPropertyType.Float) return Comp(prop.floatValue) ^ xorInverter;
  32. else if (prop.propertyType == SerializedPropertyType.String) return Comp(prop.stringValue) ^ xorInverter;
  33. else if (prop.propertyType == SerializedPropertyType.ObjectReference) return (prop.objectReferenceValue!=null) ^ xorInverter;
  34. return true;//El default es true porqu si todo falla queremos que la cosa sea visible
  35. }
  36. bool Comp(int valor)
  37. {
  38. if (valor >= intMin) return valor <= intMax;
  39. else return false;
  40. }
  41. bool Comp(float valor)
  42. {
  43. if (valor >= floatMin) return valor <= floatMax;
  44. else return false;
  45. }
  46. bool Comp(string valor)
  47. {
  48. return !string.IsNullOrEmpty(valor);
  49. }
  50. #endif
  51. public OcultadorAttribute(string nombreData, bool negarValorBool = false)
  52. {
  53. this.nombreData = nombreData;
  54. this.xorInverter = negarValorBool;
  55. }
  56. public OcultadorAttribute(string nombreData, int valInt, bool negarValorBool = false)
  57. {
  58. this.nombreData = nombreData;
  59. this.intMin = this.intMax = valInt;
  60. this.xorInverter = negarValorBool;
  61. }
  62. public OcultadorAttribute(string nombreData, float valFloat, bool negarValorBool = false)
  63. {
  64. this.nombreData = nombreData;
  65. this.floatMin = this.floatMax = valFloat;
  66. this.xorInverter = negarValorBool;
  67. }
  68. }
  69.  
  70. #if UNITY_EDITOR
  71. [CustomPropertyDrawer(typeof(OcultadorAttribute))]
  72. public class OcultadorAttributeDrawer : PropertyDrawer
  73. {
  74. float Visibilidad
  75. {
  76. get
  77. {
  78. if (visibilidad == null) return 1f;
  79. else return visibilidad.target?1f:0f;
  80. }
  81.  
  82. }
  83. OcultadorAttribute ocultador;
  84. SerializedProperty propControl;
  85. UnityEditor.AnimatedValues.AnimBool visibilidad;
  86.  
  87. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  88. {
  89. if (ocultador == null)
  90. {
  91. ocultador = attribute as OcultadorAttribute;
  92. propControl=ocultador.GetProperty(property.serializedObject);
  93. visibilidad=new UnityEditor.AnimatedValues.AnimBool(ocultador.Comp(propControl));
  94.  
  95. // visibilidad.valueChanged +=
  96. }
  97. else visibilidad.target=ocultador.Comp(propControl);
  98.  
  99. if(visibilidad.target)EditorGUI.PropertyField(position, property);
  100. }
  101. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  102. {
  103. return EditorGUI.GetPropertyHeight(property) * Visibilidad;
  104. }
  105. }
  106. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement