Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by colinday · Aug 05, 2013 at 05:59 PM · inspectorserializationenumfieldinsert

Insert enum breaking existing inspector values

Enums are great, but unfortunately when used as fields that can be changed in the inspector they are completely dependent on the order of the enum in the code. I find it curious that Unity does not have an internal level of indirection for enum values from one script build to the next, so that you can insert new values anywhere into an enum without breaking all the existing fields that have values set.

Edit: It's also worth it to note that I would like the enum values to remain consecutive as the integer values will be bit packed tightly with other values during the runtime.

Is there a C# attribute or something that would help me here? Some other kind of field? Any way to make these more useful in the Unity inspector? I could totally write a custom inspector to do this, but writing a custom inspector for every single component that uses an enum doesn't seem like an efficient use of my time.

Here's some simple code just to illustrate exactly what I'm talking about

 public enum Foo
 {
     A,
     B,
     C
 }

Then you change it to

 public enum Foo
 {
     A,
     NewValue,
     B,
     C
 }

Thanks.

Comment
Add comment · Show 2
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 Loius · Aug 05, 2013 at 06:22 PM 1
Share

It might be possible to write a PropertyDrawer for your specific enum. Word of warning, though, I've been building up a giant generic Editor-GUI utility class and trying to genericize enums is some of the most hair-pullingly frustrating stuff. I dunno if that'll present a problem with a PropertyDrawer, but you'll probably need a separate PropertyDrawer for every enum you want to behave the right way, if you do go this route.

avatar image colinday · Aug 06, 2013 at 01:23 AM 0
Share

I didn't know about property drawers ... those are incredibly cool since it would save me from having to write a while editor, i can just make an editor for the single field.

Using the property drawer, the enum can be backed by a separate string field which is parsed by the runtime as the object is created so it is ready for use. I'm not wild about having to have an additional string field for everything using an enum, but the ref counted strings of C# make the additional space required not too terrible, even if used in a prefab that is instanced many many times.

2 Replies

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

Answer by robertbu · Aug 05, 2013 at 06:13 PM

You can explicitly set the enum values:

 public enum Foo {
     Dog   = 1,
     Snake = 3,
     Cat   = 7,
     Bird  = 5
 }
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 colinday · Aug 06, 2013 at 01:16 AM 0
Share

Thanks, this is a perfectly legit solution given the requirements I laid out. Of course, what I neglected to say is that I want to keep the enum values packed closely together as the integer value of it will be bit packed into another data structure during the runtime. I'll update the question ... but if others out there don't need consecutive values this will work for you.

avatar image Shadoninja · Jul 20, 2018 at 03:11 PM 0
Share

Awesome trick with enums. I have a list that is going to get huuuuge. If every alphabetical addition broke all editor references, I would have had to come up with a new solution.

avatar image
2

Answer by colinday · Aug 06, 2013 at 01:54 AM

Also adding Louis's answer here, I can't seem to vote up or mark a comment:

It might be possible to write a PropertyDrawer for your specific enum. Word of warning, though, I've been building up a giant generic Editor-GUI utility class and trying to genericize enums is some of the most hair-pullingly frustrating stuff. I dunno if that'll present a problem with a PropertyDrawer, but you'll probably need a separate PropertyDrawer for every enum you want to behave the right way, if you do go this route. -Louis

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

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

Is "enum x : UInt32" cast to "enum x : int" when serializing? 1 Answer

Array with multiple variables in Inspector 1 Answer

Accessible collection of Type 1 Answer

Difference between assigning a value in inspector and with a custom editor script 1 Answer

Why doesn't Unity support serializing composite types in the inspector? and how to work around it? 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