EnumField does not work for custom Enum type

Hi, I found that my own enum type does not work in EnumField in UXML.

Here is minimul reproducible code:
TestWindow.uxml

<?xml version="1.0" encoding="utf-8"?>
<UXML
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="UnityEngine.UIElements"
    xmlns:uie="UnityEditor.UIElements"

xsi:noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd"
xsi:schemaLocation="
                        UnityEngine.UIElements ../../UIElementsSchema/UnityEngine.UIElements.xsd
                        UnityEditor.UIElements ../../UIElementsSchema/UnityEditor.UIElements.xsd
                        UnityEditor.PackageManager.UI ../../UIElementsSchema/UnityEditor.PackageManager.UI.xsd
"
>
    <Foldout name="foldout" class="foldout">
        <uie:EnumField label="BuildTarget" type="UnityEditor.BuildTarget" />
        <uie:EnumField label="MyEnum" type="MyNamespace.MyEnum" />
    </Foldout>
</UXML>

MyEnum.cs

namespace MyNamespace
{
    public enum MyEnum
    {
        None = 0,
        First = 1,
        Second = 2,
    }
}

First EnumField for BuildTarget works correctly, but second EnumField for my own Enum types doesn’t work. Is this bug? or intended behaviour?

Thanks.

Hi! Could you try providing the assembly name, like this:

<uie:EnumField label="MyEnum" type="MyNamespace.MyEnum, AssemblyName" />

Works. Thanks!

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:

<UXML xmlns="UnityEngine.UIElements">
    <Label name="labelowe" text="tempText"/>
    <EnumField label="Test" type="UIElementsExamples.SomeEnum, Assembly-CSharp-Editor" />
</UXML>

where type in “UIElementsExamples” namespace:

public enum SomeEnum { X, Y, Z, Makelele }

but gets error: 4756139--451676--upload_2019-7-17_14-18-48.png in console and 4756139--451679--upload_2019-7-17_14-20-29.png in custom editor.
EditorWindow code:

        private void OnEnable()
        {
            SetupEditorWindow();
        }

        private void SetupEditorWindow()
        {
            var visualTreeToClone = Resources.Load<VisualTreeAsset>("kjrUXML");
            visualTreeToClone.CloneTree(rootVisualElement);
        }

== SOLVED ==
The problem was in missing [xmlns:ue=“UnityEditor.UIElements”], which is used by EnumField (not “UnityEngine.UIElements”).

1 Like

@patrickf Hi, I found that specifying type for EnumField in UXML doesn’t work for nested type. like this:

namespace MyNamespace
{
     public class MyClass
    {
        public eum MyEnum{}
    }
}

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.

1 Like

We use Type.GetType(string s) to convert from the type name to the actual C# type. This page details the syntax of type names: Type.GetType Method (System) | Microsoft Learn

In your case, you would need to use the plus sign as the nested class separator: MyNamespace.MyClass+MyEnum

3 Likes

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:
6957800--819242--Bildschirmfoto 2021-03-21 um 17.28.16.png

Here is my class that contains the enum:

namespace Source.Components.Common
{
    [RequireComponent(typeof(Image))]
    public class ImageFX : MonoBehaviour
    {
        public enum Effect
        {
            Glow,
            Flash,
            Shine,
            GlowSoftMask,
            FlashSoftMask,
        }
        [...]
    }
}

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:

    [CustomEditor(typeof(ImageFX))]
    public class ImageFxEditor : UnityEditor.Editor
    {
        public void OnEnable()
        {
            VisualTreeAsset visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Source/Components/Common/Editor/ImageFX.uxml");
            var ui = visualTree.Instantiate();
            var enumField = ui.Q<EnumField>("effect");
            enumField.Init(ImageFX.Effect.Glow);
        }
    }

I feel like this should at least work, but it doesn’t somehow…

1 Like

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"/>

7940962--1015741--Captura de Tela 2022-03-04 às 09.55.17.png
7940962--1015762--Captura de Tela 2022-03-04 às 10.05.20.png

I don’t know if this is what you are looking for but in cs the EnumField object has a method “Init(EnumValue)”

2 Likes