Search Unity

TextMesh Pro Allow more args in TMP_Text.SetText

Discussion in 'UGUI & TextMesh Pro' started by Peter77, Feb 11, 2020.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    Could you please consider to allow more "args" to be passed to
    TMP_Text.SetText
    ?

    It supports up to three arguments at the time of writing:
    public void SetText(string text, float arg0, float arg1, float arg2)


    It covers most usages for me already. However, sometimes I need a fourth or even fifth argument and then I have to fall-back to string.Format, which causes memory allocations that I try to avoid.

    Therefore I would like to ask for more than three args.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Along with the previous improvements discussed in the following thread, I added support for up to a maximum of 8 arguments to the SetText() function.

    These changes will be included in the next release of the TMP package which will be Preview 5.
     
    Peter77 likes this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    Excellent, thanks so much!
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    @Stephan_B Any chance you could also add an overload with this signature:
    Code (CSharp):
    1. public void SetText(StringBuilder text, float arg0, float arg1, float arg2);
    2. public void SetText(char[] text, float arg0, float arg1, float arg2);
    3.  
    The reason why this would be useful is because I need to concatenate texts and format numbers.

    TMP allows to format numbers without garbage, but does not support to concatenate strings without gcalloc.
    StringBuilder supports to concatenate strings without gcalloc, but does not support to format numbers without gcalloc.

    I would use a combination of StringBuilder and TMP to achieve the following without gcalloc:
    Code (CSharp):
    1. StringBuilder sb;
    2. sb.Append(FromLoca("item.action.hit"));
    3. sb.Append(FromLoca("item.redshell.name"));
    4. sb.Append(" <color=#ff00ff>+{0}</color>");
    5. textmeshpro.SetText(sb, score);
    Combine strings with StringBuilder and leave a {0} for TMP to insert a number.

    If you know of a better way, please let me know :) Perhaps build an own alloc-free StringBuilder and just pass its internal array via TMP.SetCharArray?
     
    Last edited: May 29, 2020