Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by PanzerPotato · Jun 27, 2018 at 09:50 PM · inspectorinputfieldcustom-inspectorguilayoutcustom inspector

Moving the input fields in a custom inspector

alt text

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;
         }
     }
 }

screenshot.png (13.5 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

2 Replies

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

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)!

Comment
Add comment · Show 1 · 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 PanzerPotato · Jun 28, 2018 at 02:10 PM 0
Share

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...

avatar image
0

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;
 }
Comment
Add comment · Show 2 · 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 PanzerPotato · Jun 28, 2018 at 01:51 PM 1
Share

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!

avatar image ModLunar PanzerPotato · Jun 28, 2018 at 02:01 PM 0
Share

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

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

89 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 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 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 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

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


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