Skip to content

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

@TruthZY

Description

@TruthZY

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions