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
0
Question by infinitypbr · Sep 27, 2013 at 11:42 PM · inspectorenum

[Inspector] Populate Enum Options with List of Files?

I'd like to populate the options in an enum list based on the files inside a folder.

Here's the current code:

 enum Attacks {Option1, Option2, Option3}
 var attackChoice : Attacks;

Instead of hard-coding the options, I want the list to include every Prefab I've created inside my "Attacks" folder. That way, as I create new "Attack Prefabs", they are added to the list for selection.

Is this possible?

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
0

Answer by Jamora · Sep 28, 2013 at 10:58 AM

Modifying the contents of an enum is not possible during runtime. Doing that would defeat the whole purpose of an enum, which is to list - or enumerate - every possible option available at any given time during runtime.

What you can do, is read the contents of your folder, then add those files to a list or other data structure, whose contents you then show in your inspector.

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 Hoeloe · Sep 28, 2013 at 11:06 AM 1
Share

Just to add to this - Enums are simply a compile-time construction. They exist to aid the programmer in making code more readable, by assigning names to numerical values, and limiting the range of available numbers. If you have an enum the same as your current one (with 3 options), writing Attacks.Option1 is functionally equivalent to using the integer value 0 (and in fact you can cast between these two types to get/use these values). When you compile the code, it removes the enumeration and replaces it with just the numbers (in simple terms, anyway), so changing the enum at runtime makes no sense, as there isn't really anything there to change.

avatar image infinitypbr · Sep 28, 2013 at 05:04 PM 0
Share

The purpose would be to make it just a bit easier for me or other programmers working with the scripts. The prefab-list would be a list of available particles, or perhaps a list of available weapons, so rather than having to click/select or drag the prefab into the field, the programmer would just select from the list, and when a new weapon is added to the list, it's also available in the selection list. So it's not intended to make any real changes in the game, just in the inspector while building the game.

avatar image Hoeloe · Sep 28, 2013 at 05:07 PM 1
Share

You can do this in other ways, without using enums, as it is literally impossible to alter an enum beyond compile time (which is what you're trying to do - edit mode is still in runtime). You can search through specific folders and create a list of prefabs that way, then create a custom dropdown box, which you can populate with members of this list. Enums are a static construct, and not useful for dynamic data. That's what data structures like lists are for.

avatar image infinitypbr · Sep 28, 2013 at 05:26 PM 0
Share

Do you know what phrase I should be searching for to learn about that? $$anonymous$$y googling hasn't given me anything to go on that I know of.

avatar image Hoeloe · Sep 28, 2013 at 05:35 PM 1
Share

You should look it up in several parts. Firstly is using a list or array as the elements in a Unity dropdown menu. Second is searching for all prefab files in a specified folder (or path). Third is looking up generic data structures such as List, LinkedList, Set and Dictionary.

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

17 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

Related Questions

Instantiate GameObject on Enum Selection from Inspector 1 Answer

Getting the selected enum value set in the Inspector 2 Answers

populate array values automatically in inspector 2 Answers

Flagged Enum representation in Inspector 1 Answer

Problem with second enum selection inspector 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