Elipsis Support on TextElements

Wish TextELement class has support for custom elipsis. This is quite trivial to do on TMPro via ITextPreprocessor interface, but that’s unfortunately not the case with UIToolkit’s TextElement.

Leave it here in hope that uitk team interested on making this feature.

Hi @stevphie123

Can you elaborate on your current approach to the custom ellipsis for TMP? Are you using a different sequence of characters or a different glyph?

with tmpro we’ve custom tags via ITextPreprocessor that we can replace them with any elipses we want depends on what locales or languages we’re intended them to be used. It’s very clean and simple without much of a hassle, perfect.

Recently we mocked with uitoolkit and found out that we can no longer do as mentioned above due to we can’t find a 1:1 replacement for the ITextPreprocessor.

Do note that we’ve our own custom localization system that’s quite massive and been used in production for years.

This can be done purely on the c# side pretty easy, but hoping we’d get the same flexibility that we could do for TextElements in terms of customization so it can be well integrated to our system

I’m still unsure about the use case, can you give an example of a different ellipsis depending on the locale ?

Otherwise, you might be able to use SetRenderedText(). (see)
Here’s an example of how it could be used to support the RTL plugging.

using System;
using UnityEngine;
using UnityEngine.UIElements;

    public class RTLUI : VisualElement
    {
        public new class UxmlFactory : UxmlFactory<RTLUI, UxmlTraits> {}
       
        readonly string k_UXMLPath = "RTLUI_Sample";
        readonly FastStringBuilder finalText = new FastStringBuilder(RTLSupport.DefaultBufferSize);

        public RTLUI()
        {
            languageDirection = LanguageDirection.RTL;
            // Intercepts the new text value and perform the necessary processing before the text is rendered.
            this.RegisterCallback<ChangeEvent<string>>((evt =>
            {
                TextElement te = null;
                if (evt.target is TextElement)
                    te = evt.target as TextElement;
                else if (evt.target is TextInputBaseField<string> tf)
                    te = tf.Q<TextElement>(className:"unity-text-element--inner-input-field-component");

                if (te != null)
                    te.experimental.SetRenderedText(OnPreprocessText(te.text));
            }));
           
            var vta = Resources.Load<VisualTreeAsset>(k_UXMLPath);
            vta.CloneTree(this);

            RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
        }

        // When a VisualTreeAsset is cloned, the ChangeEvent<string> does not get transmitted to the element since it has not yet been connected to a panel.
        public void OnAttachToPanel(AttachToPanelEvent evt)
        {
            this.Query<TextElement>().ForEach((te) =>
            {
                te.experimental.SetRenderedText(OnPreprocessText(te.text));
            });
        }

        public string OnPreprocessText(string input)
        {
            if (string.IsNullOrEmpty(input))
                return input;

            finalText.Clear();
            RTLSupport.FixRTL(input, finalText, true, true, false);
            finalText.Reverse();
            return finalText.ToString();
        }
}

For example Japanese long-dash, or chinese double dashes which is unique of their own compared to the usual triple dots. That’s to name a few, there are couple others which I can’t think of them off the top of my head for now.

Hmm does SetRender() exist only in 2023.x ?
either way that’s interesting, I’ll see what I can do with it.

Thanks for the reply,
~cheers

1 Like