- Home /
Any ideas why this might lag on iPhone 4?
hello
I have this piece of code that lerps UI Text's font size up and down to achieve a pulsating effect. If I do this by animating the scale the entire rect transform comes down while growing and it just doesn't look as i need it to, and I'd rather have a direct control on pulses than change animation every time anyway.
bool hsDoOnce = true
bool hsGrow = true
void Update()
{
if(hsReached)
NewHighscore();
}
public void NewHighscore()
{
if(hsDoOnce)
{
SoundPlayer.get.PlaySound (SoundName.HighScore);
hsLerpTime = 0;
hsDoOnce = false;
}
if(hsGrow)
{
levelCounter.fontSize = Mathf.CeilToInt(Mathf.Lerp(100, hsFinalSize, (hsLerpTime += Time.deltaTime)* 9));
if(levelCounter.fontSize >= hsFinalSize)
{
hsLerpTime = 0;
hsGrow = false;
}
}
else
{
levelCounter.fontSize = Mathf.FloorToInt(Mathf.Lerp(hsFinalSize, 100, (hsLerpTime += Time.deltaTime)* 9));
if(levelCounter.fontSize <= 100)
{
hsPulseCounter--;
hsLerpTime = 0;
hsGrow = true;
if(hsPulseCounter <= 0)
hsReached = false;
}
}
}
This is fine in the engine but causes a lag spike on iPhone 4. Any ideas why may this code be so heavy?
Well, the iPhone 4 has half a GB of ram... Your computer has a lot more.
thank you for stating the obvious :D I'm asking, why would this code cause a lag spike? I know iPhone 4 is old but it should be able to handle this no?
Answer by pfreese · Mar 24, 2015 at 02:30 AM
Changing the font size every frame will cause a new texture atlas for the font to be regenerated every frame, which is something to avoid. Look for alternative ways to scale your high score message. Since the text isn't changing, you could either use an image, or generate an image with the text at runtime.
Hmm. I am doing something similar during game over, the font doesn't really pulsate there but it lerps from 100 to 300 and it didn't seem to lag, so I thought why not do the same here. The reason I opted for this in the first place was because I couldn't find a good alternative, just scaling the game object makes the image look bad. I see your point now. This effect happens mid game, so there is a constant chance that the text may change. This can happen during the pulses, so for example your high score was 17, you just reached 18, it starts to pulsate and in this moment you go to 19. I don't think I can get away with swapping an image there...
Thanks for pointing out the problem!
I'll try to find a way around this
Answer by Calum1015 · Mar 24, 2015 at 12:20 AM
You are changing the Text every frame I believe, If you put a timer on it so it pulses every couple seconds it would be much, much more efficient on ram.
yeah, I change it every frame. You mean ins$$anonymous$$d of lerping every frame, I should make it instantly jump to certain size every 2 seconds for example? I won't get the smooth grow/shrink effect then... or did I misunderstand your suggestion?
Your answer
Follow this Question
Related Questions
Lag when re-rendering TextUI. 1 Answer
Iphone gui text 2 Answers
Ipod touch 2g lag on application.levelload 1 Answer
Detect Text Box Overflow 3 Answers