Search Unity

TextMesh Pro Is it OK to assign text to the same thing each Update?

Discussion in 'UGUI & TextMesh Pro' started by AcademyOfFetishes, Mar 4, 2019.

  1. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    If every Update call, I set a TextMeshPro text to the same thing, is that slower than only setting the text when it changes, or does TextMeshPro realize that's what it already has and skip processing?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Doing anything in Update is always slower than just doing it once. Never do anything in Update that doesn't actually have to change every frame. Even if TextMeshPro skipped processing, it would still have to spend some time figuring out that it could skip. Rather than waste time on that, it would be more efficient to assume that users wouldn't repeatedly call it unnecessarily. Even an empty Update isn't free.

    --Eric
     
  3. AcademyOfFetishes

    AcademyOfFetishes

    Joined:
    Nov 16, 2018
    Posts:
    219
    Although that's true, if the time wasted is imperceptible to the human eye, I'd prefer writing maintainable code over optimized code. In this case, the code would be easier to read if I wasn't checking if it changed from call to call, so I still think the question is worth asking.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    If you assign the same text to the text property, TMP should ignore it since it hasn't changed.

    Having said that and as per @Eric5h5 post, I would also advice avoiding setting properties on objects unless it is needed.
     
    AcademyOfFetishes likes this.