I can confirm that this is with CrossFadeAlpha
. The workaround is to get the current color, set the Color.a
to 1
then set it back to 0
with CrossFadeAlpha(0f, 0f, true);
. After that, you can then call your screen.CrossFadeAlpha(1, fadeTime, false);
function.
You wont see the effect of setting Color.a
to 1
because you are setting it back to 0
with screen.CrossFadeAlpha(0f, 0f, true);
in the-same frame.
This is the proper workaround and should fix this problem:
Color fixedColor = screen.color;
fixedColor.a = 1;
screen.color = fixedColor;
screen.CrossFadeAlpha(0f, 0f, true);
screen.CrossFadeAlpha(1, fadeTime, false);
You can also make it an extension method so that it can be used from any where in your project and on any UI component:
public static class ExtensionMethod
{
public static void CrossFadeAlphaFixed(this Graphic img, float alpha, float duration, bool ignoreTimeScale)
{
//Make the alpha 1
Color fixedColor = img.color;
fixedColor.a = 1;
img.color = fixedColor;
//Set the 0 to zero then duration to 0
img.CrossFadeAlpha(0f, 0f, true);
//Finally perform CrossFadeAlpha
img.CrossFadeAlpha(alpha, duration, ignoreTimeScale);
}
}
Usage :
private bool Starting = false;
public Image screen;
float fadeTime = 3f;
public void StartGame()
{
Starting = true;
StartCoroutine(wait());
screen.CrossFadeAlphaFixed(1, fadeTime, false);
}
I noticed that you have StartCoroutine(wait());
in your code. It looks like you simply want to wait until the fading is done then do something(load new scene). I suggest you roll your own fade function that takes a callback function with Action
.
public void StartGame()
{
Starting = true;
CrossFadeAlphaWithCallBack(screen, 1f, fadeTime, delegate
{
Debug.Log("Done Fading");
SceneManager.LoadScene(1);
});
}
void CrossFadeAlphaWithCallBack(Image img, float alpha, float duration, System.Action action)
{
StartCoroutine(CrossFadeAlphaCOR(img, alpha, duration, action));
}
IEnumerator CrossFadeAlphaCOR(Image img, float alpha, float duration, System.Action action)
{
Color currentColor = img.color;
Color visibleColor = img.color;
visibleColor.a = alpha;
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
img.color = Color.Lerp(currentColor, visibleColor, counter / duration);
yield return null;
}
//Done. Execute callback
action.Invoke();
}
Again, you can make it an extension method so that it can be used anywhere in your project.
public static class ExtensionMethod
{
public static void CrossFadeAlphaWithCallBack(this Image img, float alpha, float duration, System.Action action)
{
MonoBehaviour mnbhvr = img.GetComponent<MonoBehaviour>();
mnbhvr.StartCoroutine(CrossFadeAlphaCOR(img, alpha, duration, action));
}
public static IEnumerator CrossFadeAlphaCOR(Image img, float alpha, float duration, System.Action action)
{
Color currentColor = img.color;
Color visibleColor = img.color;
visibleColor.a = alpha;
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
img.color = Color.Lerp(currentColor, visibleColor, counter / duration);
yield return null;
}
//Done. Execute callback
action.Invoke();
}
}
Usage:
private bool Starting = false;
public Image screen;
float fadeTime = 3f;
public void StartGame()
{
Starting = true;
screen.CrossFadeAlphaWithCallBack(1f, fadeTime, delegate
{
Debug.Log("Done Fading");
SceneManager.LoadScene(1);
});
}