• 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 /
  • Help Room /
avatar image
Question by JesusArroyo · Jan 12, 2019 at 11:51 PM · inspectorenumrestrictenumeraterestrictions

How to limit the types shown at the inspector in a public enum based on another public enum type selected?

I´m making a Turn Based "Mario RPG style" game and have created a script to contain all the special abilities for each character.


Each character has a CharacterScript and a SpecialAbilityScript. Since all the characters, friends or foes, have the same basic properties and methods, the only thing that the do different uppon called is their "Special Ability", which could go from healing a teammate, giving an oponent some long lasting damage for several turns, paralyzing an oponent, increasing a teammates or self attack power, etc...


The way I figured how to make this work, is by dividing and grouping the Special Abilities into groups using enumerations:

 public AbilityType abilityType;
 public AbilitySubType abilitySubType;
 public TargetType targetType;
 
         public enum AbilityType { Support, Offense }
         public enum AbilitySubType{ Bless, Help, Curse, Damage }
         public enum TargetType { Self, Teammate, Team, Enemy, Enemies, All }

However, I want to restrict the AbilitySubType types shown at the Inspector, based on which AbilityType type is chosen before.

So if, for example, The abilityType type selected at the inspector is Support, then The abilitySubType only shows types Bless and Help.


Is there a way to achieve this at the Inspector using enums? should I use something else instead? or is my approach poorly addressed?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by JesusArroyo · Jan 15, 2019 at 05:37 AM

SOLVED: So I found a way to achieve this very quick and easy using custom Inspectors, and consider my problem solved.


TL;DR: I used custom inspector to create toolbar that shows/hide another toolbar and change the types by just pressing them.


Solution:

I created a toolbar with two options: (Support | Offense), if you click Support, a function called TypeSupportSelected() is called and within it I set the AbilityType type to Support, and creates a second toolbar (Bless | Help). Selecting Bless sets the AbilitySubType type to Bless, selecting Help sets it respectively to type Help. This way I don´t have to worry about mixing the incorrect settings (e.g. setting AbilityType to Support and AbilitySubType to Curse)


However, for anyone who has little or null knowledge of using Custom Inspectors and just wants a quick solution for a similar problem, Here is an example:


This is the script that has the enumerations I want to restrict accordingly:

 using UnityEngine;
 
 public class MyScriptWithTheEnum : MonoBehaviour
 {
 
     public AbilityType abilityType;
     public AbilitySubType abilitySubType;
     public TargetType targetType;
 
     public enum AbilityType { Support, Offense }
     public enum AbilitySubType { Bless, Help, Curse, Damage }
     public enum TargetType { Self, Teammate, Team, Enemy, Enemies, All }
 }

So, for example, if you set the Ability Type to Support, You dont want to be able to set the Ability Sub Type to either Curse nor Damage.

alt text

And The way I solved it, was by creating a Custom Inspector script. For those unfamiliar with that term: It´s a script that helps you customize the way another script displays its properties at the inspector window (e.g. so you can have colorized buttons and customized labels at the inspector tab, etc...) This Custom Inspector Script you have to put it in a Folder named Editor within the Assets Folder.


The Custom Inspector Script I called MyScriptWithTheEnumEditor. It has to have the namespace "Using UnityEditor" and change the inheritance from MonoBehaviour to Editor

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(MyScriptWithTheEnum))]//inside Typeof you put the name of the script you want to modify with this custom inspector
 public class MyScriptWithTheEnumEditor : Editor
 {
     //These are the toolbar buttons you are going to see at the inspector
     private string[] _toolbarAbilityType = new string[2] { "Support", "Offense" };
     private string[] _toolbarAbilitySubTypeSupport = new string[2] { "Bless", "Help" };
     private string[] _toolbarAbilitySubTypeDamage = new string[2] { "Curse", "Damage" };
 
     //these are to hold the current selection from the toolbars mentioned above
     private int _currentToolbarType;
     private int _currentToolbarSubType;
 
     //this works as a direct reference to the variables at your script with the Enums
     private MyScriptWithTheEnum _myScriptWithEnum;
 
     
     private void OnEnable()
     {
         _myScriptWithEnum = base.target as MyScriptWithTheEnum;
     }
 
 
     //This will "re-write" the way your script looks at the inspector tab
     public override void OnInspectorGUI()
     {
         ShowMyCustomMagicSpellAssigner();//this is were the magic happens, it shows the customized version of the enums
         EditorGUILayout.HelpBox("default base values showed above this message", MessageType.Info);
         base.OnInspectorGUI();//this is the default way your script is showed at the inspector, leave it so you can see the changes happening in real-time
     }
 
     
     public void ShowMyCustomMagicSpellAssigner()
     {
         EditorGUILayout.LabelField("    Magic Spell Properties", EditorStyles.boldLabel);
         GUILayout.Space(5);
         GUILayout.Label("Type: ");
         _currentToolbarType = GUILayout.Toolbar(_currentToolbarType, _toolbarAbilityType);//this will display a toolbar with two options: (SUPPORT | OFFENSE)
         if (_currentToolbarType == 0) { TypeSupportSelected(); }
         if (_currentToolbarType == 1) { TypeAttackSelected(); }
     }
 
     void TypeSupportSelected()//If you click the SUPPORT option, this new toolbar will be created right beneath the (SUPPORT | OFFENSE) toolbar
     {
         GUILayout.Label("Sub Type: ");
         _currentToolbarSubType = GUILayout.Toolbar(_currentToolbarSubType, _toolbarAbilitySubTypeSupport);//this will display a toolbar with two options: (BLESS | HELP)
         _myScriptWithEnum.abilityType = MyScriptWithTheEnum.AbilityType.Support;//IMPORTANT: This sets the Ability Type to Support at the original script
         if (_currentToolbarSubType == 0) { SubTypeBlessSelected(); }
         if (_currentToolbarSubType == 1) { SubTypeHelpSelected(); }
     }
 
     void TypeAttackSelected()//If you click the OFFENSE option, this new toolbar will be created right beneath the (SUPPORT | OFFENSE) toolbar
     {
         GUILayout.Label("Sub Type: ");
         _currentToolbarSubType = GUILayout.Toolbar(_currentToolbarSubType, _toolbarAbilitySubTypeDamage);//this will display a toolbar with two options: (CURSE | DAMAGE)
         _myScriptWithEnum.abilityType = MyScriptWithTheEnum.AbilityType.Offense;//IMPORTANT: This sets the Ability Type to Offense at the original script
         if (_currentToolbarSubType == 0) { SubTypeCurseSelected(); }
         if (_currentToolbarSubType == 1) { SubTypeDamageSelected(); }
     }
 
     //IMPORTANT: This all set the Ability Sub Types at the original script
     void SubTypeBlessSelected()
     {
         _myScriptWithEnum.abilitySubType = MyScriptWithTheEnum.AbilitySubType.Bless;
     }
 
     void SubTypeHelpSelected()
     {
         _myScriptWithEnum.abilitySubType = MyScriptWithTheEnum.AbilitySubType.Help;
     }
 
     void SubTypeCurseSelected()
     {
         _myScriptWithEnum.abilitySubType = MyScriptWithTheEnum.AbilitySubType.Curse;
     }
 
     void SubTypeDamageSelected()
     {
         _myScriptWithEnum.abilitySubType = MyScriptWithTheEnum.AbilitySubType.Damage;
     }
 }

And now this is how it works and looks (Ignore the mouse icon not showing up, or pretend my monitor is touchscreen): alt text


you can test it very easily by just copying these two scripts and setting them up as explained above in a new project. Hope it helps whoever runs across this problem.


enumexample2.jpg (35.2 kB)
enumgifexample.gif (166.7 kB)
Comment
Hellium
Kuro4027

People who like this

2 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

unable to add new categories to enum 1 Answer

Custom enumeration class accessible through inspector? 1 Answer

check enum from another script? 1 Answer

Manage lots of enum choices in inspector 1 Answer

Interfaces vs methods selection using if(enumerations) 0 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