Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Keriz · Jan 06, 2014 at 06:34 PM · inspectorsizecustom-inspector

Custom inspector size problem

Hello,

Today I was working on a custom editor script, but i'm having an issue. In fact when I add some EditorGUI foldouts, the inspector window size does not increase with the numbers of fouldouts. So here's what I get: Moreover, I can't expand AK47' foldouts.

alt text

Here's my script:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomEditor(typeof(Character))]
 class  CharacterEditor: Editor {
 
     public bool fold = false;
 
     private GUIContent
         deleteButtonContent = new GUIContent("-", "delete weapon"),
         addButtonContent = new GUIContent("Create new weapon", "add a new weapon");
 
     bool[] weaponsFouldOut;
     private bool hasInspectorBeenInitialized = false;
 
     public override void OnInspectorGUI() {
         base.OnInspectorGUI ();
         Character character = (Character)target;
 
         if (!hasInspectorBeenInitialized) {
                         weaponsFouldOut = new bool[character.GetWeapons ().Count];
             hasInspectorBeenInitialized = true;
                 }
 
         Rect space = EditorGUILayout.BeginHorizontal();
         //EditorGUILayout.TextArea(string.Empty, GUIStyle.none, GUILayout.Height(111));
         EditorGUILayout.EndHorizontal();
 
         fold = EditorGUILayout.Foldout(fold, "Weapons");
         if(fold) {
             space.y += 16;
             space.x += 16;
             int i = 0;
         foreach (BaseWeapon weapon in character.GetWeapons()){
                 weaponsFouldOut[i] = EditorGUI.Foldout(space, weaponsFouldOut[i], weapon.m_WeaponName, true);
                 if (weaponsFouldOut[i] == true)
                     space.y += 16;
                 space.y += 16;
                 i++;
             }
         }
 
     }
 }

Thank you guys !

probleme.png (10.4 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

1 Reply

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

Answer by Bunny83 · Jan 06, 2014 at 06:49 PM

You should only use GUILayout and EditorGUILayout functions. If you still need to use normal GUI functions you should reserve a rect with GUILayoutUtility.GetRect

Comment
Add comment · Show 5 · 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 Keriz · Jan 06, 2014 at 11:15 PM 0
Share

Thanks, trying to add what you said but it's still not working. Do you figure out my mistake ?

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomEditor(typeof(Character))]
 class  CharacterEditor: Editor {
 
     public bool fold = false;
 
     private GUIContent
         deleteButtonContent = new GUIContent("-", "delete weapon"),
         addButtonContent = new GUIContent("Create new weapon", "add a new weapon");
 
     private bool[] weaponsFouldout;
     private bool hasInspectorBeenInitialized = false;
 
     public override void OnInspectorGUI() {
         base.OnInspectorGUI ();
         Character character = (Character)target;
 
         if (!hasInspectorBeenInitialized) {
                         weaponsFouldout = new bool[character.GetWeapons().Count];
             hasInspectorBeenInitialized = true;
                 }
 
 
         fold = EditorGUILayout.Foldout(fold, "Weapons");
         Rect rt = GUILayoutUtility.GetRect(new GUIContent("A$$anonymous$$47"), GUIStyle.none); //added this
         if(fold) {
             rt.x += 16;
             int i = 0;
             weaponsFouldout = new bool[character.GetWeapons().Count];
         foreach (BaseWeapon weapon in character.GetWeapons()){
 
                 weaponsFouldout[i] = EditorGUI.Foldout(rt, weaponsFouldout[i], weapon.m_WeaponName, true); //here's the issue
                 i++;
                 rt.y += 16;
             }
         }
 
     }
 }

Thank you again for your help.

avatar image Bunny83 · Jan 06, 2014 at 11:38 PM 0
Share

Why do you use "EditorGUI.Foldout" and not "EditorGUILayout.Foldout"?

You should initialize your bool array "weaponsFouldout" in OnEnable, at the moment you recreate the array all the time so the content is lost.

Something like this would make more sense:

 fold = EditorGUILayout.Foldout(fold, "Weapons");
 if(fold)
 {
     GUILayout.BeginHorizontal();
     GUILayout.Space(15);
     GUILayout.BeginVertical();
     int i = 0;
     foreach (BaseWeapon weapon in character.GetWeapons())
     {
         weaponsFouldout[i] = EditorGUILayout.Foldout(weaponsFouldout[i], weapon.m_WeaponName);
         i++;
         if (weaponsFouldout[i])
         {
             // Draw sub menu stuff
             GUILayout.Space(20); // dummy content
         }
     }
     GUILayout.EndVertical();
     GUILayout.EndHorizontal();
 }

Since the behaviour of Foldout is a bit strange i usually use a normal Toggle. You can simply use the Foldout style so it looks like a foldout but behaves like a toggle:

     weaponsFouldout[i] = GUILayout.Toggle(weaponsFouldout[i], weapon.m_WeaponName, "Foldout");
avatar image Bunny83 · Jan 06, 2014 at 11:41 PM 0
Share

Usually there's never the need for non layout functions in the inspector. If you use them you have to reserve all space your GUI will use. GetRect has many different versions (just scroll down a bit). In your case the $$anonymous$$ / max width / height version would make the most sense here.

avatar image Keriz · Jan 07, 2014 at 05:51 PM 0
Share

Perfect, i now have a very good understanding of my issue. Perfectly working right now, thanks again !

You should initialize your bool array "weaponsFouldout" in OnEnable, at the moment you recreate the array all the time so the content is lost.

Yeah, it was only for test purpose.

Glad to understand how it works now, but i guess for beginners like me in Inspector program$$anonymous$$g it's a bit tricky to see the difference between EditorGUI, EditorGUILayout, GUILayout, GUI..

Is there a way to give you a "+rep" for your answer ?

Thank you.

avatar image Bunny83 · Jan 07, 2014 at 08:27 PM 1
Share

If your problem is solved you should accept the answer by clicking the checkmark. ;)

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Custom inspector to re-order components 2 Answers

manually refresh scripts in inspector 1 Answer

Custom inspector for specific variable type to be reused? 2 Answers

Moving the input fields in a custom inspector 2 Answers


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