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 fre_ber · May 13, 2021 at 07:54 AM · inspectorserializationenum

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

I have an enum defined like this:

 public enum MessageId : UInt32
 {
     StartPingCheck               = 0x00000001,
     CompletePingCheck            = 0x00000002,
     AgentUpdate                  = 0x00000004,

     CoarseLocationUpdate         = 0x0000ff06,
     AttachedSound                = 0x0000ff0d,

     Wrapper                      = 0xffff0001,
     UseCircuitCode               = 0xffff0003
 }

When using a value of this type in the inspector, I see all the values in the popup, but I can't select any of the values with the top bit set, in this case "Wrapper" and "UseCircuitCode". When selecting any of these, the value in the editor becomes blank.

Is this a bug, by design or am I doing something wrong?

EDIT: The actual value becomes zero when selecting the large values.

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
3
Best Answer

Answer by Namey5 · May 14, 2021 at 12:55 PM

This would make sense as I would imagine Unity stores all enums as though they are flags, with the implicit 'Everything' field always set to -1. The SerializedProperty only has value properties for intValue and longValue as well - and according to the docs, uint values will be set via the intValue field (and therefore implicitly cast to int32 for some reason). To get around this, you can make a custom PropertyAttribute and PropertyDrawer that uses 'property.longValue' instead (as long as you don't need ulong);

 using System;
 using UnityEngine;
 
 public class UIntEnumAttribute : PropertyAttribute
 {
     public Type type;
     public bool flags;
 
     public UIntEnumAttribute (Type a_Type, bool a_Flags = false)
     {
         type = a_Type;
         flags = a_Flags;
     }
 }

PropertyDrawer (in an Editor folder):

 using System;
 using UnityEngine;
 using UnityEditor;
 
 [CustomPropertyDrawer (typeof (UIntEnumAttribute))]
 public class UIntEnumDrawer : PropertyDrawer
 {
     public UIntEnumAttribute EnumAttribute => (UIntEnumAttribute)attribute;
 
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
     {
         Enum value = (Enum)Enum.ToObject (EnumAttribute.type, (uint)property.longValue);
         if (EnumAttribute.flags)
         {
             value = EditorGUI.EnumFlagsField (position, label, value);
         }
         else
         {
             value = EditorGUI.EnumPopup (position, label, value);
         }
 
         property.longValue = (uint)Enum.ToObject (EnumAttribute.type, value);
     }
 }

Usage example:

 public enum MessageId : uint
 {
     StartPingCheck = 0x00000001,
     CompletePingCheck = 0x00000002,
     AgentUpdate = 0x00000004,
     CoarseLocationUpdate = 0x0000ff06,
     AttachedSound = 0x0000ff0d,
     Wrapper = 0xffff0001,
     UseCircuitCode = 0xffff0003
 }
 
 [SerializeField, UIntEnum (typeof (MessageId))] private MessageId m_MessageID = MessageId.StartPingCheck;
Comment
Add comment · Show 1 · 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 fre_ber · May 14, 2021 at 04:00 PM 0
Share

You are a star! Not only did you answer the question, you also provided a working solution to the issue caused by it! Thank you very much!

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

130 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

Related Questions

Insert enum breaking existing inspector values 2 Answers

Instantiate GameObject on Enum Selection from Inspector 1 Answer

Inspector - Show/hide property within custom class using enum 1 Answer

Unity types unable to be serialized 2 Answers

Exposing Enums in 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