Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMesh Pro Having more UI-text-related garbage-free operations

Discussion in 'UGUI & TextMesh Pro' started by Gladyon, Jan 17, 2019.

  1. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    The 'TMP_Text.SetText(StringBuilder text)' method is a very good thing, finally allowing us to avoid most of the garbage generated by the displayed texts without having to use some dirty tricks involving reflection in .NET.


    I think that it could go even further.
    For example, some 'GetPreferredValues()' methods get a string as input, we could very easily have StringBuilder versions as the text is transformed in an array of int, and there already is a function to transform a StringBuilder in an array of int.
    There are probably a lot more situations where StringBuilder could also be used in TMP, but I only use a tiny fraction of it so I don't really know about other situation where it would help to avoid garbage generation.

    Also, I noticed that the 'TMP_InputField' class do not use StringBuilder.
    I am sure it would a lot harder to implement that just adding some 'GetPreferredValues()' methods taking Stringbuilder parameters, but I am also sure that it would help, especially since that the most common situation for an input field is to concatenate a character to the current input, which is very non-optimal with strings and would greatly benefit from a StringBuilder.


    So, as a summary:
    - is it possible to have StringBuilder versions for some methods? ('GetPreferredValues()' and 'GetTextInfo()' are very easy to implement, maybe others would be interesting)
    - is it possible to base 'TMP_InputField' on a StringBuilder (or on an array of int) with most methods to interact with it taking either a string or a StringBuilder?
     
    dadude123 likes this.
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    That is certainly something possible. I won't be able to get to this until after the next TMP package is released. I also have some key new features planned for the subsequent release but adding such support might not be too complex. I'll keep you posted.

    The TMP Input Field (unlike the UI Input Field) uses a 2D Rect Mask instead of string concatenation to control the visible text. So on the front we are fine. There are string operations in other areas but I get your point and will also take a look at this.
     
  3. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    There's no rush, eliminating garbage collection is a long standing battle which won't be won in a week.
    Thanks for the answer.
     
  4. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    247
    Necro-post this. Is there any news about more GC-free text operations?
     
  5. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    Looking at the source code, I'm afraid it hasn't been included in TextMeshPro.