Advertisement
CandidMoon

Hide In Derived Inspector

Oct 29th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. /// <summary>
  2. /// Search to top of the hierarchy via fieldNames
  3. /// </summary>
  4. /// <param name="type"></param>
  5. /// <param name="fieldNames"></param>
  6. /// <returns>Parent object FieldInfo || null if has no parent</returns>
  7. public static FieldInfo GetParentObjectFieldInfoViaPath(this Type type, string[] fieldNames)
  8. {
  9.     FieldInfo fieldInfo = null;
  10.  
  11.     for (int i = 0; i < fieldNames.Length - 1; i++)
  12.     {
  13.         fieldInfo = type.GetField(fieldNames[i]);
  14.         if (fieldInfo != null)
  15.             type = fieldInfo.FieldType;
  16.         else
  17.             break;
  18.     }
  19.  
  20.     return fieldInfo;
  21. }
  22.  
  23. public static Type GetParentObjectFieldType(this SerializedProperty property, string[] fieldNames)
  24. {
  25.     Type containingObjectType = property.serializedObject.targetObject.GetType();
  26.  
  27.     FieldInfo fieldInfo = containingObjectType.GetParentObjectFieldInfoViaPath(fieldNames);
  28.     return fieldInfo.FieldType;
  29. }
  30.  
  31. namespace Kimo.Assistance.UTILITY
  32. {
  33. #if UNITY_EDITOR
  34.     public abstract class HideInInspectorDrawerBase : PropertyDrawer
  35.     {
  36.         protected abstract bool IsHidden(SerializedProperty property);
  37.  
  38.         public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  39.         {
  40.             // return this.IsHidden(property) ? 0f : base.GetPropertyHeight(property, label);
  41.             return 0f;
  42.         }
  43.  
  44.         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  45.         {
  46.             if (!this.IsHidden(property))
  47.                 EditorGUILayout.PropertyField(property, true);
  48.         }
  49.     }
  50. #endif
  51. }
  52.  
  53. namespace Kimo.Assistance.UTILITY
  54. {
  55. #if UNITY_EDITOR
  56.     [CustomPropertyDrawer(typeof(HideInDerivedInspectorAttribute))]
  57.     [CanEditMultipleObjects]
  58.     public class HideInDerivedInspectorDrawer : HideInInspectorDrawerBase
  59.     {
  60.         protected override bool IsHidden(SerializedProperty property)
  61.         {
  62.             HideInDerivedInspectorAttribute attr = this.attribute as HideInDerivedInspectorAttribute;
  63.            
  64.             string[] fieldNames = property.propertyPath.Split('.');
  65.  
  66.             // If it's not a nested field
  67.             if (fieldNames.Length <= 1)
  68.                 return property.serializedObject.targetObject.GetType() != fieldInfo.DeclaringType;
  69.  
  70.             return property.GetParentObjectFieldType(fieldNames) != fieldInfo.DeclaringType;
  71.         }
  72.     }
  73. #endif
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement