Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question EditorGUILayout.PropertyField without Foldout

Discussion in 'Immediate Mode GUI (IMGUI)' started by atmuc, May 20, 2023.

  1. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,208
    When I use EditorGUILayout.PropertyField for an object inspector adds foldout triangle. How can I show PropertyField inspector without foldout?

    EditorGUILayout.PropertyField(MyObject);

    upload_2023-5-20_21-2-36.png
     
  2. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,208
    Is it impossible?
     
  3. TSRajesh

    TSRajesh

    Joined:
    Jun 19, 2013
    Posts:
    69
     
  4. nathfade

    nathfade

    Joined:
    Aug 23, 2018
    Posts:
    4
    I have same question but for EditorGUI.PropertyField
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,937
    And the same answer applies.
     
  6. samboknight

    samboknight

    Joined:
    Aug 31, 2019
    Posts:
    27
    I know this is an old thread, but I found a way to do this that it doesn't seem anyone else has mentioned. What you can do is make a custom property drawer for your serialized property that doesn't include the foldout. This takes a bit of code, but it can be done in a way that's reusable for any serialized property.
    Code (CSharp):
    1. // Assets/Scripts/Editor/NoFoldoutDrawer.cs
    2.  
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. // Can set as a custom property drawer for any number of types
    7. [CustomPropertyDrawer(typeof(MyClassA))]
    8. [CustomPropertyDrawer(typeof(MyClassB))]
    9. public class NoFoldoutDrawer : PropertyDrawer
    10. {
    11.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    12.     {
    13.         property.Next(true); // get first child of your property
    14.         do
    15.         {
    16.             EditorGUI.PropertyField(position, property, true); // Include children
    17.             position.y += EditorGUIUtility.singleLineHeight + 2;
    18.         }
    19.         while (property.Next(false)); // get property's next sibling
    20.     }
    21.  
    22.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    23.     {
    24.         property.Next(true);
    25.         float result = 0;
    26.         do
    27.         {
    28.             result += EditorGUI.GetPropertyHeight(property, true) + 2; // include children
    29.         }
    30.         while (property.Next(false));
    31.     }
    32. }
     
    Last edited: May 4, 2024