Issue Description
There are issues with "ScreenFader.cs" when it is frequently called, such as freezing and abnormal fading speed. For instance, when I switch scenes in quick succession, like entering a scene and immediately exiting to the main menu, it may freeze.
Steps to Reproduce
Create a new script:
public class Test : MonoBehaviour
{
public bool isFading; // for observation
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
StartCoroutine(ScreenFader.FadeSceneIn());
}
if (Input.GetKeyDown(KeyCode.R))
{
StartCoroutine(ScreenFader.FadeSceneOut());
}
isFading = ScreenFader.IsFading;
}
}
Set Fade Duration to 1 or above, repeatedly pressing R and E will trigger the issue.
Solution
In the file Gamekit3D/Assets/3DGamekit/Packages/SceneManagement/Runtime/ScreenFader.cs, modify the Fade method as follows:
protected IEnumerator Fade(float finalAlpha, CanvasGroup canvasGroup)
{
m_IsFading = true;
canvasGroup.blocksRaycasts = true;
float fadeSpeed = Mathf.Abs(canvasGroup.alpha - finalAlpha) / fadeDuration;
// modify
while (Mathf.Abs(canvasGroup.alpha - finalAlpha) > 0.1f)
{
canvasGroup.alpha = Mathf.MoveTowards(canvasGroup.alpha, finalAlpha,
fadeSpeed * Time.deltaTime);
yield return null;
}
canvasGroup.alpha = finalAlpha;
m_IsFading = false;
canvasGroup.blocksRaycasts = false;
}
When alpha, finalAlpha, fadeSpeed*Time.deltaTime is close to 0, because the precision of the floating-point number loses the correct result.