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 Minchuilla · May 29, 2014 at 06:51 PM · editorvariables

Adapt variables based on other variables... confusing

Simply, what I want to be able to do is this:

  • to be able to have other public variables depending on a variable

eg.

I have an enum with the types "normal" and "advanced"

 public enum Type {normal, advanced};

then if, whether the variable is set in code or in the inspector, the enum "Type" is "normal" for me to have another variable called "height", for example, or if the Tpye is set to advanced for me to have the public variable "speed"

For clarity, to have something like this(before the start function):

 public enum Type {normal, advanced};
 public Type type;
     
 if(type == Type.normal)
     public int height;
 else if(type == Type.advanced)
     public int speed;
 
 void Start()
 {
 //...

and for the inspector to look like this

alt text

or this

alt text

Any help or advice is greatly appreciated,

thanks in advance

Minchuilla

untitled1.jpg (8.9 kB)
untitled2.jpg (16.0 kB)
Comment
Add comment · Show 1
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 Lo0NuhtiK · May 29, 2014 at 07:12 PM 0
Share

http://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector

1 Reply

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

Answer by Chris_Dlala · May 29, 2014 at 09:23 PM

Hi, what you want is a Custom Inspector. These allow you to design exactly how the inspector should look and behave. They allow you to add most types of GUI controls with ease. I'll add a simple example for the behaviour you require:

 enter code hereusing UnityEngine;
 using System.Collections;
 
 public class MyScript : MonoBehaviour
 {
     public enum MyTypes { TypeOne, TypeTwo }
     public MyTypes myType;
     public int Height { get; set; }
     public int Width { get; set; }
 }

Given this class/script, the default inspector will draw the public enum myType but will not draw anything for Height and Width because they are Properties and Unity does not show properties in the inspector. I often do this so that my custom inspector has access to them but they are not drawn by default. Now here is a custom inspector for it:

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(MyScript))]
 public class MyScriptInspector : Editor
 {
     
     public override void OnInspectorGUI()
     {
         DrawDefaultInspector();
         MyScript myScript = target as MyScript;        
         
         
         switch(myScript.myType)
         {      
             case MyScript.MyTypes.TypeOne:
             myScript.Height = EditorGUILayout.IntField("Height" ,myScript.Height);
             break;
 
             case MyScript.MyTypes.TypeTwo:
             myScript.Width = EditorGUILayout.IntField("Width", myScript.Width);
             break;
         }
 
         if (GUI.changed)
         {
             EditorUtility.SetDirty(target);
         }
     }
 }

Most of the code here is pretty self explanatory. EditorGUILayout is your friend. Remember that the Editor script must be inside a folder called "Editor". I hope that helped =)

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 Minchuilla · Jun 04, 2014 at 03:15 PM 0
Share

Thanks, that exactly what I was looking for and has helped me make a lot of things easier.

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

22 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

Related Questions

Getting data from Editor into runtime 2 Answers

Problem with prefabs, help? 0 Answers

How to get a ONLY the INSPECTOR variables that can be modified. 0 Answers

Debug inspector not showing private varibles! + Header issues 1 Answer

Get variables from custom editor to script 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