Fade GUI from Coroutine?
Hi Everyone,
I'm having an issue with my Fade In and Fade Out coroutines:
private IEnumerator DoFadeIn(FadeState e)
{
float elapsedTime = 0.0f;
while (elapsedTime < fadeTime)
{
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
Color newColor = GUI.color;
newColor.a = 1.0f - Mathf.Clamp01(elapsedTime / fadeTime);
GUI.color = newColor;
GUI.depth = drawDepth;
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}
FadeManager.OnFadesDone(e);
}
private IEnumerator DoFadeOut(FadeState e)
{
float elapsedTime = 0.0f;
while (elapsedTime < fadeTime)
{
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
Color newColor = GUI.color;
newColor.a = Mathf.Clamp01(elapsedTime / fadeTime);
GUI.color = newColor;
GUI.depth = drawDepth;
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}
FadeManager.OnFadesDone(e);
}
Before, the issue I was having was that for some reason this script required an empty OnGUI function at the end to function properly. It took a long time to figure out that this was the problem, but after realizing I only needed the empty OnGUI function everything worked fine.
Today I built my project, and some things began to make sense. Nothing was working because the integrated Fades were halting any process with the error "You can only call GUI functions from inside OnGUI". So, what was happening was the empty OnGUI function was sufficient to trick the editor that the GUI functions were happening properly, but this trick won't work for my build.
So, this begs the question... How can I get these coroutines to do what they need to do without calling GUI? Or, alternatively, if I do have to incorporate OnGUI into my coroutines, how would I go about doing it? (Note: These coroutines are triggered by events, and also trigger events when complete.)
I should also mention that this is an Oculus project, and so I've been very hesitant about using GUI for anything since previous experiences with Oculus lead me to believe that any Overlay GUI elements or functions will be disregarded entirely, and I'm not sure whether the GUI.DrawTexture call would fall into that category. (I think it would.)
So, the ideal solution would be to recreate these coroutines with the $$anonymous$$imal required change to avoid using anything that starts with "GUI".
But what does that leave me with to use as a fade?
Answer by Ereptor · Jul 14, 2017 at 06:51 PM
I've decided to simply use an inverted sphere FadeObject to surround the camera. My fade routines now look like this:
private IEnumerator DoFadeIn(FadeState e)
{
float elapsedTime = 0.0f;
while (elapsedTime < fadeTime)
{
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
FadeMaterial.color = new Color(1.0f, 1.0f, 1.0f, 1.0f - Mathf.Clamp01(elapsedTime / fadeTime));
}
FadeObject.SetActive(false);
FadeManager.OnFadesDone(e);
}
private IEnumerator DoFadeOut(FadeState e)
{
FadeObject.SetActive(true);
float elapsedTime = 0.0f;
while (elapsedTime < fadeTime)
{
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
FadeMaterial.color = new Color(1.0f, 1.0f, 1.0f, Mathf.Clamp01(elapsedTime / fadeTime));
}
FadeManager.OnFadesDone(e);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612130252im_/https://answers.unity.com/themes/thub/images/avi.jpg)