Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Search to top of the hierarchy via fieldNames
- /// </summary>
- /// <param name="type"></param>
- /// <param name="fieldNames"></param>
- /// <returns>Parent object FieldInfo || null if has no parent</returns>
- public static FieldInfo GetParentObjectFieldInfoViaPath(this Type type, string[] fieldNames)
- {
- FieldInfo fieldInfo = null;
- for (int i = 0; i < fieldNames.Length - 1; i++)
- {
- fieldInfo = type.GetField(fieldNames[i]);
- if (fieldInfo != null)
- type = fieldInfo.FieldType;
- else
- break;
- }
- return fieldInfo;
- }
- public static Type GetParentObjectFieldType(this SerializedProperty property, string[] fieldNames)
- {
- Type containingObjectType = property.serializedObject.targetObject.GetType();
- FieldInfo fieldInfo = containingObjectType.GetParentObjectFieldInfoViaPath(fieldNames);
- return fieldInfo.FieldType;
- }
- namespace Kimo.Assistance.UTILITY
- {
- #if UNITY_EDITOR
- public abstract class HideInInspectorDrawerBase : PropertyDrawer
- {
- protected abstract bool IsHidden(SerializedProperty property);
- public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
- {
- // return this.IsHidden(property) ? 0f : base.GetPropertyHeight(property, label);
- return 0f;
- }
- public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
- {
- if (!this.IsHidden(property))
- EditorGUILayout.PropertyField(property, true);
- }
- }
- #endif
- }
- namespace Kimo.Assistance.UTILITY
- {
- #if UNITY_EDITOR
- [CustomPropertyDrawer(typeof(HideInDerivedInspectorAttribute))]
- [CanEditMultipleObjects]
- public class HideInDerivedInspectorDrawer : HideInInspectorDrawerBase
- {
- protected override bool IsHidden(SerializedProperty property)
- {
- HideInDerivedInspectorAttribute attr = this.attribute as HideInDerivedInspectorAttribute;
- string[] fieldNames = property.propertyPath.Split('.');
- // If it's not a nested field
- if (fieldNames.Length <= 1)
- return property.serializedObject.targetObject.GetType() != fieldInfo.DeclaringType;
- return property.GetParentObjectFieldType(fieldNames) != fieldInfo.DeclaringType;
- }
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement