Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Smike · Apr 10, 2014 at 06:57 PM · editorinspectoreditorwindowfoldout

Custom Editor foldout doesn't unfold when clicking on name?

Hi everyone, I implemented my custom Unity editor class with a foldout which shows its child variables when clicked. However, in the inspector, it only unfolds when I click on the little triangle: alt text and not when I click on the actual name itself: alt text

Usually, in Unity's built-in foldouts (for example: Public array variables), clicking on the name automaticallt unfolds it.

Is there any way to get that effect in a custom Unity Editor Script?

Here's my code so far:

 isUnfolded = EditorGUILayout.Foldout (isUnfolded , "My Folder");

 if (isUnfolded)
     //My variables...


1.png (1.6 kB)
2.png (1.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by frarees · Apr 11, 2014 at 10:41 AM

Handle the event yourself before EditorGUILayout.Foldout does for you (and Use it! as shown below)

         EditorGUI.BeginChangeCheck ();
         EditorGUILayout.GetControlRect (true, 16f, EditorStyles.foldout);
         Rect foldRect = GUILayoutUtility.GetLastRect ();
         if (Event.current.type == EventType.MouseUp && foldRect.Contains (Event.current.mousePosition)) {
             foldout = !foldout;
             GUI.changed = true;
             Event.current.Use ();
         }
         foldout = EditorGUI.Foldout (foldRect, foldout, "Foldout");
         if (EditorGUI.EndChangeCheck ()) {
             Debug.Log ("fold state updated");
         }
 
         if (foldout) {
             EditorGUI.indentLevel++;
             EditorGUILayout.LabelField ("I see you");
             EditorGUI.indentLevel--;
         }

I've added some extra stuff to mark the foldout as changed, so it's handled properly..

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 11, 2014 at 11:26 AM 12
Share

You don't have to handle the event yourself. You just need to call the Foldout version that takes an additional bool (toggleOnLabelClick) between the content and the style parameter. Unfortunately this version isn't "wrapped" for GUILayout. So all you have to do is create the missing wrapping method(s):

 public static bool Foldout(bool foldout, GUIContent content, bool toggleOnLabelClick, GUIStyle style)
 {
     Rect position = GUILayoutUtility.GetRect(40f, 40f, 16f, 16f, style);
     // EditorGUI.kNumberW == 40f but is internal
     return EditorGUI.Foldout(position, foldout, content, toggleOnLabelClick, style);
 }
 public static bool Foldout(bool foldout, string content, bool toggleOnLabelClick, GUIStyle style)
 {
     return Foldout(foldout, new GUIContent(content), toggleOnLabelClick, style);
 }

So you just have to pass "true" to "toggleOnLabelClick" like this:

     expanded = Foldout(expanded, "someName", true, EditorStyles.foldout);

Like mentioned in the code-comment the original code uses an internal constant "EditorGUI.kNumberW" which has the value 40f but it's internal so you can't access it.

avatar image Smike · Apr 12, 2014 at 06:58 PM 0
Share

Great, thanks! Your technique works, Bunny83.

avatar image PedroMR · Aug 09, 2014 at 11:05 AM 1
Share

Thanks Bunny83 -- that technique worked perfectly for me too. Wish I could upvote, no rep on this site yet. ;-)

avatar image codestage · Mar 01, 2015 at 12:05 AM 6
Share

1-line solution: just use EditorGUI version with automatically calculated Rect and toggleOnLabelClick argument set to true:

 commonFoldout = EditorGUI.Foldout(EditorGUILayout.GetControlRect(), commonFoldout, "Common issues", true);
         
avatar image VipHaLongPro · May 17, 2015 at 08:09 PM 0
Share

@Dmitriy Yukhanov are you sure that always works? I've tried exactly that code but there was an ArgumentException saying: GUILayout: $$anonymous$$ismatched LayoutGroup.Repaint ...

Show more comments
avatar image
8

Answer by gresolio · May 04, 2018 at 03:05 PM

I don't know exactly starting with which version this is available, but very handy:

 state = EditorGUILayout.Foldout (state, title, true);

or

 state = EditorGUILayout.Foldout (state, title, true, EditorStyles.foldout);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by PixelFireXY · Apr 15 at 03:02 PM

Actually, there is a parameter to open and close the foldout by clicking on the label.


alt text


nuova-immagine-bitmap.jpg (28.5 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inspector view changed! Looks totally different. Apply, Revert, ... prefab buttons gone. Help! 1 Answer

Unity editor - keeping your foldout from collapsing 1 Answer

How to have two inspectors of different types thats objects they are inspecting change 0 Answers

Custom Editor like Package Manager 0 Answers

Create an Editor Window like Mecanim 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges