Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
22
Question by Kourosh · Sep 24, 2010 at 03:17 PM · guivariablesinterfacegroup

How to group variables in the Inspector?

I'm creating a dynamic GUI which requires lots of variables in it, just wondering how i can group variables under a title so that it looks organized and easy to change values?


now included in Unity .. [Header("Hi there!")]

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

5 Replies

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

Answer by Fattie · Dec 11, 2014 at 04:58 PM

It's this simple:

 [Header("Hi there!")]

since about 2014, cheers


Note that

 [Space(10)]

is also quite handy.

There's also a Range slider which is very handy

 [Range(5,9)]

alt text

alt text

doco ..
http://docs.unity3d.com/ScriptReference/RangeAttribute-ctor.html
http://docs.unity3d.com/ScriptReference/HeaderAttribute.html
etc


Note; there was previously an "order" attribute .. Unity have completely removed it around 2016. They also much improved the general functioning of the "Header" system.

Note that you cannot, for example, follow a "Header" line with an enum. You have to go enum - header - variable; you can't go header - enum - variable !

And each "header" must be followed by at least one variable. You can't (currently) have a header then a gap and then another header, even if you use space.

The system is a bit quirky but one of the nicest things in Unity.


screen-shot-2016-01-25-at-65547-am.png (164.9 kB)
screen-shot-2016-01-25-at-70007-am.png (84.5 kB)
Comment
Add comment · Show 4 · 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 Ziplock9000 · May 19, 2016 at 12:44 PM 1
Share

It seems ", order=0" no longer works with Unity :/

avatar image Fattie Ziplock9000 · Jun 02, 2016 at 07:43 PM 0
Share

Correct, they completely removed "Order" ...!

avatar image petey Fattie · Nov 26, 2016 at 02:11 AM 0
Share

I don't know how I missed this! Awesome! You sir, are a dude :)

avatar image kamyflex · Nov 07, 2016 at 04:18 AM 0
Share

Thanks $$anonymous$$an :)

avatar image
20

Answer by Stardog · Mar 16, 2014 at 03:24 PM

You can group things in a new class:

 [System.Serializable]
 public class Movement
 {
     public float moveV;
     public float moveH;
 
     public bool isRunning;
     public bool isLooking;
 }
 public Movement movement;
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
9

Answer by terrehbyte · Jun 12, 2017 at 08:46 AM

I would advise against @Stardog's suggestion to create an entirely new struct since it would introduce structural changes to your code in order to achieve a specific result in the editor. In this case in particular, it would be less than preferable because it impacts the way those variables are accessed.

In principle, the editor provides an interface for working with your scripts, so you should avoid making those changes of accommodations for the editor unless it's an editor script, which explicitly serves the purpose of defining specific behavior for the editor to follow.

Rather than accessing them directly as a part of the namespace of that class, it's now part of a member struct of that class.

No Changes

 public float moveV;
 
 ...
 
 moveV = 1.0f;

Enclosed in a Struct

 [System.Serializable]
 public class Movement
 {
   public float moveV;
   public float moveH;
   
   public bool isRunning;
   public bool isLooking;
 }
 public Movement movement;
 
 ...
 
 movement.moveV = 1.0f;

In this case, an entirely new data type is being created and instantiated in order to take advantage of editor's preference to display structs as a foldable section.

An easy alternative would be to follow what the selected answer has done, which is to use relatively innocuous attributes to insert header labels in the inspector view. That approach does not make any structural changes, which means that the manner in which that data is accessed will remain unchanged.

If it's sufficiently complex, then you could opt to leverage an entirely custom solution where you define a custom inspector script for a particular component. Something like a foldout to go as far as creating a little arrow that can expand and collapse a number of members. It will add some overhead in terms of the amount of work that must be done to expose new variables to the editor. That would be not be a cost you would incur if you were opt for something more lightweight, like the editor attributes mentioned in the selected answer.

An example of foldout usage

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 IgniteCoders · May 27, 2018 at 03:15 PM 1
Share

This should be the accepted answer. You doesn't have to change your code for the inspector, you only add another file to configurate that, without adding more behaviours than necesary to our code.

avatar image millerlyte87 · Sep 15, 2020 at 02:46 PM 0
Share

This is a great point about not changing your code for visual layout in the inspector. It's also worth pointing out that structs cannot be assigned default values, and if you switch to classes, well then more allocations.

avatar image
1

Answer by luislodosm · Jan 20, 2018 at 11:29 AM

Example:

 public class Player : MonoBehaviour
 {
     public float health;
     public float stamina;
     public float speed;
     public CombatStats combatStats;
 
     [System.Serializable]
     public class CombatStats
     {
         public float attackPower;
         public float defense;
         public float dodge;
     }
 }
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
1

Answer by reddtoric · Jul 22, 2019 at 02:25 AM

I agree with @terrehbyte's response.
Creating a class (or even a struct, because classes create more garbage) for the sake of organizing inspector is a bad solution.
However, if for example the fields belong together such as @luislodosm's example, it is a good idea (and should be used) but use structs rather than classes.

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

20 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

Related Questions

Trouble creating a horizontal group for Tall and Wide screen orientation! 0 Answers

How to create an agenda or timetable system? 1 Answer

GUI.Label positioning for many device resolutions 1 Answer

Camera in Interface 1 Answer

GUI area vs group 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