Author Topic: UILabel.text garbage from string array  (Read 3050 times)

coxy17

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
UILabel.text garbage from string array
« on: March 30, 2017, 09:26:12 AM »
Hi

I am setting UILabel.text using 'GetComponent<UILabel>();' and I am updating the text from a string array.
when i update this i get garbage being generated, is this just a unity profiler issue and wont appear during a build?

I ask this as i have done a test using the same code but with UGUI instead and i get no garbage when updating the ui text.

All i can find is this post http://www.tasharen.com/forum/index.php?topic=11000.0 but i can see that setting a text value for a UILabel allocates memory. Is this still the case?

Nick

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel.text garbage from string array
« Reply #1 on: March 31, 2017, 01:09:01 PM »
If you turn off deep profiling, you will see the notes about allocations only happening in the editor -- but I'm pretty sure WrapText isn't going to be affected here. Inside WrapText a new string builder is allocated. This can probably be made static to avoid the allocation. Declare it outside the function:
  1. static StringBuilder sb;
And in place where it was declared before, instead have:
  1.                 if (sb == null) sb = new StringBuilder();
  2.                 else sb.Length = 0;

coxy17

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: UILabel.text garbage from string array
« Reply #2 on: April 04, 2017, 04:39:59 AM »
Hi

I'm not sure exactly what to do, im not suing the same code as the link in the post. But i am making a Playmaker action from a script i found online (which ive pasted below). Is this still possible with this approach? sorry i'm still learning and not sure how to apply what you said to this following code. My fault as i forgot to paste this in my first post. You can see that if you use NGUI its there's garbage, if i use UGUI and comment out NGUI then i get no garbage.

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class garbagefreetext : MonoBehaviour {
  5.  
  6.         UILabel _NguiLabel; //NGUI
  7.         //Text _UguiLabel; //uGUI
  8.          
  9.         string[] generated;
  10.          
  11.         void Start () {
  12.                
  13.                 _NguiLabel = GetComponent<UILabel>(); //NGUI
  14.                 //_UguiLabel = GetComponent<Text>(); //uGUI
  15.          
  16.                 generated = new string[100];
  17.          
  18.                 // go from 0 to 99.
  19.                 for (int i = 0; i < 100; i++) {
  20.                         generated [i] = string.Format ("{0:00}", i);
  21.                 }
  22.         }
  23.          
  24.         void Update () {
  25.                 int random = UnityEngine.Random.Range (0, generated.Length);
  26.                 _NguiLabel.text = generated [random]; //NGUI
  27.                 //_UguiLabel.text = generated [random]; //uGUI
  28.         }
  29.          
  30. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel.text garbage from string array
« Reply #3 on: April 04, 2017, 05:41:57 AM »
By default NGUI parses and wraps text. If you change your label to not use symbols (uncheck that checkbox) and change the wrapping to be "resize freely", you will avoid the need to make any modifications I mentioned in the previous post.

That said, you're trying to tackle a task for a dev, but based on your mention of Playmaker I am guessing you're more of an artist. If my code modifications don't make much sense to you, you can just wait for the next version of NGUI. I've added the change locally, so it will be there in the next version.

coxy17

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: UILabel.text garbage from string array
« Reply #4 on: April 04, 2017, 06:30:02 AM »
Ok yes im more of an artist but just creating NGUI action for this and others in the playmaker community for NGUI if it works for me as this would be really useful. Love NGUI myself over all the other assets ive used as an artist in Unity. I have done loads of making C# scripts into playmaker actions but i knew its more of a dev job, but i tried to tackle this as a one off task as i need this for my own project.

i tried what you said about the wrapping to be 'resize freely' and i cant seem to see the uncheck box 'to not use symbols' using the encoding checkbox. (screenshot attached). I tried it again with this but still get garbage, must be as i cant find the symbol checkbox.

Ok well if there is going to be in the next version of NGUI then that's great! appreciate it. So does that mean in the next version, do i have to use 'resize freely' or can i use any other 'overflow' setting.

Nick

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel.text garbage from string array
« Reply #5 on: April 07, 2017, 05:38:23 PM »
The symbols box looks unchecked already, so not sure why you're saying you can't seem to uncheck it.

I recommend on focusing on something that will actually affect your gameplay's performance though. Even doing nothing, 40 bytes allocation per text change is not going to cause anything noticeable on your end. If it was 40 kb, then it would matter, but 40 bytes? Not quite sure why you're looking into this in the first place, to be honest.