Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to do "HideInInspector" from a custom inspector?

Discussion in 'Editor & General Support' started by JeffersonTD, Jan 12, 2018.

  1. JeffersonTD

    JeffersonTD

    Joined:
    Feb 5, 2013
    Posts:
    267
    I've often done custom stuff to be shown in a custom inspector, based on some criteria, and always hiding certain fields is of course simple with HideInInspector, but how do I hide a certain field based on some condition using a CustomEditor?

    I know I could do this in a reverse fashion by using HideInInspector in the target script, and then recreating the thing manually in the custom inspector, but that would require a lot of manual work if there are multiple fields that may need to be hidden, and it would also result in a lot of ugly code repetition too. There should be some way to do HideInInspector in a CustomEditor too, right?
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Something like this.

    Code (csharp):
    1. GUI.enabled = someCondition;
    2. EditorGUILayout.PropertyField(MyCoolField);
    3. GUI.enabled = true;
    So here if `someCondition` is false then all GUI after this line is greyed out. If you turn it back on immediately, then only one line is greyed out conditionally.
     
    JeffersonTD likes this.
  3. JeffersonTD

    JeffersonTD

    Joined:
    Feb 5, 2013
    Posts:
    267
    Thanks for the comment! Yeah, I actually just found from the decompiled Editor.cs that EditorGUILayout.PropertyField does the job. I was actually going for not rendering the field at all (so not calling PropertyField at all for certain fields as opposed to disabling those fields), but yeah, it's good to keep the GUI.enabled = false option in mind too.

    In any case, problem solved!