#How do I delete Links out of a TMPro Object?

1 messages · Page 1 of 1 (latest)

tall warren
#

I have 4 links, which are being created via this code:

 public TextMeshProUGUI tmpText;
 void Start()
 {
     tmpText.text =
         "<link=Text1><color=blue><u>Text1</u></color></link>, " +
         "<link=Text2><color=blue><u>Text2</u></color></link>, " +
         "<link=Text3><color=blue><u>Text3</u></color></link>, " +
         "<link=Text4><color=blue><u>Text4</u></color></link>";
 }

And handling it via this code:

public class TextClickHandler : MonoBehaviour, IPointerClickHandler
{
    private TextMeshProUGUI tmpText;
    public static event Action<string> ClickedText;
    void Awake()
    {
        tmpText = GetComponent<TextMeshProUGUI>();
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        //Nur Gott weiß wieso das funktioniert
        int linkIndex = TMP_TextUtilities.FindIntersectingLink(tmpText,Input.mousePosition,null);

        if (linkIndex != -1)
        {
            TMP_LinkInfo linkInfo = tmpText.textInfo.linkInfo[linkIndex];
            string clickedText = linkInfo.GetLinkID();
            Debug.Log($"Clicked: {clickedText}");
            ClickedText?.Invoke(clickedText);
        }
    }
}```

After firing the Event, I would like to delete the link from the TMPro Object, but I don't really know how to do that. I tried replacing the text with " " but that didn't seem to work, as nothing would happen.
Any ideas?
atomic cape
#

you're saying just tmpText.text = ""; didn't work?

tall warren
#

yes, I would assume that that was because of how I was trying to do that, not that the idea was wrong

atomic cape
#

so how did you do it then

#

did you debug to check the code was being reached?

tall warren
#

I called RemoveLink(linkInfo) below the Event line.

private void RemoveLink(TMP_LinkInfo linkInfo)
    {
        string fullText = tmpText.text;

        string linkText = fullText.Substring(linkInfo.linkTextfirstCharacterIndex, linkInfo.linkTextLength);

        string pattern =
            $"<link={linkInfo.GetLinkID()}><color=blue><u>{linkText}</u></color></link>";

        fullText = fullText.Replace(pattern, "");

        tmpText.text = fullText;
        Debug.log("Hello There!")
    }```
The Debug was working
atomic cape
#

and did you try debugging to see what fullText was?

tall warren
#

ooh, no I didn't, lemme try that rq

atomic cape
#

i feel like that string manipulation is overkill

#

are you trying to remove a specific link there or something?

#

if so, you could just have a list of visible links, then modify that and rebuild the string

tall warren
#

I press a category first, then press a link (the black underlined words at the bottom). Then they will be added to the bottom of the category in the color of the player whose turn it was. After a player has already sorted a "thing" it should no longer be in the list of things to select as it already has been used if you understand what I mean

atomic cape
#

so yeah using a list and manipulating that would probably be much easier than trying to do string manipulation

#

but also, are you just using the links to get click info? why not just use a button?

tall warren
#

I currently only use 4 for testing. The actual game will have up to 17 "things" to sort per round, which all will be different lengths. That was the easier thing to do longterm

atomic cape
#

you mean the links or the string manipulation?

#

either way i don't really think that's true..?

tall warren
#

links instead of buttons

atomic cape
#

you know prefabs are a thing right

tall warren
#

🫠

#

altho, I plan on being able to upload a file with the game data, as to not hardcode the questions and things into the game

#

anyways, I added the debug for the full text

atomic cape
tall warren
#

aight

#

how exactly would I do that?

atomic cape
#

what, use a list?

#

or building the string dynamically?

tall warren
#

I would assume, I should do something like this:

public TextMeshProUGUI tmpText;

    private List<string> names = new List<string>();

    void Start()
    {
        names.Add("Alice");
        names.Add("Bob");
        names.Add("Charlie");

        UpdateTMP();
    }

    void UpdateTMP()
    {
        tmpText.text = "";

        for (int i = 0; i < names.Count; i++)
        {
            string name = names[i];
            // Add a unique link id per name (can also just use the name)
            tmpText.text += $"<link={i}><color=blue><u>{name}</u></color></link>\n";
        }
    }```
#

(used a quick ai prompt to give an example)

atomic cape
#

oh god no

tall warren
#

and from then on I would keep everything the same, and after firing the ClickedText event just manipulate the list?

atomic cape
#

definitely would not be mutating .text a bunch of times

atomic cape
tall warren
#

that is

atomic cape
#

the UpdateTMP

tall warren
#

much much easier than what I was trying before

atomic cape
#

but.. written better

tall warren
#

yeah that should be no problelm

#

just the general idea should work

#

thanks :D

atomic cape
#

as for the button thing - doing this with buttons just changes the string manipulation into gameobject manipulation

tall warren
#

I actually even started with buttons xD

atomic cape
#

you could have the list be of gameobjects (or more practically a component to manage it) and then removing an item would be destroying the gameobject and removing the component from the list

tall warren
#

yeah I had that idea to begin with, even the interaction would have been easier

#

But I figured astetically (how the hell do you write that) using a TMPro Object would be much more fitting for what I am planning

atomic cape
#

aesthetically

tall warren
atomic cape
#

good point on the graphical aspect - just imo having buttons, or at least separate objects would make it easier to manage the events

#

but ultimately your call of course

#

pick what's right for your vision

tall warren
#

eh I am just out here copying a gameshow I saw on YouTube for me and my friends, what could go wrong 🤷‍♂️

atomic cape
#

-# getting sued

tall warren
#

ehh, neither have they published (or infact even have the game) themselves, nor will I

#

I ain't that stupid 😂

#

besides, that it is one of the games that nobody really can own