Search Unity

Official UIElements developer guide

Discussion in 'UI Toolkit' started by antoine-unity, Mar 20, 2019.

Thread Status:
Not open for further replies.
  1. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello,

    As the API is going out of experimental in 2019.1 we have improved the developer guide and moved it out of the experimental section.

    Any feedback is appreciated about what you feel is missing or should be further documented.

    We are working on a best practice guide explaining how to best implement custom elements as well as potential performance considerations.

    Thanks.
     
  2. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    The page on uQuery is painfully sparse. Is there more complete documentation with examples anywhere? At the very least there should be examples of how to query objects with different selectors.
     
    marcospgp likes this.
  3. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    You are right that the uQuery doc is lacking. We will improve this.
     
    Last edited: Mar 27, 2019
    DonPuno, ModLunar, fxlange and 2 others like this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    I'd love to see links to basic examples in that page. How to create a custom inspector using UIElements, how to write an editor window, etc.

    I've seen such examples earlier, but I can't quite remember where they are, so having links in the developer guide would be nice.

    As for now, the developer guide has a ton of information about the parts of the system (visual tree, layout engine, etc.), but no context for any of those things.
     
    marcospgp, Alverik, EricLampi and 4 others like this.
  5. GilbertoBitt

    GilbertoBitt

    Joined:
    May 27, 2013
    Posts:
    111
  6. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    ModLunar and Sarkahn like this.
  7. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,161
    Do we have a roadmap for the game UI phase?
     
    tonytopper and ModLunar like this.
  8. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    90
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Okay, so I came back to this now half a year after I wrote my last post. I've recently upgraded an old project to 2019.2, and I was looking at a custom editor for a MonoBehaviour that was behaving kinda weirdly.

    I figured "you know what, why don't I try to rebuild it in UI Elements? It's the new thing, and it looks pretty good". So I googled, and the first hit was the developer guide.

    Now, my goal when opening that page was to figure out how I go about writing a custom editor using UI elements. And there's nothing there. There's all this information about the parts of the system and how they work, but not how you do anything. I think there's some very good reasons to put a hello world very early in the documentation. There are two main things people will use UI elements for; custom editors and editor windows. Those things should be the entry point.

    In other words, the two first links on the developer guide should be "this is how you write a custom editor" and "this is how you write an editor window". That's the most important things, and the natural way to start out understanding the system.
     
  10. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    90
    Those are good points. Meanwhile you can look at these videos



    And this one https://github.com/Unity-Technologies/UIElementsExamples , I haven't checked them out yet but I keep those tabs open for when I have the time.
     
    mandisaw likes this.
  11. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    80
    This is pretty much my experience too.
     
    daneobyrd likes this.
  12. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    We are currently working on making the manual more helpful and task based.

    We will post in thread when improvements are available.
     
    Sarkahn, SLGSimon and PixelLifetime like this.
  13. Cleverlie

    Cleverlie

    Joined:
    Dec 23, 2013
    Posts:
    219
    does anybody know anything about that UI Builder they mentioned in the last roadmap video? does it work to create UI for both editor tools and for in-game UI? does it allow us to replace UGUI with it?
     
  14. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    We'll post more info on the UI Builder once the Unite talk this week introducing it is posted on YouTube. For now, to answer your questions:
    1. Yes, the UI Builder just generates UXML and USS assets. They can be consumed by a custom inspector, an Editor window, or the runtime panel (that renders UIElements in runtime).
    2. The plan with UIElements, the framework (including UI Builder), is to become the main UI framework for Unity, for both runtime and Editor. However, UGUI and IMGUI, the older Unity UI frameworks, will not be deprecated anytime soon and will continue to be maintained.
     
    mandisaw and Cleverlie like this.
  15. Cleverlie

    Cleverlie

    Joined:
    Dec 23, 2013
    Posts:
    219
    I hope this happens soon, I'm eager to try it, I realize that after 5 years of using UGUI there are things that are simple not scalable, hard to implement in a good battle-proven architecture like MVC or MVVM or just by design hard to accomplish, like having some rectTransform be anchored to something different than its parent in the hierarchy, or like having a different render order than the order given by the hierarchy, those things are crucial to some UI designs, but since HTML+CSS is used along the entire industry this will be a change for good probably.
     
    mandisaw likes this.
  16. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    You seem to need more control than specifying z-order. Can you share a ui design where what you mention would be needed?
     
  17. Cleverlie

    Cleverlie

    Joined:
    Dec 23, 2013
    Posts:
    219
    I can't bring a detailed repro project right now, but for example one difficulty that I usually encounter is when you want a background Image follow the preferred size of a TextMeshProUGUI text, you could do this ideally by adding a ContentSizeFitter to the TMP object, now it's rect will change its size dynamically to fit the entire text, now I could add an Image as child of the text object and then set the anchors to fit the parent rect, the problem now is that the image will render in front of the text, if I could simple give it a different sort order this would be an easy thing to do, even if I could put the Image as a sibling of the text, but somehow have it's rectTransform think it's parent is the text (like how we can do with the recently added ParentConstraint for gameObjects) this problem could be easily solved. But I can't see an easy way with the current system other than making a custom script that adjusts sizes constantly, maybe there is a way if I learn how the layout system works to implement some ILayoutElement or something like that, but that would require knowledge and time, and I tried it before but I'm still a bit helpless about the UGUI layout system, I didn't find much detailed documentation, the devil is on the details and is not as straight forward as, say, how I solved this problem with NGUI in the old days.

    other examples are, when you want at runtime to bring something to the front, like if you have multiple panel windows opened, since the hierarchy defines the sorting order then you have to mess with child Indexes and resort childrens to move something to front, or mess around with Canvas objects which are not ideal since they add a layer of additional features and complexity not needed for the case, also I think they break batching.

    I'm not saying UGUI is bad or too complex, I've been using it for the last 5 years and I can manage to do lots of interesting things with it, I just think that HTML + CSS + jquery (aka UXML + USS + uquery) are nowadays industry standards to create UI for all kinds of platforms, and bringing them to Unity is a good move, I love the idea of developing interfaces in retained mode, and having my logic separated from styles and presentation, way more MVVM-friendly and adapted to industry standard patterns! I also hope UGUI doesn't go anywhere for the foreseeable future!
     
    mandisaw likes this.
  18. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    With UIElements, flex takes care of this. If you need both the image and the text to size dynamically according to their content, you simply parent the text element under an image and sizes would follow as intended. If you only need to display the image as simple background of the text, you can set the label's background-image to your image and that would be the end of it :)

    https://docs.unity3d.com/ScriptReference/UIElements.VisualElement.BringToFront.html
    https://docs.unity3d.com/ScriptReference/UIElements.VisualElement.SendToBack.html

    These change the children ordering for one level. I guess you could recursively call it on the parent's element to make sure the entire branch is on top.

    There is no plan for deprecating uGUI anytime soon.
     
    mandisaw likes this.
  19. krassemanne

    krassemanne

    Joined:
    Jun 27, 2015
    Posts:
    8
    Hello, I am trying to prepare for the runtime version of UI Elements by only using the stuff in UnityEngine.

    I then discovered that there is no dropdown component to use, so I figured I create one myself, like I did for my CSS/Javascript with absolute position and overlay.

    So far so good.

    But then I noticed that there is no z-index support in USS, which makes it much harder to implement such overlays.

    Any standard solution for this? I noticed that there is a BringToFront() etc. but that doesn't really work when there are other components.
    For example, if you move the overlay outside its hierarchy, the absolute position property will look at another parent and position relative that and the simple specification of positioning within the USS will have to be scripted instead.


    The best solution I have come up with so far is to have all the overlays last in the hierarchy and position them absolute by checking the absolute position of the dropdown. This is fine but a z-index would be much cleaner.
     
    Last edited: Oct 22, 2019
    PassivePicasso likes this.
  20. PassivePicasso

    PassivePicasso

    Joined:
    Sep 17, 2012
    Posts:
    100
    https://forum.unity.com/threads/typ...pup-dropdown-screen-space-positioning.764504/

    See my post about it and comment any thoughts you may have, I see this as a problem overall.
     
    krassemanne likes this.
  21. EvgenyVasilyev

    EvgenyVasilyev

    Joined:
    Jul 5, 2019
    Posts:
    7
    UIElementsEditorUtility appears to be missing from the docs. This is useful if you are going to change the mouse cursor through code.
     
  22. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    184
    You are right. This is noted and I will notify our UI documentation team.
     
  23. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    184
    If/when it becomes public of course.
     
  24. matthew-holtzem

    matthew-holtzem

    Joined:
    Sep 1, 2016
    Posts:
    40
    Any word on when we might see some updated documentation? Trying to get up to speed on this system but finding it pretty difficult to get started due to lack of documentation or Unity Learn tutorials
     
    marcospgp likes this.
  25. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
  26. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    I have a question regarding the UIBuilder, as a developer is writing the plain XML files still fully supported, and taken into consideration when you are developing the standards?

    My background is in frontend webdevelopment, where nobody uses a visual editor for HTML. Although i see some value in the UI builder for my colleages, i dont plan on using it myself very much and prefer to write plain XML in my text editor. Though when i look at the runtime example the code is quite convoluted with namespaces etc.
     
  27. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    We are striving to have the UI Builder generate clean UXML, as close to a manually written one as possible. What you linked above is about as clean as you could write that UXML manually, including the use of namespaces to shorten the tag names. This also means having the UI Builder properly load and handle any manually written UXML as well.

    Keep in mind that UXML tags are really just C# type names, and they need to be fully qualified with their namespace. UXML is not HTML, in that it has no <div>, <h1>, <p>... tags. It just has a few special types of tags, like <Style> and <Instance>, but even those can be namespaced so they don't conflict with your own custom C# type: "Style".
     
    TJHeuvel-net likes this.
  28. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    I havent really read into it completely, but when i read the UXML from the manual here it is quite different, and readable. Much more like HTML, and easily human-writable.

    Thanks for your response, but i still dont know if writing it manually is a scenario you want to support or not.
     
  29. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    I see what you mean now. We've changed our UXML standard (and therefore our generation of UXML) to avoid using the default namespace (x
    mlns="UnityEngine.UIElements"
    ). You are free to use the default namespace in your UXML files but we no longer assume the
    UnityEngine.UIElements
    is the default namespace. It conflicted too much with user-made custom C# elements with the same names (ie. "Box"). That's why Builder-generated UXML always uses explicit namespaces.

    To confirm, writing UXML/USS manually is a scenario we will continue to support, including IDE extensions for better auto-completion and syntax highlighting.
     
  30. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    Awesome, thats great to hear! It makes a lot of sense that generated code is less readable than handwritten.

    Cant wait to use this in runtime!
     
    Last edited: Dec 2, 2019
  31. thepigeonfighter

    thepigeonfighter

    Joined:
    Nov 7, 2017
    Posts:
    37
    I think something that would be really nice would be if the UIElements debugger had a USS dump section similar to the UXML dump section. The values that are edited get added to a generated USS dump section that I can just copy the changes I like and paste them into my file. And obviously as others were saying more "how to docs".Especially on big picture stuff. For example design strategies. I was looking over how shadergraph was implemented using UIElements and it looked awesome although it was very hard to navigate due to Visual Studio not indexing that assembly. Which meant I couldn't jump to definitions. Plus it seemed to be using some sort of variation of a MVC pattern but not a one to one implementation. So I would be interested in the most efficient organizational strategies you all used.
     
  32. JodyMagnopus

    JodyMagnopus

    Joined:
    Sep 17, 2016
    Posts:
    13
    I would just like to mention that there is nothing in the documentation about how to create a UI which consists of an array of objects which comes from a member variable in a Scriptable object using the XML-based approach. It is also hard to know what you can type in with the XML so I avoided it entirely. Perhaps the UI builder is meant to address that part of it. I will definitely give the latest UI Builder a try if I have to make a new UI window in the future.
     
  33. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    If you add a PropertyField in UXML with the binding-path set to the name of the member variable, you should get a resizable list generated for you.

    This is not a list example, but it shows how to use the PropertyField in UXML:
    https://github.com/Unity-Technologi...Intro/Resources/Inspector/inspector_uxml.uxml
    Here's the C# counterpart:
    https://github.com/Unity-Technologi.../Assets/QuickIntro/Editor/BasicsDemoWindow.cs (look for
    #region Bindings


    If you want type of items in the array are custom and have a CustomPropertyDrawer, they will be drawn using the custom drawer. The custom drawer itself can also use UIElements and therefore UXML.
     
  34. TheZaeron

    TheZaeron

    Joined:
    May 10, 2014
    Posts:
    5
    Hi!

    I was recommended to ask about this here.

    I'm trying to get an EnumField style dropdown to display custom strings that represent the actual enum values, and either I really suck (which is entirely possible, I've got no prior experience working with Property Drawers, as was suggested for me to try and work around this) or there doesn't seem to be a straightforward way of achieving this?

    If good documentation or a more straightforward way of doing this is coming, please let me know, otherwise feel free to slap me on the wrist for being low-effort/unskilled and not working harder on this :D

    Many thanks
     
  35. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    Sorry to do this but please start another thread for your question. This thread is just about the developer guide itself.
     
  36. TheZaeron

    TheZaeron

    Joined:
    May 10, 2014
    Posts:
    5
    Sorry!
     
  37. aassal

    aassal

    Joined:
    Jan 29, 2020
    Posts:
    4
    Hi,

    How can I change the position of the VisualTree, i.e. the panel itself, in realtime/in code? I am trying to render a uxml anchored to a 3D object in runtime.

    Also, I successfully bind a serialized object/property to a label UIElement, when the property value changes the text attribute changes successfully (I log the new value of the attribute) but the UI does not render this change, what am I missing? Do I need to manually refresh the UI?

    Thank you.
     
    Last edited: May 31, 2020
  38. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    To move your VisualElement without influencing layout, you access change its 2D position:
    https://docs.unity3d.com/ScriptReference/UIElements.ITransform.html


    You can't use SerializedObject binding at runtime since the editor assembly will not be available when you make a player build. In the current version of the preview package, there are refresh issues when in edit mode. These issues will be fixed with the next release.
     
    Last edited: Jun 2, 2020
  39. aassal

    aassal

    Joined:
    Jan 29, 2020
    Posts:
    4
    Thank you for your reply and support.

    So I can actually position it in the world if I transform the values in the transform. Excellent!

    Release of what the runtime PanelRenderer or the UIElements package itself?

    This is the first time I read this! It is not mentioned anywhere!!! Based on what you are saying I can not use binding at all in runtime and the only way to go is to use either Schedulers or change events?

    Thanks....
     
  40. WavyRancheros

    WavyRancheros

    Joined:
    Feb 5, 2013
    Posts:
    176
    How can I listen to changes in a PropertyField?
     
  41. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    In 2020.2, we added a `SerializedPropertyChangeEvent` dispatched by PropertyField.

    But for any version, you can still listen to the regular ChagenEvent<T> events. They will still be sent and propagated up the tree from whatever field(s) the PropertyField created inside itself.

    You can do something like this:

    Code (CSharp):
    1. void RegisterPropertyChangeCallback(PropertyField field, SerializedProperty property, FieldInfo fieldInfo, Action callback)
    2. {
    3.      var propertyType = property.propertyType;
    4.         switch (propertyType)
    5.         {
    6.             case SerializedPropertyType.Integer:
    7.             case SerializedPropertyType.LayerMask:
    8.             case SerializedPropertyType.ArraySize:
    9.                 field.RegisterCallback<ChangeEvent<int>>( (evt) => callback());
    10.                 break;
    11.             case SerializedPropertyType.Boolean:
    12.                 field.RegisterCallback<ChangeEvent<bool>>( (evt) => callback());
    13.                 break;
    14.             case SerializedPropertyType.Float:
    15.                 field.RegisterCallback<ChangeEvent<float>>( (evt) => callback());
    16.                 break;
    17.             case SerializedPropertyType.String:
    18.             case SerializedPropertyType.Character:
    19.                 field.RegisterCallback<ChangeEvent<string>>( (evt) => callback());
    20.                 break;
    21.             case SerializedPropertyType.Color:
    22.                 field.RegisterCallback<ChangeEvent<Color>>( (evt) => callback());
    23.                 break;
    24.             case SerializedPropertyType.ObjectReference:
    25.                 field.RegisterCallback<ChangeEvent<UnityEngine.Object>>( (evt) => callback());
    26.                 break;
    27.             case SerializedPropertyType.Enum:
    28.             {
    29.                 Type enumType = fieldInfo.FieldType;
    30.                 if (enumType.IsDefined(typeof(FlagsAttribute), false))
    31.                 {
    32.                     field.RegisterCallback<ChangeEvent<Enum>>( (evt) => callback());
    33.                 }
    34.                 else
    35.                 {
    36.                     field.RegisterCallback<ChangeEvent<string>>( (evt) => callback());
    37.                 }
    38.                 break;
    39.             }
    40.             case SerializedPropertyType.Vector2:
    41.                 field.RegisterCallback<ChangeEvent<Vector2>>( (evt) => callback());
    42.                 break;
    43.             case SerializedPropertyType.Vector3:
    44.                 field.RegisterCallback<ChangeEvent<Vector3>>( (evt) => callback());
    45.                 break;
    46.             case SerializedPropertyType.Vector4:
    47.                 field.RegisterCallback<ChangeEvent<Vector4>>( (evt) => callback());
    48.                 break;
    49.             case SerializedPropertyType.Rect:
    50.                 field.RegisterCallback<ChangeEvent<Rect>>( (evt) => callback());
    51.                 break;
    52.             case SerializedPropertyType.AnimationCurve:
    53.                 field.RegisterCallback<ChangeEvent<AnimationCurve>>( (evt) => callback());
    54.                 break;
    55.             case SerializedPropertyType.Bounds:
    56.                 field.RegisterCallback<ChangeEvent<Bounds>>( (evt) => callback());
    57.                 break;
    58.             case SerializedPropertyType.Gradient:
    59.                 field.RegisterCallback<ChangeEvent<Gradient>>( (evt) => callback());
    60.                 break;
    61.             case SerializedPropertyType.Vector2Int:
    62.                 field.RegisterCallback<ChangeEvent<Vector2Int>>( (evt) => callback());
    63.                 break;
    64.             case SerializedPropertyType.Vector3Int:
    65.                 field.RegisterCallback<ChangeEvent<Vector3Int>>( (evt) => callback());
    66.                 break;
    67.             case SerializedPropertyType.RectInt:
    68.                 field.RegisterCallback<ChangeEvent<RectInt>>( (evt) => callback());
    69.                 break;
    70.             case SerializedPropertyType.BoundsInt:
    71.                 field.RegisterCallback<ChangeEvent<BoundsInt>>( (evt) => callback());
    72.                 break;
    73.             case SerializedPropertyType.Quaternion:
    74.             case SerializedPropertyType.ExposedReference:
    75.             case SerializedPropertyType.FixedBufferSize:
    76.             case SerializedPropertyType.Generic:
    77.             default:
    78.                 // Not supported for now
    79.                 break;
    80.         }
    81. }
     
    KarolRG, brunocoimbra and Xarbrough like this.
  42. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    191
    How flexible is this UI Toolkit in terms of dynamic content generation ? My game is pretty "PopUp" heavy... it focuses mobiles and is server based, this means that everytime a player clicks on a enemy e.g. the server sends a package with the popup, title and possible interactions ( buttons ) to the client where we construct the popup by using the old GameObject driven system.

    Im not using any common UI models, so any popup is self driven and contains the logic it requires for inserting title, buttons... Completly based on components attached to the popup gameobject and the inserted title, buttons...

    Is this possible with the upcoming UI-Toolkit ? Do we need manager classes for controlling the logic of those VisualElements ? Are they self driven ? Are we able to define logic in custom elements ? And can we define custom elements that act as logic classes which dont render anything ( Store inputs... sorting of elements, choosing insertation location of inserted elements ) ?
     
  43. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    It's very flexible. You can inherit from VisualElement and stick whatever logic and data you want to in there, it'll still be renderable by the UI.
     
  44. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    Is this tech useful for simple responsive UI layout and simpler to use than regular unity ui, and how to do that? :p
     
  45. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
  46. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    I've seen that ages ago but the thing is way more complex that just watching a video. :p
     
  47. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    Please start a new thread with a more specific question and we'll do our best to help.
     
  48. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    So if I have no idea where to start you can't help me? :p

    Tried getting the same windows than in the video but I can't find anything, what asset should I create etc... the video kinda go directly into something you're supposed to know...
     
  49. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
  50. ZoidbergForPresident

    ZoidbergForPresident

    Joined:
    Dec 15, 2015
    Posts:
    157
    The point of that video eludes me, sorry.

    And the tutorial seems outdated and doesn't work: I get a bunch of errors at the styles part. Maybe it has to do with the fact I had to delete the template data when I created the files through the menu (UIToolkit).
     
    TJHeuvel-net and steinbitglis like this.
Thread Status:
Not open for further replies.