Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

EnumField does not work in UXML (edit: partially)

Discussion in 'UI Toolkit' started by aybeone, Jan 14, 2019.

  1. aybeone

    aybeone

    Joined:
    May 24, 2015
    Posts:
    107
    None of the syntaxes I've tried actually work, e.g.

    Code (CSharp):
    1. <editor:EnumField label="test" type="Assets.Scene.Editor.What" value="One" />
    Opened a bug : https://fogbugz.unity3d.com/default.asp?1117037_hrq07u1ffmrmg64r

    EDIT

    Until I've just realized that you can make it working by calling Init ...

    Great, my problem is fixed for now but, again :D, trusting the docs was really misleading:
    https://docs.unity3d.com/2019.1/Documentation/Manual/UIE-ElementRef.html

    When I read these docs, to me it's crystal clear that you can set type and eventually value so it picks a default one and you're ready to go.

    It seems that the UXML factory is having trouble assigning these values from UXML.

    Thanks !
     
    GesunderApfel likes this.
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    Thanks for the feedback. We are aware of this limitation and for now are constrained by how we can convert types form text to C# types for instantiation. Ideally, the EnumField would be a generic, but UXML cannot instantiate generic types for now.
     
  3. aybeone

    aybeone

    Joined:
    May 24, 2015
    Posts:
    107
    Alright !
     
  4. aybeone

    aybeone

    Joined:
    May 24, 2015
    Posts:
    107
    Another bug: it's not bindable at all.

    Bug opened but I still didn't receive the email after one hour so cannot post it here.

    Basically it complains that XXX is not Enum and therefore can't set bindings.

    Another thing, I think you should make m_EnumType public just like it's done in ObjectField to make people's life easier.

    Also, I've been trying to build a generic popup field like there is on IMGUI, currently TagField and LayerField seems to have good infrastructure for it but it's not public.

    :D
     
    Djayp likes this.
  5. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    Have you had a look at PopupField?
     
  6. aybeone

    aybeone

    Joined:
    May 24, 2015
    Posts:
    107
  7. aybeone

    aybeone

    Joined:
    May 24, 2015
    Posts:
    107
    Just double checked my last post, while the GH link is about Experimental namespace, I'm talking about the non-experimental one.
     
  8. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    Noted! Changing the choices after creation has been requested before. I don't think we'll get support for PopupField<T> in UXML anytime soon (complicated) but we should be able to make the choices property public at some point.
     
  9. aybeone

    aybeone

    Joined:
    May 24, 2015
    Posts:
    107
    Yes, it's certainly better (and faster) to just make the properties public :)
     
  10. vkalaikkovan

    vkalaikkovan

    Joined:
    Jul 31, 2018
    Posts:
    2
    I would like to know how to convert a list of string as an enum in UI elements enum field.
     
  11. mura_vrs

    mura_vrs

    Joined:
    Jan 22, 2019
    Posts:
    11
    Hi all, any update on this issue? I'm in 2019.3.7f1 with v0.11.0 and I get "EnumField: Invalid enum type: "<MyEnum>" for every character I type into the EnumField Type inline field.
     
  12. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    396
    To use enumField from uxml, you must set the type using the assembly-qualified name:
    <ui:EnumField label="MyEnumType" type="MyNamespace.MyEnumType, MyAssembly" />

    https://docs.microsoft.com/en-us/do...em_Type_GetType_System_String_System_Boolean_

     
    dnorambu and DonLoquacious like this.
  13. mura_vrs

    mura_vrs

    Joined:
    Jan 22, 2019
    Posts:
    11
    Ok thanks, that worked for me.

    For future folks reading this using UIBuilder 0.11.0:

    Your enum seems to need a default item with a value of 0 or it will throw an error when the editor tries to display the VisualElement. Also, the "Type" inline field will clear your input after saving in UIBuilder but it will still be reflected in the XML preview.
     
    nicolasgramlich likes this.
  14. coderD1mka

    coderD1mka

    Joined:
    Jun 26, 2019
    Posts:
    8
    Hi!
    That does not work for me!
    My system configuration: Unity 2020.1.4f1 , IDE Rider 2020.1

    Path to the enum file: Assets/Scripts/UI_System/WindowName.cs

    Code (CSharp):
    1. namespace LID_UISystem
    2. {
    3.     public enum WindowName
    4.     {
    5.         None,
    6.         Hangar,
    7.         Shop,
    8.         Equipment,
    9.         VehiclesTree,
    10.         BuyVehicle,
    11.     }
    12. }
    Path to my uxml file: Assets/Scripts/UI_System/UXML-Templates/Template1.uxml

    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <engine:UXML
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns:engine="UnityEngine.UIElements"
    5.     xmlns:editor="UnityEditor.UIElements"
    6.     xmlns:lid="LID_UISystem"
    7.     xsi:noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd"
    8.     xmlns = "UnityEngine.UIElements"
    9. >
    10.     <Label text="Choose something:" style="background-color: yellow; color: black;"/>
    11.     <Box style="background-color: grey;" >
    12.         <Toggle name="boots" label="Boots" value="false" />
    13.         <Toggle name="helmet" label="Helmet" value="false" />
    14.         <Toggle name="cloak" label="Stuff for invisible" value="false" />
    15.     </Box>
    16.     <Box class="root">
    17.         <engine:Style src="Template1Style.uss" />
    18.         <Button name="cancel" text="Cancel"/>
    19.         <Button name="ok" text="Ok"/>
    20.         <Label text="Copyright"/>
    21.     </Box>
    22.     <Box>
    23.         <engine:Foldout>
    24.             <editor:EnumField name="wnd" label="Window" type="LID_UISystem.WindowName,Assembly-CSharp-Editor"/>
    25.         </engine:Foldout>
    26.     </Box>
    27. </engine:UXML>
    EnumField does not work. That field show me nothing in the editor window =(

    Unity Console Output:

    TypeLoadException: Could not load type 'LID_UISystem.WindowName' from assembly 'Assembly-CSharp-Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
    System.Type.GetType (System.String typeName, System.Boolean throwOnError, System.Boolean ignoreCase) (at <fb001e01371b4adca20013e0ac763896>:0)
    System.Type.GetType (System.String typeName, System.Boolean throwOnError) (at <fb001e01371b4adca20013e0ac763896>:0)
    UnityEngine.UIElements.UxmlTypeAttributeDescription`1[TBase].ConvertValueToType (System.String v, System.Type defaultValue) (at <e11cddb636f444f68eec62123764f018>:0)
    UnityEngine.UIElements.VisualTreeAsset:Instantiate()
    LID_UISystem.UIGraphEditor:OnEnable() (at Assets/Scripts/UISystem/EditorWindow/UIGraphEditor.cs:39)
    UnityEditor.EditorWindow:GetWindow()
    LID_UISystem.UIGraphEditor:ShowExample() (at Assets/Scripts/UISystem/EditorWindow/UIGraphEditor.cs:20)
     
    Last edited: Sep 12, 2020
  15. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    771
    I would try "Assembly-CSharp" instead of "Assembly-CSharp-Editor". The former is the default assembly name for all game scripts that live under Assets/, while the latter is the default assembly name for all scripts living an Assets/*/Editor/ folder.
     
    coderD1mka likes this.
  16. coderD1mka

    coderD1mka

    Joined:
    Jun 26, 2019
    Posts:
    8
    OMG! Big thanks to you! It`s worked for me! :)
     
  17. Djayp

    Djayp

    Joined:
    Feb 16, 2015
    Posts:
    114
    @uDamian Any chance we can access the enum type from code ? This doesn't work :
    upload_2020-9-30_14-15-59.png
     
  18. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    584
    This took me a while to figure out, but manually editing the uxml file to:
    Code (CSharp):
    1. <uie:EnumField label="Pentatonic Scale" value="Major" name="pentatonic" type="PentatonicScale, Assembly-CSharp" />
    And then in the editor .cs file this worked:

    Code (CSharp):
    1.        
    2. EnumField pentatonic = rootElement.Query<EnumField>("pentatonic").First();
    3.         pentatonic.value = starSystem.pentatonic;
    4.         pentatonic.RegisterCallback<ChangeEvent<System.Enum>>(
    5.              e =>
    6.              {
    7.                  starSystem.pentatonic = (PentatonicScale)e.newValue;
    8.                  EditorUtility.SetDirty(starSystem);
    9.              }
    10.          );