- Home /
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.
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 !
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
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.
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");
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.
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.
If your problem is solved you should accept the answer by clicking the checkmark. ;)