Search Unity

Manipulating TMP mesh as it moves

Discussion in 'UGUI & TextMesh Pro' started by nospoone, Jul 19, 2017.

  1. nospoone

    nospoone

    Joined:
    Aug 13, 2013
    Posts:
    2
    Hi!

    First of all, thanks for TMP, it is awesome to work with.

    I am trying to do an effect similar to the VertexJitter example:



    However, as you can see, the text only animates when the text is not moving. I am using UGUI to display the text (which gets its position updated in `LateUpdate`.)

    Here is what I tried so far:
    • I tried using the VertexJitter.cs directly and it causes the same issue.
    • I tried moving the mesh updating logic from the Coroutine to a direct function call right after the position of the text gets updated.
    • I tried forcing a mesh update right after moving the text and before modifying the mesh.
    I'm not sure where the problem comes from as I was thinking the mesh would be updated right after forcing an update. It seems TMP is updating its mesh after I apply my offsets to it.

    I guess my question is:
    When is TMP updating the mesh's position, so that I can offset it from its new position?

    I hope I am making sense... Thanks! :D
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Whenever a property of the text object is changed, the text is processed just before the frame is rendered which happens late in the frame cycle in OnPreRender. Since users are likely to try to modify the text in Update or LateUpdate, OnPreRender is the ideal candidate for processing the text changes.

    Now the VertexJitter script grabs the geometry of the text object and manipulates it which is fine. However as soon as any of the text properties are changed, those changes are processed and the geometry of the text is updated thus undoing whatever the VertexJitter script was / has done.

    Just moving the text object should not trigger an update of the text unless you have some other script or a layout component which is forcing the text to be updated. I would need more information about how you are moving the text to provide more insight here.

    Using ForceMeshUpdate() will force a re-process of the text and thus reset the geometry so you want to avoid that. Basically, unless you change the text, you want to avoid any updates from TMP since you are modifying the geometry on your end. I believe the VertexJitter script uses the ON_TEXT_CHANGED event to track when it needs to re-apply the changes.

    Hopefully this give you better insight in what is going on. Please provide an update of your progress on this which would be beneficial to other users trying to do similar things :)
     
    nospoone likes this.
  3. nospoone

    nospoone

    Joined:
    Aug 13, 2013
    Posts:
    2
    There we go. The problem is that I have two different horizontal layout scripts to ensure my speech bubble and my text is resized properly when the text inside changes.

    Sure enough, disabling them made it work:



    However, I lost my ability to make sure my components resize correctly. :eek:

    I used the solution listed here to ensure my box has a maximum width. I could probably manage the same kind of resizing using custom code but it was pretty much what I wanted to avoid... Perhaps there's a way to only trigger the layout scripts once when the text changes (using the event)?

    I know it isn't really TMP related but I thought I'd ask anyway!

    Merci beaucoup!
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The issue (sometimes) with layout components is that although they should not set the layout dirty as a result of moving an object around, depending on the RectTansform Anchor modes (like Stretch), in the computation of the anchors (offset), some rounding error can occur where the system thinks the dimensions of the container have changed where they have not. When this happens, it results in OnRectTransformDimensionsChanged which ends up resulting in a re-layout of the text although it was not necessary.

    I suggest you experiment with your RectTransform setup in case you are running into this scenario.