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. Dismiss Notice

Resolved Will 2023.2+ UxmlFactory and UxmlTraits obsolete? (Yes!)

Discussion in 'UI Toolkit' started by CaseyHofland, Jul 24, 2023.

  1. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    542
    I noticed there's a few changes happening in the UIElements landscape:

    New Code.png
    New way of defining attributes (this is awesome!)

    I welcome the change! But I just want to be sure: will these attributes replace
    UxmlFactory
    and
    UxmlTraits
    ? I might base a design decision around that information.

    I also noticed
    UxmlAttributeConverter
    in 2023.2, which is just mega-super-amazing! This lets us get really creative and solves a lot of headaches for me, great work!
    https://docs.unity3d.com/2023.2/Doc...ence/UIElements.UxmlAttributeConverter_1.html
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    Yes, we will eventually deprecate and remove UxmlFactory and UxmlTraits.
     
    CaseyHofland likes this.
  3. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    542
    I'm gonna try out regardless, but I assume this would be no problem:

    Code (CSharp):
    1. using UnityEditor.UIElements;
    2.  
    3. public class TimeSpanConverter : UxmlAttributeConverter<TimeSpan>
    4. {
    5.     public override TimeSpan FromString(string value) => TimeSpan.TryParse(value, out TimeSpan result) ? result : TimeSpan.Zero;
    6.     public override string ToString(TimeSpan value) => value.ToString("g");
    7. }
    Code (CSharp):
    1. public class TimeSpanField : TextValueField<TimeSpan>
    2. {
    3.     // UIBuilder just works
    4. }
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    Your example won't work, the value needs to be serializable by Unity and TimeSpan is not.
    You can work around this in a few ways. You could use a string instead or create a string backing field like this:

    Code (csharp):
    1.  
    2. [UxmlElement]
    3. public partial class TimeSpanField : BaseField<TimeSpan>
    4. {
    5.     [UxmlAttribute("value")]
    6.     string valueAsString
    7.     {
    8.         get => value.ToString("g");
    9.         set
    10.         {
    11.             if (TimeSpan.TryParse(value, out TimeSpan ts))
    12.                 this.value = ts;
    13.             else
    14.                 ts = TimeSpan.Zero;
    15.         }
    16.     }
    17.  
    18.     public TimeSpanField() : base(null, null)
    19.     {
    20.     }
    21. }
    This will now use the string method to serialize the field. It makes the UxmlAttributeConverter redundant however its still needed to avoid error messages at the moment.
    You can add a custom property drawer to improve the field so its not just taking any string.
     
  5. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    542
    I do need to override from
    TextValueField<T>
    because I need to override for
    TextValueInput
    as well to create the dragging functionality. If I put the above code in here:

    Code (CSharp):
    1. [UxmlElement]
    2. public partial class TimeSpanField : TextValueField<TimeSpan>
    3. {
    4.     [UxmlAttribute("value")]
    5.     protected string valueAsString
    6.     {
    7.         // yada yada
    8.     }
    9. }
    Will it then override the "value" attribute or throw an error? I would hope it doesn't throw an error, it would be really nice to create a structurally sound object where
    TimeSpanField.value
    is a
    TimeSpan
    , that is a lot safer than putting the onus on the user to provide correct strings.
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    Yes, by design this will override the uxml attribute with the matching name in the base class.
     
    CaseyHofland likes this.
  7. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    542
    I will become unstoppable. Can't wait for 2023.2+ now!
     
    karl_jones likes this.