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
5
Question by GlitchBait · Apr 13, 2013 at 01:59 PM · c#inspector

C# public - dropdown selection?

Hello all. I'm looking on how I can declare a public string in c# and it will offer a drop down list within the inspector. What can I google to look to see how it's done?

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

3 Replies

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

Answer by fafase · Apr 13, 2013 at 02:31 PM

A drop down list, do you mean a set of choice among a list?

You may want to use an enum then:

 public enum State{
    walking,idle,attacking,eating,sleeping
 }
 
 public class AClass:MonoBehaviour{
    public State current;
 }

This will show the variable with a list of possible choices

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 GlitchBait · Apr 13, 2013 at 05:31 PM 0
Share

That is exactly what I was needing. thank you!

avatar image aman_jha · Jul 15, 2014 at 08:54 PM 0
Share

How would I use this? $$anonymous$$eaning, how could I read this information in the script? If you could provide a link in the documentation that'd be good too because I can't find anything

avatar image fafase · Jul 16, 2014 at 10:34 AM 0
Share

http://www.dotnetperls.com/enum

avatar image
1

Answer by Aqibsadiq · Nov 20, 2017 at 09:16 AM

@fafase @GlitchBait

Here is a simpler way - Multiple Drop Down Selection

Simple 4 Steps

Step 1 : Make a new Script "EnumFlagsAttribute"

  using UnityEngine;
  using System.Collections;
  public class EnumFlagsAttribute : PropertyAttribute
   {
       public EnumFlagsAttribute() { }
   }

Step 2 : Make another Script "EnumFlagsAttributeDrawer"

 using UnityEngine;
  using System.Collections;
  using System;
  using UnityEditor;
  
  [CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
   public class EnumFlagsAttributeDrawer : PropertyDrawer
   {
       public override void OnGUI(Rect _position, SerializedProperty _property, GUIContent _label)
       {
           _property.intValue = EditorGUI.MaskField( _position, _label, _property.intValue, _property.enumNames );
       }
   }

Step 3: Enum Declaration

  [System.Flags]
      public enum FurnitureType
      {
          None , SofaPrefab , Curtains , Table , Chairs 
          }
  
   [EnumFlagsAttribute]
      public  FurnitureType   enumType;


Step 4: Getting Selected Elements

 List<int> ReturnSelectedElements()
      {
  
          List<int> selectedElements = new List<int>();
          for (int i = 0; i < System.Enum.GetValues(typeof(FurnitureType)).Length; i++)
          {
              int layer = 1 << i;
              if (((int) enumType & layer) != 0)
              {
                  selectedElements.Add(i);
              }
          }
  
          return selectedElements;
  
      }

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 Bonfire-Boy · Nov 20, 2017 at 10:10 AM 2
Share

"Simpler"?

avatar image Aqibsadiq Bonfire-Boy · Nov 27, 2017 at 10:58 AM 0
Share

@Bonfire-Boy Oops $$anonymous$$y Bad its a $$anonymous$$ultiple Drop Down Selection not just dropdown selection

avatar image
-1

Answer by khanbhai1992 · Apr 30, 2020 at 08:56 PM

There is another short trick for this

   public enum State{
         walking,idle,attacking,eating,sleeping
      }
      
      public class AClass:MonoBehaviour{
      public static string[] names = {"walking","idle","attacking","eating","sleeping"}
      public State current;
      public string getCurrentState(){
           return names[(int)current];
      }
     
      }

,

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 Bonfire-Boy · Apr 30, 2020 at 09:12 PM 1
Share

Several things bad about this, I'm afraid.

You never need to make your own list of names like that, and it's really flaky.

Using System.Enum.GetNames(typeof(State)) when you want an array of the names means you don't need to change the array every time you change the enum.

The only time you should make your own array like that is when you want to make a subset available, or want to specify a different ordering. But even then you're better off with an array of the enum type itself. That way it's typechecked and robust to rena$$anonymous$$g and reordering.

But in any case, if you want the current state as a string why not just do this ?

 public string getCurrentState()
 {
        return current.ToString();
 }

...if you really need a function for that, that is - most people would just use the ToString() as and when it's needed.

You're only making things harder for yourself here.

avatar image khanbhai1992 Bonfire-Boy · May 01, 2020 at 11:32 AM 0
Share

you are right @Bonfire-Boy. We can should use ToString() in this case. this style is used when have data array relative to enums as in case of script-able objects. Now you will say we can use dictionary in that case..

avatar image Bonfire-Boy khanbhai1992 · May 01, 2020 at 02:05 PM 0
Share

No I won't say that but I've no idea what you mean. ScriptableObjects makes no difference. Like I said, I can't think of any reason to make an array like that unless you want them sorted differently to the default, or only want to expose a subset of them.

And even then you should still use the enums themselves rather than hardcode their names as strings.

Sorry I know you're trying to be helpful, I'm only critiqueing because a noob could come across your code and think it makes sense. They should learn to use Enum.GetValues and Enum.GetNamesins$$anonymous$$d, and remember if you find yourself typing the name of an enum into a string then you're doing things a bad way.

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Multidimentional Array for Inventory style system in inspector. 1 Answer

Inspector cannot find script instance 1 Answer

c# enum wont show in inspector 3 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