- Home /
Moving the input fields in a custom inspector
As you can see in the image above, the input fields of "Gravity" and "Blast Height" are not in line with that of the other values. Any ideas on how I would fix this? (The other values don't require a GUILayout.Label ()
.)
Thanks!
Edit: As requested, here is my code for the custom inspector (Sorry it's kinda messy):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor (typeof (ProjectileScript))]
public class ProjectileInspector : Editor {
public ProjectileScript script;
//public ProjectileType type;
void OnEnable () {
script = (ProjectileScript) target;
}
public override void OnInspectorGUI () {
GUILayout.BeginHorizontal ();
script.rb = (Rigidbody) EditorGUILayout.ObjectField ("Rigidbody", script.rb, typeof (Rigidbody), true);
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
script.gravity = EditorGUILayout.FloatField ("Gravity", script.gravity);
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
EditorGUILayout.LabelField ("Specifications", EditorStyles.boldLabel);
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
script.damage = EditorGUILayout.FloatField ("Damage", script.damage);
GUILayout.EndHorizontal ();
DisplayBlast ();
GUILayout.BeginHorizontal ();
script.floodingChance = EditorGUILayout.FloatField ("Flooding Chance", script.floodingChance);
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
script.type = (ProjectileType) EditorGUILayout.EnumPopup ("Projectile Type", script.type);
GUILayout.EndHorizontal ();
DisplayOptions ();
GUILayout.BeginHorizontal ();
EditorGUILayout.LabelField ("Particles", EditorStyles.boldLabel);
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
script.splash = (ParticleSystem) EditorGUILayout.ObjectField ("Splash", script.splash, typeof (ParticleSystem), true);
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
script.blast = (ParticleSystem) EditorGUILayout.ObjectField ("Explosion", script.blast, typeof (ParticleSystem), true);
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
script.trail = (ParticleSystem) EditorGUILayout.ObjectField ("Trail", script.trail, typeof (ParticleSystem), true);
GUILayout.EndHorizontal ();
}
void DisplayBlast () {
GUILayout.BeginHorizontal ();
script.explosion = EditorGUILayout.Toggle ("Blast Damage", script.explosion);
GUILayout.EndHorizontal ();
if (script.explosion) {
GUILayout.BeginHorizontal ();
script.radius = EditorGUILayout.FloatField ("Explosion Radius", script.radius);
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
script.dropoff = EditorGUILayout.CurveField ("Damage Dropoff", script.dropoff);
GUILayout.EndHorizontal ();
}
}
void DisplayOptions () {
switch (script.type) {
case ProjectileType.Shell:
GUILayout.BeginHorizontal ();
script.spin = EditorGUILayout.FloatField ("Shell Spin", script.spin);
GUILayout.EndHorizontal ();
break;
case ProjectileType.Torpedo:
GUILayout.BeginHorizontal ();
script.torpedoSpeed = EditorGUILayout.FloatField ("Torpedo Speed", script.torpedoSpeed);
GUILayout.EndHorizontal ();
break;
case ProjectileType.Bomb:
GUILayout.BeginHorizontal ();
script.bombDetonateHeight = EditorGUILayout.FloatField ("Blast Height", script.bombDetonateHeight);
GUILayout.EndHorizontal ();
break;
case ProjectileType.Rocket:
break;
case ProjectileType.DepthCharge:
GUILayout.BeginHorizontal ();
script.chargefuseTime = EditorGUILayout.FloatField ("Fuse Length", script.chargefuseTime);
GUILayout.EndHorizontal ();
break;
}
}
}
Answer by PanzerPotato · Jun 28, 2018 at 02:08 PM
Oh wait I found my problem! With variables that use rigidbodies and enums, they were fine because I didn't need a GUILayout.Label ()
, but other variables (ie. ones that use floats) did. But instead of using GUILayout.Label ()
, I could just put the name in EditorGUILayout.FloatField ();
as the first parameter (Take a look at the EditorGUILayout.FloatField ();
parts in my code: they now have the display name as the first parameter)!
Weirdly enough, I used GUILayout.Label ();
because I tried putting a string in the first parameter of EditorGUILayout.FloatField ();
, which didn't work. But now it does...
Answer by ModLunar · Jun 28, 2018 at 02:39 AM
Actually, there's a utility class somewhere in Unity that sets the default label width. .. I can never remember all their GUI/EditorGUI classes xD.
After a few minutes of searching, I remember now! It's EditorGUIUtility.labelWidth -- that's how many pixels the prefix labels take, not including the field to the right of them. (In your screenshot, that'd be the text saying "Rigidbody", "Gravity", "Projectile Type", etc.)
Perhaps try setting that value to something maybe around 120? Or you can always add to it and then subtract that same amount once you're done, set it to a variable, etc. If you're mixing your custom GUI code with some default GUI using base.OnInspectorGUI() or something like the DrawDefaultInspector() method, you could try wrapping it like this:
public override void OnInspectorGUI() {
EditorGUIUtility previousWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 120;
base.OnInspectorGUI(); //or DrawDefaultInspector(), Unity's GUI stuff
//Do your custom GUI stuff here
//Then finally, change the labelWidth back to what it was before you changed it:
EditorGUIUtility.labelWidth = previousWidth;
}
I experimented with something like that, adding and subtracting until it was of equal length, but once I resized the window it was all wrong again :(. Thanks for your answer though!
Aw dang >.< yeah right? Cause in EditorWindow classes, you get access to "position", which is a Rect that holds the width and height of the window. But I don't see anything like that when I looked in my Editor class
Your answer
Follow this Question
Related Questions
Edit existing editor inspector? 1 Answer
How to Hide/Show List or Array in the inspector based on a variable? 0 Answers
Prefix label greyed out when following component disabled 1 Answer
Cutom Array Inspector in a Custom Inspector 1 Answer
Error while using SerializeField and HideInInspector tags 0 Answers