Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    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:
    498
    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:
    47
    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:
    498
    Works. Thanks!
     
  4. KosaJr

    KosaJr

    Joined:
    Jan 3, 2017
    Posts:
    8
    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:
    498
    @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.
     
  6. patrickf

    patrickf

    Unity Technologies

    Joined:
    Oct 24, 2016
    Posts:
    47
    Kichang-Kim likes this.
unityunity