Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Unsupported Enum Type for Long, Ulong or Int64

Discussion in 'Scripting' started by yusuf_isik, Jul 16, 2021.

  1. yusuf_isik

    yusuf_isik

    Joined:
    Feb 14, 2014
    Posts:
    21
    Hello everyone. I'm trying to use 64bit type for my enum declaration. Because I'm gonna need almost 60 unique type for my game. And also I'm gonna use bitwise operations between these types. But Unity isn't doing serialization on 64bit enums. It is supporting for ulong serialization, but it can't do serialization on 64bit enums. It's really weird issue.

    Here is my example code:
    Code (CSharp):
    1. using UnityEngine;
    2. [System.Flags]
    3. public enum FruitType: ulong
    4. {
    5.     None = 0,
    6.     Apple = 1,
    7.     Orange = 2,
    8.     Banana = 4,
    9.     Cherry = 8,
    10. };
    11.  
    12. public class Test : MonoBehaviour
    13. {
    14.     public ulong TestVariableForUlongSerializaton; // This is working.
    15.     public FruitType MyFruitType = FruitType.Cherry; // This is ulong based, but it isn't working.
    16. }
    After the compiling, the unity editor is giving unsupported type error.

    Do you have any idea about that problem and possible solutions?
     
    Last edited: Jul 16, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Do you need bitfields? Can something be both an apple, an orange, a banana and a cherry? Just use normal (ordinal integer) enums and you can handle over 2 billion fruit types with a standard enum.

    ALSO: beware that Unity serializes enums as integers. This means if you EVER renumber your enums, you will destroy all your hard work and not even realize it until you start getting weird bugs.

    It is always better to use ScriptableObjects to define things like this. Or at least use strings.

    Enums are bad in Unity3D if you intend them to be serialized:

    https://forum.unity.com/threads/bes...if-not-do-something-else.972093/#post-6323361

    https://forum.unity.com/threads/unity-card-game-structure.1006826/#post-6529526
     
    Joe-Censored and ADNCG like this.
  3. yusuf_isik

    yusuf_isik

    Joined:
    Feb 14, 2014
    Posts:
    21
    Thanks for kind answer, but as I said before. I'm gonna use bitwise operations on enums. Here is the another example to explain my problem deeply:

    Code (CSharp):
    1. using UnityEngine;
    2. [System.Flags]
    3. public enum ElementType: ulong
    4. {
    5.     None = 0,
    6.     Sword= 1,
    7.     Shield= 2,
    8.     Item = Sword | Shield,
    9.  
    10.     Banana = 4,
    11.     Cherry = 8,
    12.     Fruit= Banana | Cherry,
    13. };
    14. public class Test : MonoBehaviour
    15. {
    16.     public ulong TestVariableForUlongSerializaton; // This is working.
    17.     public ElementType MyElementType = ElementType.Cherry; // This is ulong based, but it isn't working.
    18.     public void Start()
    19.     {
    20.           if((MyElementType & ElementType.Fruit) > 0) //bitwise compare operations
    21.          {
    22.                Debug.Log("This a fruit");
    23.          }
    24.     }
    25. }
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    The problem is simply that Unity's enum serializer can only serialize 32 bit enums. You can do this - you just can't do it on a serialized field. You can pull it off with a custom editor script where you obfuscate the actual enum field behind a plain serialized ulong and a dropdown.
     
    Bunny83 likes this.
  5. yusuf_isik

    yusuf_isik

    Joined:
    Feb 14, 2014
    Posts:
    21
    Actually I have written a custom property drawer for this problem, but it is really annoying problem. Because there is a ulong serializer already but ulong enum isn't. That is weird, really weird problem. I hope, someone from developer team of Unity sees this thread, and add support for 64bit enums.

    And also I can't write a custom serializer for 64bit enum. Because it is the default system type of C#. If we would have been talking about custom class, I would be already written custom serializer or something for it.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Unity has known about this since at least 2015 https://forum.unity.com/threads/uns...tworking-types-networkid.359462/#post-2370283

    I wouldn't hold my breath for a fix anytime soon.

    Cast the enum to a ulong for serialization and back to your enum for use.
     
    Bunny83 and yusuf_isik like this.