- Home /
FadeIn/Out code optimisation
Hey All, i was wondering if theres anything i could do to optimise this code i wrote for a smooth fade in, not that its behaving slowly, i just want to get the hang of code optimisation, feel free to use it btw, it gives a lovely AAA feeling.
public var fadeInTexture : Texture2D;
public var fadeOutTexture : Texture2D;
public var fadeSpeed = 0.3;
public var fadeInMultiplier = -5;
public var fadeOutMultiplier = 1;
public var reloadWait = 2.0f;
public var newTimeScale = 0.1;
public var rampTimeIn = 0.05;
var drawDepth = -1000;
private var alpha = 1.0;
private var fadeDir = 0;
private var fadingIn = true;
function OnGUI()
{
alpha += fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
GUI.color.a = alpha;
GUI.depth = drawDepth;
if (fadingIn)
{
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeInTexture);
}
if (!fadingIn)
{
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
}
}
function fadeIn()
{
Time.timeScale = 0.1;
yield WaitForSeconds(0.01);
fadeDir = -fadeInMultiplier;
fadingIn = true;
}
function fadeOut()
{
fadeDir = fadeOutMultiplier;
fadingIn = false;
yield WaitForSeconds(reloadWait);
Application.LoadLevel(Application.loadedLevel);
}
function Start()
{
alpha=1;
Time.timeScale = newTimeScale;
fadeIn();
}
function FixedUpdate()
{
if (fadingIn)
{
newTimeScale+=rampTimeIn;
Time.timeScale = newTimeScale;
Time.fixedDeltaTime = 0.02 * Time.timeScale;
}
if (Time.timeScale >=1)
{
Time.timeScale = 1;
Time.fixedDeltaTime = 0.02 * Time.timeScale;
}
}
function OnTriggerEnter (other: Collider)
{
if(other.gameObject.tag != "Player")
{
var go:GameObject = other.gameObject;
Destroy(go);
}
else
{
fadeOut();
}
}
I haven't tested this, but the 2nd piece of optimization advice is not to use OnGUI() (of course, you can't use GUI.DrawTexture, then. Use a GUITexture.)
The first piece of optimization advice is that if it's for a menu, or anything where you aren't pushing the framerate, keep it easy to read and don't optimize.
Answer by Eric5h5 · Jul 19, 2013 at 02:26 PM
This is sub-optimal:
if (fadingIn) ...
if (!fadingIn) ...
Change it to
if (fadingIn) ...
else ...
A far bigger problem is that you always have a texture covering the screen. Even if it's 100% transparent, you're always drawing it, which burns fill rate for no reason (which can be a major issue on some platforms like the iPhone 4 where the GPU is inadequate for the native screen resolution).
Also, ideally you would not have code running at all if it's not necessary. OnGUI runs every frame, so for simple things (like a texture covering the screen), it's better to use a GUITexture object. You can disable it when it's not actually needed, and use a coroutine (like Fade) to only do the fading when necessary, so no code is running when no fading is occurring.
(Not that it has anything to do with performance, but your function names should be uppercase, so they are consistent with the Unity API, which makes code easier to read.)
Thank you, could you briefly explain why (!X) is more expensive than Else? feel free to talk techno...
Because you're executing two if statements ins$$anonymous$$d of one.
@Novaous: It's not. Or rather, it is, but the difference is so incredibly, incredibly $$anonymous$$or that, unless you are calling this function literally millions of times a second, it makes absolutely zero difference whatsoever in speed.
The real reason to write it the second way is that it's cleaner code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110432im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Circular Timer 1 Answer
Quick change over time math 2 Answers
Time Freezer 1 Answer