Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Deeeds · May 08, 2019 at 03:32 PM · garbage

Frequently change TextMeshPro text without garbage allocation?

Is there a way to frequently change a timer display created with TMP, without it generating garbage?

I'm currently using TMP.SetText(), which is supposed to not generate garbage, but it does.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cdnDave · May 08, 2019 at 05:24 PM

Are you profiling it in the editor or from a build? I believe that function does create garbage when running in the editor but not in an actual build. This is because in the editor calling the SetText function will update the .text property of the TMP object, whereas in the built player it does not.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deeeds · May 09, 2019 at 03:13 AM 0
Share

Profiling in Editor.

Not aware of ways to profile a build.

avatar image cdnDave Deeeds · May 11, 2019 at 06:07 PM 1
Share

In the build settings you need to check the boxes for "Development Build" and "Autoconnect Profiler". If you then build and run with the profiler window open it should connect. There are quite a few things under the hood that are different between running in the editor and running a build, profiling a build will give you a much more accurate look at how your games performance.

avatar image
0

Answer by andrew-lukasik · May 08, 2019 at 05:35 PM

If your SetText looks like this:

 SetText( $"{ minutes } : { seconds }" )

Then you may be interested in knowing how to code timers without creating new strings every frame

 // Note: I'm not aware of TextMeshPro details. This is general approach I use when dealing with UI components.
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deeeds · May 09, 2019 at 03:11 AM 0
Share

Hey Andrew: I'm doing it in a weird way... any problems with this?

     void UpdateSectorTimeHUDs ( ) {
         _val = Time.realtimeSinceStartup - _sktrStartTime;
         
         if ( _val < 1 ) {
             tmpTimerL.SetText( "00{0:3}", _val );
             return;
         }
         if ( _val < 10 ) {
             tmpTimerL.SetText( "0{0:3}", _val );
             return;
         }
         if ( _val < 100 ) {
             tmpTimerL.SetText( "{0:3}", _val );
         }
         else { print("time is being reset because took too long"  );
             _sktrStartTime = Time.realtimeSinceStartup;
         }
     }
 
avatar image andrew-lukasik Deeeds · May 09, 2019 at 08:31 AM 1
Share

Yes, methods where you provide some kind of format and (uint/ulong etc) value are most often than not just equivalent to writing:

 SetText( _val.ToString(format) )

Where ToString() method allocates a tiny string object every time you call it.

avatar image Deeeds andrew-lukasik · May 11, 2019 at 01:00 PM 0
Share

THAN$$anonymous$$ YOU!!! This bit of insight is invaluable!!!

avatar image andrew-lukasik Deeeds · May 09, 2019 at 09:04 AM 1
Share

$$anonymous$$y general idea to prevent this is to generate ALL possible strings for this counter when scene/game starts to a generic hash table (dictionary). And on every Update after that just reference one you need.

To prevent creating a medicine that is worse than the actual ill you should estimate size of your output cache for given counter format string.

 For "hh : mm : ss" it would be:
 24h * 60m * 60s = 86400 combinations
 86400 * "00 : 00 : 00".Length * sizeof(char) = 1 036 800 bytes = 1$$anonymous$$B (too much)

As you see from the math this can go south easily when your format string is too long and produces too many permutations. So to optimize that - just break this format string into few separate UI pieces (hours,seconds etc using separate UI components) and number of variations will drastically fall to just few hundreds of them and will take no more than just few $$anonymous$$B of memory.

 "mm" and "ss" is just 60 * "mm".Length * sizeof(char) = 240 bytes (good)

I wrote an implementations + example to simplify the whole thing if you want: www.github.com/andrew-raphael-lukasik/CacheStrings.

avatar image Deeeds andrew-lukasik · May 11, 2019 at 01:01 PM 0
Share

I've had a look at this. But am having trouble unwinding it (mentally) for how I'm doing things with just seconds and .000 ti$$anonymous$$gs for a very fast game.

Not your fault. I'm not a coder. So anything more complex than my example above is tricky for me.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What does error shouldIgnoreInGarbageDependencyTracking mean ? 1 Answer

5.6.0f3 Particle system in Edit mode is creating garbage?! (~0.5 MB/s) 1 Answer

Instantiate/Destroy Garbage Collection advice 1 Answer

Ui Effects components shadow and outline are creating tons of garbage 0 Answers

Garbage collection 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges