Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug How do I proceed while facing this closed issue?

Discussion in 'Editor & General Support' started by Paul_John_Jeffs, Dec 13, 2022.

  1. Paul_John_Jeffs

    Paul_John_Jeffs

    Joined:
    Jun 26, 2019
    Posts:
    17
    I am seeing a bug where a custom class property with the
    HideInInspector
    attribute cannot be drawn with the
    EditorGUILayout.PropertyField
    method.

    I found a closed issue describing the same issue but I am still seeing it on 2022.1.20f1.

    Here is the issue: https://issuetracker.unity3d.com/is...-attribute-only-works-for-some-variable-types

    How should I proceed? I've never really used the issue tracker or the forums before so I'm not sure what the best course of action is. I've left a comment on the issue but I'm not sure if it will get seen by the right people or if I need to do more.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    Is it actually a property, in the C# sense? Or is it a field?
     
  3. Paul_John_Jeffs

    Paul_John_Jeffs

    Joined:
    Jun 26, 2019
    Posts:
    17
    It's a field.

    The custom editor will draw the label but none of the members of the class.
    If I remove the
    HideInInspector
    attribute the serialized class is drawn correctly in the inspector, but I need to hide the property so that when I call
    base.OnInspectorGUI
    to draw all the other properties, the properties I am trying to detect changes on are not drawn twice.

    I've got a workaround where I just get all the relative properties of my custom class and draw a property field for each one but this is not ideal because if I add/remove members of the class I will need to update the custom editor.

    I could also make my custom class a ScriptableObject to make it work but I don't want changes on one gameobject to affect others and I don't want to have to create a new SO every time I create a new prefab.
     
  4. Paul_John_Jeffs

    Paul_John_Jeffs

    Joined:
    Jun 26, 2019
    Posts:
    17
    Maybe easier if I post this code. The following code fails to draw the property field for "_classB"; just the label is drawn.

    Code (CSharp):
    1.  
    2. public class ClassB
    3. {
    4.     public float FloatB;
    5. }
    6.  
    7. public class ClassA : MonoBehaviour
    8. {
    9.     // I hide this field so that I can draw it myself and detect any changes.
    10.     [SerializeField, HideInInspector]
    11.     private ClassB _classB;
    12.     [SerializeField]
    13.     private float _floatA;
    14.  
    15.     public void OnClassBChanged()
    16.     {
    17.         // Do a thing
    18.     }
    19. }
    20.  
    21. [CustomEditor(typeof(ClassA))]
    22. public class ClassAEditor : Editor
    23. {
    24.     private ClassA _classA;
    25.    
    26.     private void OnEnable()
    27.     {
    28.         _classA = (ClassA)target;
    29.     }
    30.  
    31.     public override void OnInspectorGUI()
    32.     {
    33.         // I want to draw the base inspector so that _floatA and any other fields added in future are drawn in the inspector.
    34.         base.OnInspectorGUI();
    35.         using (var check = new EditorGUI.ChangeCheckScope())
    36.         {
    37.             // Then I draw the ClassB field within a change check so that I can detect changes.
    38.             SerializedProperty property = serializedObject.FindProperty("_classB");
    39.             EditorGUILayout.PropertyField(property);
    40.  
    41.             if (check.changed)
    42.             {
    43.                 _classA.OnClassBChanged();
    44.             }
    45.         }
    46.     }
    47. }
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
  6. Paul_John_Jeffs

    Paul_John_Jeffs

    Joined:
    Jun 26, 2019
    Posts:
    17
    I'm much more interesting in learning how to make it work rather than just making it work and moving on so I'll probably have a look at how NaughtyAttributes gets it done and implement a solution that does what I need, thanks for the tip.

    I would also like to know what the best procedure is for reporting this issue. If anyone is able to say so, is my comment on the issue tracker sufficient? Or should I submit a new issue? If so, where?