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
1
Question by hmb3141 · Dec 14, 2014 at 12:48 AM · arrayinspectortagenumcustom-inspector

Creating enum using a string array

I'm trying to recreate the Tag dropdown menu.

It's possible to create a string array that contains all the Tags in the project using

         public string[] tagsStr = UnityEditorInternal.InternalEditorUtility.tags;
 

So say I have a public tags enum ,

 enum Tags {} 
 

How do I populate it with my tagsStr array? And will this need to be a custom inspector as I assume the Enum needs to be populated preenum runtime?

Comment
Add comment · Show 3
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 fafase · Dec 14, 2014 at 03:10 PM 0
Share

Could this be of help:http://stackoverflow.com/questions/13841880/convert-string-array-to-enum-on-the-fly

http://www.csharp-examples.net/string-to-enum/

avatar image Bunny83 fafase · Dec 14, 2014 at 03:22 PM 0
Share

Those only works if you have an enum with all those values already. $$anonymous$$ost on the first SO question didn't understand the question properly because the OP had an hardcoded enum in his question. However he said that he's getting the names from an API callback as string array, so he doesn't know what members the enum needs to have. Like i said in my answer, there's actually no point in creating an enum "on the fly" at runtime.

avatar image fafase · Dec 14, 2014 at 03:34 PM 0
Share

Yep I was trying to think of a way but I got stuck.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Dec 14, 2014 at 03:17 PM

No, that's not possible. An Enum need to be compiled at compile time and not at runtime. Even when it's possible to create an enum type with the members defined by the elements of a string array, you can't use the enum in your code since it doesn't exist at compile time.

At runtime an enum is simply a replacement for a certain numeric / primitive value (usually int32). So an enum like this:

 public enum MyEnum
 {
     Cat, Dog, Snake, Cow
 }

Just maps those names to integer values:

 MyEnum.Cat    == 0
 MyEnum.Dog    == 1
 MyEnum.Snake  == 2
 MyEnum.Cow    == 3

Unity's tags are actually string based so what's the point of creating an enum? If you want to display a dropdown for a tag selection in a custom editor, just use the EditorGUILayout.TagField or use the more general EditorGUILayout.Popup which takes the current index and a string array.

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 hmb3141 · Dec 14, 2014 at 03:42 PM 0
Share

Thanks, that's helped a lot.

I now how a dropdown allowing me to select one tag from all available tags.

I've had a look over the doco for both EditorGUILayout.TagField and EditorGUILayout.Popup, and it doesn't look like it's possible to select multiple items from the drop down at once. Are you aware of a way of achieving this?

 [CustomEditor(typeof(PointerColliderAction))]
 public class PointerColliderEditor : Editor
 {
     string[] tagStr = UnityEditorInternal.InternalEditorUtility.tags;
     int tagIndex = 0;
     
     public override void OnInspectorGUI ()
     {
         // Draw the default inspector
         DrawDefaultInspector();
         tagIndex = EditorGUILayout.Popup(tagIndex, tagStr);
         EditorUtility.SetDirty(target);
     }
 }
avatar image Bunny83 · Dec 14, 2014 at 03:59 PM 0
Share

@hmb3141: A GameObject can only have one tag at a time. It's not possible to select multiple tags. Even when you have an enum that wouldn't change any thing. An enum value also just has a single value. You can't use tags like layers as layers are implemented as a bitmask. Bitmasks are limited to the bit count of the underlying type. Usually 32 bits since and int (int32) as 32 bits. You can have more than 32 tags so there's no way to generally create a bit mask for tag values.

What's actually the point of that? As said: Only one tag can be used at a time.

edit
If you want the user to specify multiple tags you have to store them in an array. When you ask a question it's usually a good idea to describe your whole problem and not just ask a fraction of something you think could be a possible solution.

ps: Why do you call SetDirty every time OnInspectorGUI is called? That would invalidate the scene / asset all the time. You also doesn't seem to edit anything on your target object.

avatar image hmb3141 · Dec 14, 2014 at 04:39 PM 0
Share

What's actually the point of that? As said: Only one tag can be used at a time.

I'm creating a script that controls a crosshair. I want this script to alter the crosshair (change colour, animate, etc) depending on what my Raycast collides with. For example, check if it collides with an abject with tag "x" || "y" || "z"

I realise it's simpler to do this without accessing the Tags in the inspector at all, however being able to chose the tags from the inspector seemed like a nice addition. Also I thought it may be useful for other situations at some point. Also, I was just curios how it would work.

If you want the user to specify multiple tags you have to store them in an array.

Is this achievable with EditorGUILayout.Popup? Would I have to just have multiple dropdowns?

When you ask a question it's usually a good idea to describe your whole problem and not just ask a fraction of something you think could be a possible solution.

That's a good point, I'll keep this in $$anonymous$$d in future.

ps: Why do you call SetDirty every time OnInspectorGUI is called? That would invalidate the scene / asset all the time. You also doesn't seem to edit anything on your target object.

I just copied and pasted an example of EditorGUILayout.Popup that I found and then altered it to test whether the tags showed in the inspector .

Thanks for your help

avatar image
1

Answer by larex39 · May 07, 2020 at 07:23 AM

Here is youtube tutorial how to show List as Enum in inspector

https://www.youtube.com/watch?v=ThcSHbVh7xc

alt text


gif.gif (253.9 kB)
Comment
Add comment · 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
0

Answer by HolBol · Dec 14, 2014 at 12:50 AM

I believe you could simply do this:

 enum tags {
 
    tag0 = tagStr[0],
    tag1 = tagStr[1]
 
 }

Just replace tag0 etc with what you want each tag to be referred to when you call tags.whatever.

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 hmb3141 · Dec 14, 2014 at 02:56 PM 0
Share

Thanks for your suggestion. It makes sense.

However it's not ideal as

  • It requires me to manually name the enum values

  • I want the enum to be dynamic in that when a tag is added to tags, my tags enum will also be updated. I don't want to have to update my enum manually.

I'm trying to create a script which enables me to chose when to call a method based on which tags a ray cast collides with.

avatar image fafase · Dec 14, 2014 at 03:07 PM 1
Share

enum can only take integers.

avatar image Bunny83 · Dec 14, 2014 at 03:26 PM 0
Share

Yep only integers of those types:

 byte, sbyte, short, ushort, int, uint, long, ulong

However the values of an enum need to be a constant expression as it need to be known at compile time. You can't use a value stored in a variable.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

populate array values automatically in inspector 2 Answers

Making a user-friendly array with indices based on an enum 2 Answers

Rename array element name in inspector with enum 0 Answers

Custom Inspector & Arrays 0 Answers

Cutom Array Inspector in a Custom Inspector 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