Author Topic: Programmatically changing text in Label doesn't work first time.  (Read 4132 times)

reuben_nomad

  • Guest
Programmatically changing text in Label doesn't work first time.
« on: February 24, 2013, 03:03:36 PM »
Hello,
This is my first post on the NGUI forum.  I'm doing some in-game instructions and I have them working with Localization.  I am also resizing the text bkgd to fit the text.  I used the code example from UITooltip for the resizing and it works great.

Here's the problem I'm having:   I added in a couple of forced line breaks, so that the text is shifted upward to allow for a "Tap to Continue" text on the bottom.   

This works well EXCEPT for the first time.    When the function is called, it activates the UIlabel's parent, properly fills the UILabel's text field from the Localization text file, and resizes the bkgd, but the extra text isn't being added.   I tried to prefill the Label's text in the Inspector, but that didn't work either. 

Here's an abridged code snippet:

public void Instruct(string currentInstruction)
{
  instructionParent.SetActive(true);
  instructionLocalize.key = currentInstruction;
  instructionLocalize.Localize();
  //add in forced line breaks to leave room for the "Tap to continue" text
  instructionLabel.text += "\n\n";

  //resizing code happens here and works well
}

If anyone has any advice or experience with this, it would be much appreciated.
thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Programmatically changing text in Label doesn't work first time.
« Reply #1 on: February 24, 2013, 05:00:39 PM »
  1. instructionLabel.text = Localization.Localize(currentInstruction) + "\n\n";
There is no reason to have the UILocalize script if you are doing this via code.

reuben_nomad

  • Guest
Re: Programmatically changing text in Label doesn't work first time.
« Reply #2 on: February 24, 2013, 05:19:27 PM »
Thanks for the optimization.  I tried it,  but there's still the crazy behavior of it not working on first showing.  My code now reads:

instructionParent.SetActive(true);
instructionLabel.text = Localization.Localize(currentInstruction) + "\n\n";

The first time an instruction pops up, it doesn't have the extra lines at the bottom.  The strange thing is, I noticed that it must be accounting for the hard returns, because there is extra space at the top, and the resizing is taking that into account. 

I attached 2 images.   The first capture is from the first time the Instruct function is called, and the 2nd capture is same instruction being called a 2nd time.

The instruction label has a bottom pivot, and the "tap to continue"  is a separate label (consistent in all instructions) that has a bottom pivot as well.

thanks again for your help.

milali

  • Guest
Re: Programmatically changing text in Label doesn't work first time.
« Reply #3 on: February 25, 2013, 05:44:10 AM »
this is due to the activation code.

try applying your text 1 frame after enabling it.

So start a co-routine which waits for end of frame and then applies the text

that should fix it.

if anyone knows a better way I am ALL EARS as I dislike having to have so many short term co-routines for this stuff!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Programmatically changing text in Label doesn't work first time.
« Reply #4 on: February 25, 2013, 07:01:38 AM »
I always set the values, then enable the object.

milali

  • Guest
Re: Programmatically changing text in Label doesn't work first time.
« Reply #5 on: February 25, 2013, 09:29:16 AM »
ohhh really? interesting? I found that didn't work with sprites.. but maybe cause activation was before the enable call..

so if we flip it around and enable before we set the sprite we're swapping to it should all be dandy?

reuben_nomad

  • Guest
Re: Programmatically changing text in Label doesn't work first time.
« Reply #6 on: February 25, 2013, 11:16:46 AM »
Yes,  removing the UILocalize script and doing it manually worked.  I misunderstood the first reply, and the text label still had the UILocalize script on it.  So basically the text was being set correctly manually, but the UI localize was overwriting it without the appended breaks, at some point ( I still had a default key in for the first instruction).
The code is now the same as it was on my second post, and works great.  thanks for the responses.

instructionParent.SetActive(true);
instructionLabel.text = Localization.Localize(currentInstruction) + "\n\n";