Assets/TextExtensions.cs
/// <summary>
/// Updates the text of a TextMeshPro object, even from a non-UI thread.
/// </summary>
/// <param name="mesh">The text mesh pro to update.</param>
/// <param name="text">The new text to apply to the mesh.</param>
Updates the text of a TextMeshPro object, even from a non-UI thread.
Updates the text of a TextMeshPro object even from a non - UI thread .
public static void UpdateText(this TextMeshPro mesh, string text)
        {
            void updateTextHandler(object[] args)
            {
                TextMeshPro target = (TextMeshPro)args[0];
                target.text = args[1] as string;
                if (!string.IsNullOrWhiteSpace(target.text) && target.TryGetComponent<BoxCollider2D>(out BoxCollider2D collider))
                {
                    target.ForceMeshUpdate();
                    Bounds bounds = target.textBounds;
                    collider.offset = bounds.center;
                    collider.size = new Vector3(bounds.size.x + 1.0F, bounds.size.y + 1.0F, bounds.size.z);
                }
            }
            UIThread.Invoke(updateTextHandler, mesh, text);
        }
public static void UpdateText ( this TextMeshPro mesh , string text ) { void updateTextHandler ( object [ ] args ) { TextMeshPro target = ( TextMeshPro ) args [ 0 ] ; target . text = args [ 1 ] as string ; if ( ! string . IsNullOrWhiteSpace ( target . text ) && target . TryGetComponent < BoxCollider2D > ( out BoxCollider2D collider ) ) { target . ForceMeshUpdate ( ) ; Bounds bounds = target . textBounds ; collider . offset = bounds . center ; collider . size = new Vector3 ( bounds . size . x + 1.0F , bounds . size . y + 1.0F , bounds . size . z ) ; } } UIThread . Invoke ( updateTextHandler , mesh , text ) ; }
Updates the text of a TextMeshPro object, even from a non-UI thread.
Updates the text of a TextMeshPro object even from a non - UI thread .
// Auto resize any associated collider after text has been updated // Text updates must occur on main UI thread
TextMeshPro
this
text
string
The new text to apply to the mesh.
The new text to apply to the mesh .
mesh
The text mesh pro to update.
The text mesh pro to update .