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
7
Question by JumpDistrict · Mar 15, 2013 at 11:29 PM · inspectorvariableschangeenum

Change Inspector Variables Depending On Enum

Hello UnityAnswers,

How would on go through in editing/changing the inspector variables depending on one variable that's an enum.

EXAMPLE: There's an enum variable called "Task" which has the options "Guard", "Patrol", or "Off-Duty".

If I select "Guard" different variables will show in the inspector specific to the guard-task. And then same for "Patrol" and "Off-Duty"

How would I go about in doing this?

Thanks in advance

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
25
Best Answer

Answer by Chronos-L · Mar 16, 2013 at 04:16 AM

You will have to write your own editor. Unity Custom Editor Reference

In the OnInspectorGUI(), you can use a switch() or if-else to determine what inspector variable you want to show.

Example

 using UnityEngine;
 using System.Collections;
 
 public class PropertyHolder : MonoBehaviour {
    public enum Status { A, B, C };
     
    public Status state;
     
    public int valForAB;
     
    public int valForA;
    public int valForC;
     
    public bool controllable;
     
    void Start () {
     
    }
     
    void Update () {
     
    }
 }
 



Put this in Assets/Editor

 using UnityEditor;
 using UnityEngine;
 
 [CustomEditor(typeof(PropertyHolder)), CanEditMultipleObjects]
 public class PropertyHolderEditor : Editor {
     
     public SerializedProperty 
         state_Prop,
         valForAB_Prop,
         valForA_Prop,
         valForC_Prop,
         controllable_Prop;
     
     void OnEnable () {
         // Setup the SerializedProperties
         state_Prop = serializedObject.FindProperty ("state");
         valForAB_Prop = serializedObject.FindProperty("valForAB");
         valForA_Prop = serializedObject.FindProperty ("valForA");
         valForC_Prop = serializedObject.FindProperty ("valForC");
         controllable_Prop = serializedObject.FindProperty ("controllable");        
     }
     
     public override void OnInspectorGUI() {
         serializedObject.Update ();
         
         EditorGUILayout.PropertyField( state_Prop );
         
         PropertyHolder.Status st = (PropertyHolder.Status)state_Prop.enumValueIndex;
         
         switch( st ) {
         case PropertyHolder.Status.A:            
             EditorGUILayout.PropertyField( controllable_Prop, new GUIContent("controllable") );            
             EditorGUILayout.IntSlider ( valForA_Prop, 0, 10, new GUIContent("valForA") );
             EditorGUILayout.IntSlider ( valForAB_Prop, 0, 100, new GUIContent("valForAB") );
             break;
 
         case PropertyHolder.Status.B:            
             EditorGUILayout.PropertyField( controllable_Prop, new GUIContent("controllable") );    
             EditorGUILayout.IntSlider ( valForAB_Prop, 0, 100, new GUIContent("valForAB") );
             break;
 
         case PropertyHolder.Status.C:            
             EditorGUILayout.PropertyField( controllable_Prop, new GUIContent("controllable") );    
             EditorGUILayout.IntSlider ( valForC_Prop, 0, 100, new GUIContent("valForC") );
             break;
             
         }
         
         
         serializedObject.ApplyModifiedProperties ();
     }
 }
 

Comment
Add comment · Show 5 · 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 JumpDistrict · Mar 16, 2013 at 07:42 PM 0
Share

Wow, great! Thanks a lot!

avatar image Chronos-L · Mar 17, 2013 at 12:51 AM 1
Share

I am glad that I could help.

avatar image Denis Goroshkov · Jul 04, 2013 at 10:09 AM 4
Share

Some edit: It's work fine for default enumerations like:

enum DefaultEnumSample{ kWood, kSteel, kStone }

what is equal to:

enum DefaultEnumSample{ kWood = 0, kSteel = 1, kStone = 2 }

But if you use enumerator like:

enum WeaponSampleEnum{ kNone = -1, kHand = 0, kSword = 10, kBigSword = 11, k$$anonymous$$ace = 20 }

then this method doesn't work.

Solution:

just change

PropertyHolder.Status st = (PropertyHolder.Status)state_Prop.enumValueIndex;

to:

PropertyHolder.Status st = (PropertyHolder.Status)state_Prop.intValue;

avatar image merpheus Denis Goroshkov · Nov 30, 2018 at 12:53 PM 0
Share

Sir you saved my day. Thanks a lot!

avatar image SomeRandomGuy · Oct 16, 2013 at 11:34 AM 0
Share

Works great, just one question, how would you go about adding color fields and object fields to this? Say I want valForAB to be an object field, how would I do this? same thing for color. I seem to be getting errors because the serializedproperties are not actually object/color types. Should I just not use serializedproperty for those?

avatar image
0

Answer by awesomeface · May 12, 2015 at 02:26 AM

For that answer's code, is it possible to have an intfield rather than an intslider?

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 deCalle · Feb 23, 2016 at 03:44 PM 0
Share

yeah, sure, go ahead ;-)

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

21 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

Related Questions

Save previous Enum variable in Update 1 Answer

if statement inside variables of a class 1 Answer

Array with multiple variables in Inspector 1 Answer

Enum drop down menu in inspector for nested arrays 2 Answers

Public class variables not displaying in the inspector 4 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