Interacting with subsections of text

Hey! Unfortunately, no ETA to share on it yet.

1 Like

Thanks a lot for adding this! But is there a way to disable the native hyperlink handling when using the tag (in cases where we have user created text, for example)?

Hi @DrSeltsam !

At the moment, the only way to disable the tag would be by disabling rich text or using the tag. It’s something we’ll keep in mind once we add rich text support to TextFields though.

1 Like

u should also be able to just replace(“<”, “<”), (“>”, “>”) for example on any string from the user. i would imagine this works in Unity (but could be mistaken… just figured I’d mention it, since that’s an easy/typical solution if so).

Edit: I wrote the above assuming this meant preventing tags from being parsed as a link within HTML (just showing the < and > symbols instead). If it’s plain text with simple replacement for tags only (no other HTML supported), then the above suggestion wouldn’t work (because replacement would just show “<” instead of “<” like it would for actual HTML).

1 Like

When rich text support for TextFields is coming? It is mentioned it was planned for 2022, two years later still not implemented.

This feature was added in 2022, but events are still in experimental stage.
PointerOverLinkTagEvent - does not trigger the event when pointer is hovering the link. It is triggered only when clicked on the link.
PointerOutLinkTagEvent - same thing here, triggered when pointer is released over the link, not when it leaves the link area.
PointerMoveLinkTagEvent - Does not work at all

Hi @Unique-Player

Even though the API is under experimental, it is definitely supported. What you’ve reported sounds like a bug to me. Would you mind logging a ticket so we can have a closer look ? (See how to report a bug)

I think there must be something wrong. I’m using 2022.3.21f1 and this simple code allows me to change the mouse cursor when hovering over links in rich text:

RegisterCallback<PointerOverLinkTagEvent>( evt =>
{
    if( evt.target is VisualElement target )
    {
        target.AddToClassList( "link-hover" );
    }
} );

RegisterCallback<PointerOutLinkTagEvent>( evt =>
{
    if( evt.target is VisualElement target )
    {
        target.RemoveFromClassList( "link-hover" );
    }
} );

You can see it working here, where hovering over any link changes the mouse cursor:
https://www.youtube.com/watch?v=XG-w0eCZwWo

Might be well worth making a repro and filing a bug report.

1 Like

If I understand correctly, you can only interact with links, not with any kind of text, right?
In my specific case I need to find out the word under the cursor and also the boundaries of the word. The use case is for an educational game where students are able to highlight text which is then extracted.
Is this currently possible with the UI Toolkit? If not are you planning on adding these features?

It’s just links indeed, that said, there are ways to access info regarding the cursor index as well as the word/line that is on.

Your use case sounds possible to make to me, although you’ll probably have to take a somewhat hacky approach to be able to use some of the handy methods/fields that are otherwise internal.

It has been a while, but at some point I had a rudimentary version of embedded buttons/other UI elements working using links, hence I think this is possible.

1 Like

I know this is not immediately useful, but…

In this github repository for a unity-based Markdown renderer, there is a file that has code to extract the internal structure of a Label control.

It uses reflection to copy information about all of the text contained by the label, including the rectangle in which each letter is drawn, into a structure like this:

internal class TextElementInfoCopy
{
    public char     character;
    public int      index;
    public int      spriteIndex;
    public Material material;
    public int      materialReferenceIndex;
    public bool     isUsingAlternateTypeface;
    public float    pointSize;
    public int      lineNumber;
    public int      pageNumber;
    public int      vertexIndex;
    public Vector3  topLeft;
    public Vector3  bottomLeft;
    public Vector3  topRight;
    public Vector3  bottomRight;
    public float    origin;
    public float    ascender;
    public float    baseLine;
    public float    descender;
    public float    xAdvance;
    public float    aspectRatio;
    public float    scale;
    public Color32  color;
    public Color32  underlineColor;
    public Color32  strikethroughColor;
    public Color32  highlightColor;
    public bool     isVisible;
}

This could conceivably be used to implement what you’re looking for, I think.

1 Like

I essentially need to do the same exact thing. Can someone confirm or deny if this is doable today, and without unnecessary gymnastics?

Sorry for necroing this but is the syntax <link ...> something that will be supported or do we have to use <a>? I am Asking because “<link>” is not documented anywhere in the manual, see: Unity - Manual: Supported rich text tags

As of now will just do nothing which is great for piggy-backing custom logic to that tag. Though I wonder if it is future-proof to use it.

Hi @_geo1!

Yes, it’s safe to use the <link> tag. Once we have a proper alternative, we’ll follow the usual deprecation process—first marking it as obsolete for an entire Unity release before replacing it. This should give you plenty of time to make any necessary changes.

1 Like