Morning everyone… i’m having an issue that i can’t seem to figure out a better solution for. We have a timer in our game thats represented by a clock on screen that uses the code below. When the game is played in normal mode (aka non-timed) the framerate is great… but when the same exact level is played in timed mode we get very visible lag. The levels are identical except for the timer. Here’s the code we’re using…
string PrintTimer()
{
string TimeStr = Minutes().ToString();
TimeStr+=":";
int sec = Seconds();
if (sec < 10)
TimeStr+="0";
TimeStr += sec.ToString();
return(TimeStr);
}
void Update ()
{
if(isTimedOn == true Paused == false)
{
if(Timer != null)
Timer.Text = PrintTimer();
if(Timer !=null TimeRemaining() <= 60 Timer.gameObject.GetComponent<GroupFader>().bFadeLoop == false flashCount != 3)
{
Timer.gameObject.renderer.material.SetColor("_Color", Color.red);
Timer.gameObject.GetComponent<GroupFader>().bFadeLoop = true;
Timer.gameObject.GetComponent<GroupFader>().FadeOut();
}
if (Timer !=null Timer.gameObject.GetComponent<GroupFader>().bFadeLoop == true flashCount == 3)
{
Timer.gameObject.GetComponent<GroupFader>().bFadeLoop = false;
//Timer.gameObject.GetComponent<GroupFader>().FadeIn();
Timer.gameObject.GetComponent<ShowHide>().Show(true);
}
if(Timer !=null TimeRemaining() <= 15 Timer.gameObject.GetComponent<GroupFader>().bFadeLoop == false)
{
Timer.gameObject.GetComponent<GroupFader>().bFadeLoop = true;
Timer.gameObject.GetComponent<GroupFader>().FadeOut();
}
if(Timer !=null TimeRemaining() <= 0)
{
Debug.Log("times up!");
Timer.Text = "";
TimeModeFail.Show(true);
}
}
}
public void addFlashCount()
{
flashCount = flashCount + 1;
}
public int Minutes()
{
return (TimeRemaining()/60);
}
public int Seconds()
{
return (TimeRemaining() % 60);
}
public int TimeRemaining()
{
int curTime = (int) Time.time;
int TimeElapsed = curTime - (LevelStartTime + pauseTime);
int TimeLeft = LevelDuration - TimeElapsed;
if(addToTime != 0)
{
addToTime = 0;
return(TimeLeft + addToTime);
}
else if (subFromTime != 0)
{
return (TimeLeft - subFromTime);
}
else
{
return (TimeLeft);
}
}
Curious if anyone out there has any advice on optimizing the above code or better yet a simpler implementation of a timer on screen (that shows in minutes and seconds). Right now the slowdown is significant. it’s not constant… more like periodic spots of lag that happen quite often.
Cheers and thanks for the help!
Bryan