Skip to content

ScreenFader.cs Freezing and Abnormal Fading Speed When Frequently Called #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
TruthZY opened this issue Nov 21, 2023 · 0 comments
Open

Comments

@TruthZY
Copy link

TruthZY commented Nov 21, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant