Forum rules - please read before posting.

Making use of TextMeshPro's <link> tags

Hi everyone!

I'm following this tutorial to make use of the tag in TextMeshPro. I understand it can turn tagged text within a UI text box into a clickable button.

The reason to use tags instead of UI buttons is that I need the clickable text to fit within a larger string of text – almost like a hyperlink. The clickable text also needs to be editable at runtime using variables.

Would it be possible to have an option to connect with AC menus instead of using the Button menu element?

Comments

  • A custom script is needed to process what happens when the link is clicked. The link tag itself can be used - but what exactly do you want it to do once clicked?

  • Thanks for the reply! I'd like AC to treat it like a Unity UI button so I can trigger action lists by clicking it.

  • AC won't treat the link as anything - it'll be processed by whatever means you input it (speech, variable, menu manager etc), and should display correctly, but what happens when you click it is still reliant on a custom script.

    Such a script needs to hold a record of ActionLists paired with the associated "ID" of the link it should run for. Something like:

    using UnityEngine;
    using AC;
    using TMPro;
    
    public class ActionListLinks : MonoBehaviour, IPointerClickHandler
    {
    
        public TMP_Text text;
        public ActionListIDs[] actionListIDs = new ActionListIDs[0];
    
        public void OnPointerClick(PointerEventData eventData)
        {
            var linkIndex = TMP_TextUtilities.FindIntersectingLink(text, Input.mousePosition, null);
            var linkId = text.textInfo.linkInfo[linkIndex].GetLinkID();
    
            foreach (var actionListID in actionListIDs)
            {
                if (actionListID.id == linkId)
                {
                    actionListID.actionList.Interact ();
                    return;
                }
            }
        }
    
        [System.Serializable]
        public class ActionListIDs
        {
            public string id;
            public ActionListAsset actionList;
        }
    
    }
    
  • Amazing, thanks it works great!

    If anyone else needs to do something similar, you also need to add:

    using UnityEngine.EventSystems;

    Make sure your Unity UI is Screen Space - Overlay
    And that your Textmesh pro element has Raycast enabled (in extra settings)

  • Sorry, I have one more question: why doesn't it work when the UI is set to Screen Space - Camera?

    I've tried playing with the UI's Z axis and different settings but it looks like clicks on the tags aren't registered. Clicks on a standard button linking to the same action list work fine.

  • I'm not sure - but AC won't be in charge of registering the link clicks, only what happens afterwards. It may be a Unity UI / TMPro issue.

    Screen Space - Camera requires a Render Camera to be assigned. If this UI is a Menu prefab spawned in at runtime, AC will automatically assign this as the MainCamera.

  • Thanks for the help - I'll see if I can work it out.

  • edited March 11

    After testing a bit more, it seems like there is an issue from the AC side.

    I have a test UI menu with a TexMesh PRO link and a button. Both work fine in play mode whether the canvas is overlaid or rendered by AC's Main camera.

    But as soon as AC handles the menu, the link doesn't detect the mouse clicks. It's weird because the button still works fine.

    If I disable the AC main camera, it does work (but the AC cursor isn't rendered properly).

    Any idea why that could be?

  • How is AC handling the Menu - just spawning it in and enabling it, or are there any elements linking to the Text?

    Where is the script placed, and if you place a Debug.Log statement at the top of the OnPointerClick function, does it show in the Console when the link is clicked?

  • The script is placed on my Unity UI's TextMesh Pro text.
    The text is linked to a Label element in AC.

    The menu is set to appear during gameplay.

    A Debug.Log statement appears when I click the tag when the menu is set to Overlay, but nothing happens when it's set to Screen Space - Camera (Main Camera).

  • I don't see how AC can be a factor here. If you place the UI prefab in a blank scene, without AC, and run it with some sample link text in the TextMeshPro text box, does the Log show, after setting it to Screen Space Camera and assigning the Main Camera?

  • edited March 17

    Hi again.

    sorry but I still can't work this one out. Here is my full testing setup:

    • I am using this script to turn textmesh pro links into hyperlinks.

    • The script is placed on a textmesh pro element. The text has link tags around the words CLICK THIS

    • The UI canvas is placed in a new scene without AC. When I run the game, the link turns blue when I hover my mouse over it. It works both in Screen Space - Overlay and in Screen Space - Camera mode.

    • I then make a prefab of the UI and link it to AC. It has a Label element and the right Constant ID. The menu label has the same text and tags as the original textmesh pro text.

    • I disable the prefab in the scene and launch the game. Hovering above the link turns it blue as expected, but only when the UI canvas is set to Screen Space - Overlay.

    • When I save the UI canvas prefab as Screen Space - Camera mode and AC launches it, the link detection doesn't work.

    I've tried assigning different cameras to render the UI. Different main camera settings for clipping planes, depth, FOV. Also played with the aspect ratio in the camera settings but nothing seems to work. Any ideas what else I could try? It's very puzzling

  • The UI canvas is placed in a new scene without AC. When I run the game, the link turns blue when I hover my mouse over it. It works both in Screen Space - Overlay and in Screen Space - Camera mode.

    When testing, are you assigning the scene's Camera in the Canvas component? If unassigned, the Canvas will act identical to Screen Space - Overlay mode.

    I then make a prefab of the UI and link it to AC. It has a Label element and the right Constant ID. The menu label has the same text and tags as the original textmesh pro text.

    If you link the UI prefab to AC as a Menu, but remove the Label element (so that the TextMesh Pro component remains, but is unaffected by AC), what then?

    When I save the UI canvas prefab as Screen Space - Camera mode and AC launches it, the link detection doesn't work.

    Does the Debug.Log statement appear in the Console?

  • Thanks for the help, Chris. I'm still stumped by this one so I will leave it in the Overlay screen space for now.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.