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

Question How do I update linkInfo after changing text property?

Discussion in 'UGUI & TextMesh Pro' started by DeusIntra, Aug 16, 2020.

  1. DeusIntra

    DeusIntra

    Joined:
    Jul 20, 2020
    Posts:
    3
    I want to use links to define a range. Characters in that range will get animated. The problem I have is that after I change text property with some new text like textComponent.text = "new text", the animation is still running in the same range, despite not having any links. However, if I change it for a text with links, the range changes. For example if I have a code like this:

    Code (CSharp):
    1. textComponent.text = "text example <link=anim>number</link> one";
    2. animation.Apply(textComponent);
    3. // ...wait for event to change text...
    4. textComponent.text = "text example number two";
    5. animation.Apply(textComponent);
    6. // ...wait
    7. textComponent.text = "text <link=anim>example</link> number three";
    8. animation.Apply(textComponent);
    9. // ...wait
    10. textComponent.text = "the fourth string"
    11. animation.Apply(textComponent);
    on the first change, "number" gets animated as it should. Second change "number" animates again. Third change - "example" is animated as it should. On the last change "ourth st" animates. Possible bug?
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Whenever properties of a text object are changed, once per frame, late in the update cycle and just before the frame is rendered, the text object will be processed and the content of the textInfo and sub structures like linkInfo will be updated.

    For when you need the text object to be rendered right away instead of when it is normally done, you can use ForceMeshUpdate() on the text object. This result in the text object being process immediately.
     
  3. DeusIntra

    DeusIntra

    Joined:
    Jul 20, 2020
    Posts:
    3
    Thanks for your reply! ForceMeshUpdate() doesn't seem to work for linkInfo. But I've made this test script:
    Code (CSharp):
    1. void Update()
    2. {
    3.     textComponent.ForceMeshUpdate();
    4.  
    5.     Debug.Log("textInfo.linkCount = " + textComponent.textInfo.linkCount);
    6.     Debug.Log("linkInfo.Length = "    + textComponent.textInfo.linkInfo.Length);
    7. }
    Probably didn't need ForceMeshUpdate() in Update() but whatever.
    On the first change it outputs "textInfo.linkCount = 1" and "linkInfo.Length = 1".
    On the second: "textInfo.linkCount = 0" and "linkInfo.Length = 1".
    Third is 1 and 1. Fourth is 0 and 1.

    So yeah, I'm sure it's a bug. But at least now I know how to avoid it so thanks again Stephan!
     
    Last edited: Aug 17, 2020