Question by
DaresFireson · May 30, 2019 at 03:46 PM ·
c#scenefade
My scene don't load immediately
I have a fade out fade in script which work fine in other games. It was working fine even in the game I currently working but for unknown reasons it started to fade out slowly out of the current scene without any black screen. here is my fade out/fade in script but I don't think this is the issue
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SceneFader : MonoBehaviour {
public Image img;
public AnimationCurve curve;
private float _progress = 0f;
public Text loadingText;
private void Start()
{
StartCoroutine(FadeIn());
}
public void FadeTo(string scene)
{
StartCoroutine(FadeOut(scene));
}
IEnumerator FadeIn()
{
float t = 1f;
while(t > 0f)
{
t -= Time.deltaTime;
float a = curve.Evaluate(t);
img.color = new Color(0f, 0f, 0f, a);
yield return 0;
}
}
IEnumerator FadeOut(string scene)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(scene);
operation.allowSceneActivation = false;
while(_progress < 1f)
{
_progress = Mathf.Clamp01(operation.progress / 0.9f);
loadingText.text = "Loading... " + (int)(_progress * 100f) + "%";
//Debug.Log("Loading... " + (int)(_progress * 100f) + "%");
yield return null;
}
float t = 0f;
while (t < 1f)
{
t += Time.deltaTime;
float a = curve.Evaluate(t);
img.color = new Color(0f, 0f, 0f, a);
loadingText.color = new Color(1f, 1f, 1f, a);
yield return 0;
}
//SceneManager.LoadScene(scene);
operation.allowSceneActivation = true;
}
}
Comment
Your answer
Follow this Question
Related Questions
Why Scene does not open after scene fading? C# 1 Answer
Terrain Ground, lerp Grass and Snow in Cities: Skylines in C# 0 Answers
Unable to Compile scripts on my game but my Team mates can on their machines?CS0103 0 Answers
Text adventure game, how to change text in a specific way with C# 1 Answer
I want my UI rawimage to do something when the movie texture has finished 1 Answer