Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Krish-Vikram · Dec 03, 2016 at 11:52 AM · inspectoreditor-scripting

Editor: How to display properties in inspector based on enum in same script ?

I have a Enum say playerType{X,Y,Z}. i want the properties of the script to be displayed based on the type of the player. Similar to "Image Type" of Image component. how to write editor script for this ?

For ex: if type= X; string Xname; Gameobject Xaddon;

if type=y; string Yname; Gameobject Yaddon;

if type=z; string Zname; Zbehaviour Ztype; (Say Zbehaviour is some enum)

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

Answer by Bunny83 · Dec 03, 2016 at 01:37 PM

First of all you should understand that a custom inspector can only change how the properties are displayed. They all still need to exist in the actual class. In your case you can reuse certain variables since only one "type" is valid at a time.

For example:

 public class MyTest : MonoBehavioud
 {
     public PlayerType type;
     public string objName;  // shared between type X, Y and Z
     public GameObject addon; // shared between type X and Y
     public ZBehaviour zType;
 }

In the custom inspector you then can show different GUI depending on the set type:

 [CustomEditor(typeof(MyTest))]
 public class MyTestEditor : Editor
 {
     MyTest mTest;
     void OnEnable()
     {
         mTest = (MyTest)target;
     }
     public override void OnInspectorGUI()
     {
         mTest.type = (PlayerType)EditorGUILayout.EnumPopup("PlayerType", mTest.type);
         switch(mTest.type)
         {
             case PlayerType.X:
                 {
                     mTest.objName = EditorGUILayout.TextField("Name", mTest.objName);
                     mTest.addon = (GameObject)EditorGUILayout.ObjectField("Addon", mTest.addon, typeof(GameObject), true);
                     break;
                 }
             case PlayerType.Y:
                 {
                     mTest.objName = EditorGUILayout.TextField("Name", mTest.objName);
                     mTest.addon = (GameObject)EditorGUILayout.ObjectField("Addon", mTest.addon, typeof(GameObject), true);
                     break;
                 }
             case PlayerType.Z:
                 {
                     mTest.objName = EditorGUILayout.TextField("Name", mTest.objName);
                     mTest.zType = (ZBehaviour)EditorGUILayout.EnumPopup("ZType", mTest.zType);
                     break;
                 }
         }
     }
 }

Note that you could use seperate variables for your name and addon variables for each type but it wouldn't make much sense if you want to switch between the different types and only one type is in use at a time.

ps: Keep in mind that a MonoBehaviour already has a "name" property so i renamed the variable to "objName".

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 Krish-Vikram · Dec 03, 2016 at 03:01 PM 0
Share

Thank you... It Works.

avatar image BradyIrv · May 15, 2020 at 01:34 AM 0
Share

Is it possible to do this with a Scriptable Object? I've had no success trying to replicate with a Scriptable.

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

64 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 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 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

Custom classes, inheritance and the inpector 0 Answers

Custom Inspector Enum 1 Answer

get editor camera without focus on the scene 0 Answers

Custom ordering of variables in inspector when using base and derived classes 1 Answer

Help with editor serialization 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