agiro agiro - 3 months ago 13
C# Question

c# disable code region on condition

So the question is this: I have states in my code. I want to divide them to regions and simply enable or disable them upon some conditions. The code is in a looping Unity function named LateUpdate. I wish to do this to save up on processor time instead of using booleans or enums.
Can I do that? if so, how?
Add. info: I need to do this disabling on runtime. The script is an image sequence player that plays gifs. I have a warm up part then a looping part. I can write it with bools but as I said I need to save on processor time as this will be a mobile game.

some code:

void LateUpdate ()
{
if (warmUp)
{
currentImageIndex = Mathf.RoundToInt(Time.time * frameRate);
currentImageIndex = currentImageIndex % spriteSheet.Length;
spriteRenderer.sprite = spriteSheet[currentImageIndex];
warmUp = false;
}
if (updateEnabled && !warmUp)
{
switch (playMode)
{
case PlayMode.order:
currentImageIndex = Mathf.RoundToInt(Time.time * frameRate);
currentImageIndex = currentImageIndex % spriteSheet.Length;
spriteRenderer.sprite = spriteSheet[currentImageIndex];
break;
case PlayMode.random:
updateEnabled = false;
StartCoroutine(RandomizedPlay());
break;
}
}

}

Answer

Simple if checks like you are currently doing already is the correct way to disable code at runtime that does not always need to run inside a function.

You should learn how to use the unity profiler on the mobile device, the source of your slowdown (if you are having any) are not from your if statements themselves.

I would say it is extremely likely that each of those if checks won't even get above 0.00% of the CPU time during the profiler.