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
2
Question by Vel_1828 · Apr 05, 2015 at 01:14 PM · variablescustom editorcustom-inspector

Custom Editor - Show Variable if Boolean is True.

A simple question to Cutom Editor users.

I'm trying to organize my workflow. I've got three categories: A, B and C in one script (Overall settings, Primary Fire, Alternative Fire). Each category has about ~20 different variables in the inspector and most of the time I don't need to show all of them in the inspector.

What ritual should I perform to make a Custom Inspector, which shows different variables only when another variable is set True and hides them if False? For example:

 (if A == true)
     { 
     //Show variables 1-20 in the Inspector 
     }
 (if B == true)
     { 
     //Show variables 21-30 in the Inspector 
     }
 (if C == true)
     { 
     //Show variables 31-35 in the Inspector 
     }



Image Example: alt text

example.png (58.9 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

1 Reply

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

Answer by DoTA_KAMIKADzE · Apr 05, 2015 at 01:36 PM

Use Popup with a string array consisting of "A", "B", "C" and then check the returned value from it if "A" else if "B" else if "C"

Or if you can have all types at the same time then just utilize 3 Toggle

P.S. Here you have an updated answer for your updated question, something like this should work:

 [CustomEditor(typeof(ItsDaScriptDude))]
 public class ItsDaScriptDudeEditor : Editor
 {
     private bool AToggle = false;
     private ItsDaScriptDude _evCtrl = null;
     void OnEnable()
     {
         _evCtrl = (ItsDaScriptDude)target;
     }
     public override void OnInspectorGUI()
     {
         //blablabla
         GUILayout.BeginHorizontal();
         GUILayout.Label("A Features", GUILayout.Width(70));
         AToggle = EditorGUILayout.Toggle(AToggle);
         GUILayout.EndHorizontal();
         if (AToggle)
         {
             GUILayout.Space(5);
             GUILayout.BeginHorizontal();
             GUILayout.Label("This Var", GUILayout.Width(70));
             _evCtrl.ThisVar = EditorGUILayout.TextField(_evCtrl.ThisVar);
             GUILayout.EndHorizontal();
             GUILayout.Space(5);
             GUILayout.BeginHorizontal();
             GUILayout.Label("And This One", GUILayout.Width(70));
             _evCtrl.AndThisOne = EditorGUILayout.TextField(_evCtrl.AndThisOne);
             GUILayout.EndHorizontal();
             GUILayout.Space(5);
             GUILayout.BeginHorizontal();
             GUILayout.Label("And This Can Be Slider", GUILayout.Width(70));
             _evCtrl.AndThisCanBeSlider = EditorGUILayout.Slider(_evCtrl.AndThisCanBeSlider, 0f, 100f);
             GUILayout.EndHorizontal();
         }
         //blablabla
     }
 }

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 Vel_1828 · Apr 05, 2015 at 02:16 PM 0
Share

Ha, great find! The Toggle option almost answers my question! It gives the answer to "How to make "(if A == true)" part, so 50% done. But what exact command can show or hide the variables in each category? :)

avatar image DoTA_KAMIKADzE · Apr 05, 2015 at 03:03 PM 0
Share

There is no command to show/hide, Inspector "refreshes" every time something changes, so think about it like Update() function works for game, it just iterates trough that OnInspectorGUI() over and over again so if code does not reach some controls because your "if" was not true then that part of code will just not run - thus everything under that if statement will run only if that if is true.

P.S. Just in case I got you wrong and if you meant which one to use to show variables, then just FIND HERE anything that suits you more, for example:

 EditorGUILayout.ObjectField
 EditorGUILayout.TextField
avatar image Vel_1828 · Apr 05, 2015 at 05:29 PM 0
Share

Just to be exact - updated the First Post with an image of what I'm trying to do in the Inspector.

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

How to handle Serializable classes in a CustomEditor? 0 Answers

Initializing 2D array via inspector 3 Answers

Scrollbar in Inspector 1 Answer

Handles not displaying 1 Answer

C# unity custom editor multiple different components but same base class 2 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