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

Bug EnumField does not work for custom Enum type

Discussion in 'UI Toolkit' started by Kichang-Kim, May 15, 2019.

  1. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    979
    Hi, I found that my own enum type does not work in EnumField in UXML.

    Here is minimul reproducible code:
    TestWindow.uxml
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <UXML
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns="UnityEngine.UIElements"
    5.     xmlns:uie="UnityEditor.UIElements"
    6.  
    7. xsi:noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd"
    8. xsi:schemaLocation="
    9.                        UnityEngine.UIElements ../../UIElementsSchema/UnityEngine.UIElements.xsd
    10.                        UnityEditor.UIElements ../../UIElementsSchema/UnityEditor.UIElements.xsd
    11.                        UnityEditor.PackageManager.UI ../../UIElementsSchema/UnityEditor.PackageManager.UI.xsd
    12. "
    13. >
    14.     <Foldout name="foldout" class="foldout">
    15.         <uie:EnumField label="BuildTarget" type="UnityEditor.BuildTarget" />
    16.         <uie:EnumField label="MyEnum" type="MyNamespace.MyEnum" />
    17.     </Foldout>
    18. </UXML>
    MyEnum.cs
    Code (CSharp):
    1. namespace MyNamespace
    2. {
    3.     public enum MyEnum
    4.     {
    5.         None = 0,
    6.         First = 1,
    7.         Second = 2,
    8.     }
    9. }
    10.  
    First EnumField for BuildTarget works correctly, but second EnumField for my own Enum types doesn't work. Is this bug? or intended behaviour?

    Thanks.
     
  2. patrickf

    patrickf

    Unity Technologies

    Joined:
    Oct 24, 2016
    Posts:
    57
    Hi! Could you try providing the assembly name, like this:

    <uie:EnumField label="MyEnum" type="MyNamespace.MyEnum, AssemblyName" />
     
  3. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    979
    Works. Thanks!
     
  4. KosaJr

    KosaJr

    Joined:
    Jan 3, 2017
    Posts:
    10
    Sorry for digging, but I'm working on custom editor script now, and I'm wondering if there's a possibility to create a EnumField directly in .uxml file?
    I've tried this:
    Code (CSharp):
    1. <UXML xmlns="UnityEngine.UIElements">
    2.     <Label name="labelowe" text="tempText"/>
    3.     <EnumField label="Test" type="UIElementsExamples.SomeEnum, Assembly-CSharp-Editor" />
    4. </UXML>
    where type in "UIElementsExamples" namespace:
    Code (CSharp):
    1. public enum SomeEnum { X, Y, Z, Makelele }
    but gets error: upload_2019-7-17_14-18-48.png in console and upload_2019-7-17_14-20-29.png in custom editor.
    EditorWindow code:
    Code (CSharp):
    1.         private void OnEnable()
    2.         {
    3.             SetupEditorWindow();
    4.         }
    5.  
    6.         private void SetupEditorWindow()
    7.         {
    8.             var visualTreeToClone = Resources.Load<VisualTreeAsset>("kjrUXML");
    9.             visualTreeToClone.CloneTree(rootVisualElement);
    10.         }
    == SOLVED ==
    The problem was in missing [xmlns:ue="UnityEditor.UIElements"], which is used by EnumField (not "UnityEngine.UIElements").
     
    Last edited: Jul 17, 2019
    Lost-Shadow likes this.
  5. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    979
    @patrickf Hi, I found that specifying type for EnumField in UXML doesn't work for nested type. like this:
    Code (CSharp):
    1. namespace MyNamespace
    2. {
    3.      public class MyClass
    4.     {
    5.         public eum MyEnum{}
    6.     }
    7. }
    MyNamespace.MyClass.MyEnum doesn't work. If I move MyEnum to the outside of MyClass, it works. Is this intended behaviour? and how to specify nested types in UXML?

    Thanks.
     
    PhilWilson likes this.
  6. patrickf

    patrickf

    Unity Technologies

    Joined:
    Oct 24, 2016
    Posts:
    57
  7. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    256
    I have the same issue that an enum field now doesn't work anymore for my editor scripts. Which before was the easiest task, now seems to be a massive pain in the ass, plus a nightmare to debug if the type string / namespace changes, which happens quite often, mostly in the early stages of a project.

    I use the UI Builder to check UI Toolkit out for the first time, the first field is an enum and my world falls apart already. Here is the enum definition:
    Bildschirmfoto 2021-03-21 um 17.28.16.png

    Here is my class that contains the enum:
    Code (CSharp):
    1. namespace Source.Components.Common
    2. {
    3.     [RequireComponent(typeof(Image))]
    4.     public class ImageFX : MonoBehaviour
    5.     {
    6.         public enum Effect
    7.         {
    8.             Glow,
    9.             Flash,
    10.             Shine,
    11.             GlowSoftMask,
    12.             FlashSoftMask,
    13.         }
    14.         [...]
    15.     }
    16. }
    I get a popup type attribute invalid, make sure to include assembly name.
    I tried Source.Components.Common.ImageFX.Effect as-well.

    The generated UXML window shows as my new editor layout, but the enum field has no values.

    PS: I also tried to fill the enum popup via code, which would be fine for me, but this doesn't seem to work either:
    Code (CSharp):
    1.     [CustomEditor(typeof(ImageFX))]
    2.     public class ImageFxEditor : UnityEditor.Editor
    3.     {
    4.         public void OnEnable()
    5.         {
    6.             VisualTreeAsset visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Source/Components/Common/Editor/ImageFX.uxml");
    7.             var ui = visualTree.Instantiate();
    8.             var enumField = ui.Q<EnumField>("effect");
    9.             enumField.Init(ImageFX.Effect.Glow);
    10.         }
    11.     }
    I feel like this should at least work, but it doesn't somehow..
     
    Last edited: Mar 21, 2021
    BG_Grill likes this.
  8. julio_kolbapps

    julio_kolbapps

    Joined:
    Mar 2, 2021
    Posts:
    32
    For you and for someone that need. You need to create a assembly definition of the your project. Take a look at some AssemblyDefinition tutorial.

    In My case, i created a folder for my UIElement project, called "GMB", (Game Manager BackEnd).

    At my GMB folder project i have scripts for behaviour and scripts only for editor, so i have a folder for Editor too.

    I created a Assembly Definition in root of my GMB Folder and other in Root of Editor Folder of the my GMB Folder.

    In my Assembly Definition of Editor, i have a assemblyDefinition Reference, that is my GMB Assembly definition, and it is set to only Editor plataform. So now, my ENUM work very fine, because i can use my GMB Assembly name, that contains my custom enum.

    My Custom enum path is: GMB.Data_ItemIngredient.RequireIngredientType.
    My Assembly name is GMB.
    BUT, Keep in mind that i use namespace GMB for all my project, so this is my assembly name, and i use GMBEditor namespace for all only editor scripts... Thats is it!

      <uie:EnumField label="Enum" type="GMB.Data_ItemIngredient+RequireIngredientType, GMB"/> 
     

    Attached Files:

    Last edited: Mar 4, 2022
  9. Perengano

    Perengano

    Joined:
    Feb 15, 2017
    Posts:
    2
    I don't know if this is what you are looking for but in cs the EnumField object has a method "Init(EnumValue)"
     
    Dlalsdud and JoseLuisAL like this.