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
0
Question by Chatar · Oct 18, 2011 at 04:00 PM · javascriptarraysvariablesclasses

Organizing variables in the inspector

Hi, is there a way with classes and with javascript to do the same thing in the inspector that arrays does. The thing where you can fold out so you can see more variables. Lets say i got some code like this. What would i need to do so the class Attack and the Movement class is a part of the Enemy in the inspector but as a thing that you can fold out and in.

 class Enemy
 {
     var Health : float;
     
 }
 
 class Attack
 {
     var Damage : int;    
 }
 
 class Movement
 {
     var moveSpeed : float;
     var normalSpeed : float;
 }
 var enemy = new Enemy();
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
3
Best Answer

Answer by Steven-Walker · Oct 18, 2011 at 07:10 PM

To organize your parameters in the inspector, you need to create an editor script. However, the code example you give won't really work. You need to create 1 class that encompasses all the attributes of your object, rather than being spread out across multiple classes/objects. For example, I'd move Damage, moveSpeed and normalSpeed all into the Enemy class. Then your editor script would look something like:

 @CustomEditor(Enemy)
 class EnemyEditor extends Editor {
     private var showParams : boolean = true;
     function OnInspectorGUI() {
         showParams = EditorGUILayout.Foldout(showParams, "Enemy Parameters");
         if(showParams) {
             target.Health = EditorGUILayout.FloatField("Health", target.Health);
             target.Damage = EditorGUILayout.FloatField("Damage", target.Damage);
             target.moveSpeed = EditorGUILayout.FloatField("moveSpeed", target.moveSpeed);
             target.normalSpeed = EditorGUILayout.FloatField("normalSpeed", target.normalSpeed);
         }
     }
 }

Name your editor script something like 'EnemyEditor.js' and place it in Assets/Editor. Then when you select an object with the Enemy.js script, the inspector will display your custom editor script layout.

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 Chatar · Oct 18, 2011 at 08:29 PM 0
Share

This looks promising. Cant test it out now as i'm not by a computer with unity. But just a question, say i got a couple of variables for lets say movement. Can i name a foldout like "$$anonymous$$ovement" and have all my movement variables in that foldout?

avatar image Steven-Walker · Oct 18, 2011 at 08:52 PM 0
Share

Yes. You just create another foldout set.

avatar image Chatar · Oct 18, 2011 at 09:14 PM 0
Share

Ok, will try this tomorrow. Will come back if there is something thats not working. Thanks.

avatar image Chatar · Oct 19, 2011 at 09:30 AM 0
Share

Hm, i tried this out but cannot get it to work. I put the EnemyEditor in a folder that i made thats in assets thats named Editor. But nothing happens when i press my object with the Enemy script on it? Any idea why it's not working? -Never $$anonymous$$d i figured it out the thing at the top (@CustomEditor($$anonymous$$ainEnemy)) was not same as my script name. But now i get a error "NullReferenceException: Object reference not set to an instance of an object". - And never $$anonymous$$d again.. Fixed it too. In the script Steven wrote he called health directly, that wont work you have to call the object you set the class to. So it will be "target.enemy.Health = EditorGUILayout.FloatField("Health", target.enemy.Health);" If you set your class like this "var enemy = new Enemy();".

avatar image
5

Answer by syclamoth · Oct 19, 2011 at 10:29 AM

How about just doing it the simple way, using composition? I don't understand why the other two posts here are so complex!

class Enemy { var attack : Attack; var move : Movement;

}

Then, when you put it in your script they should show up in the inspector.

Comment
Add comment · Show 3 · 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 Chatar · Oct 19, 2011 at 10:57 AM 0
Share

First, i think thats C# not javascript and second i want to be able to make foldouts where i can group together variables.

avatar image syclamoth · Oct 19, 2011 at 01:08 PM 0
Share

First, no it's not, second, the foldouts happen automatically! You don't need to make custom editors unless you're talking about really unusual behaviour.

avatar image sig · Jun 06, 2013 at 09:03 AM 1
Share

This is the best answer; curious I can't vote it up.

Useful to notice is that in C#, you must add [System.Serializable] before the class definition, then it will show up. Of course, the class needs to be serializable...

avatar image
1

Answer by chemicalvamp · Oct 18, 2011 at 06:18 PM

You could put your variables in a region like so:

 #region Variables
 //variables go here..
 #endregion

But i have noticed monodevelope doesnt always put in the + and - in for collapsing it should look like this:

Photobucket

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 syclamoth · Oct 19, 2011 at 04:36 PM 0
Share

I don't think we're talking about the same thing, here.

avatar image
0

Answer by Bilelmnasser · Jun 06, 2014 at 10:59 PM

Simple if i understand your " in and out " , test this two example :

 #pragma strict
 
 
 public class Enemy
 {
     var Health : float;
  var attack:Attack;
 var mouvement:Movement;
 }
  
 class Attack
 {
     var Damage : int;  
 }
  
 class Movement
 {
     var moveSpeed : float;
     var normalSpeed : float;
 }
 var enemy = new Enemy();
 
 function Start () {
 
 }
 
 function Update () {
 
 }


and this with array element for those 2 subclasses stript :

 #pragma strict
 
 
 public class Enemy
 {
     var Health : float;
  var attack:Attack [];
 var mouvement:Movement[];
 }
  
 class Attack
 {
     var Damage : int;  
 }
  
 class Movement
 {
     var moveSpeed : float;
     var normalSpeed : float;
 }
 var enemy = new Enemy();
 
 function Start () {
 
 }
 
 function Update () {
 
 }
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
0

Answer by Bryan-Legend · Oct 20, 2014 at 05:54 PM

I've added a feedback item to have attribute based categories added to the default inspector.

Please go here and vote for it:

http://feedback.unity3d.com/suggestions/built-in-inspector-should-use-system-dot-componentmodel-attributes-category-description-like-the-winforms-propertygrid

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Does anyone know of good tutorials for learning arrays and classes? 1 Answer

Inventory Help. 2 Answers

Calling variables in classes from other scripts 2 Answers

Array of custom class objects, possible? 1 Answer

Linking prefabs to classes that are not attatched to gameobjects 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