- Home /
Question by
nightmaregiba · Nov 13, 2016 at 01:25 PM ·
lightingloading screenloadlevelasyncprogress-bar
SceneManager.LoadSceneAsync() problem on Andorid
Hi everyone,
I have use thiss code to load a new Scene and display the progress to the users
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
public class LoadingScreenManager : MonoBehaviour
{
[Header("Loading Visuals")]
public Text loadingText;
public Image progressBar;
[Header("Loading Settings")]
public LoadSceneMode loadSceneMode = LoadSceneMode.Single;
public ThreadPriority loadThreadPriority;
AsyncOperation operation;
Scene currentScene;
public static int sceneToLoad = -1;
static int loadingSceneIndex = 1; // !!!change this
public static void LoadScene(int levelNum)
{
Application.backgroundLoadingPriority = ThreadPriority.High;
sceneToLoad = levelNum;
SceneManager.LoadScene(loadingSceneIndex);
}
void Start()
{
if (sceneToLoad < 0)
return;
currentScene = SceneManager.GetActiveScene();
StartCoroutine(LoadAsync(sceneToLoad));
}
private float currentProgress = 0f;
private IEnumerator LoadAsync(int levelNum)
{
yield return null;
StartOperation(levelNum);
float lastProgress = 0f;
// operation does not auto-activate scene, so it's stuck at 0.9
while (DoneLoading() == false)
{
yield return null;
if (Mathf.Approximately(operation.progress, lastProgress) == false)
{
currentProgress = operation.progress;
if (currentProgress < 0.85)
{
progressBar.fillAmount = currentProgress;
loadingText.text = ((int)((progressBar.fillAmount) * 100)) + " %";
}
else
{
progressBar.fillAmount = 1;
loadingText.text = "100 %";
}
lastProgress = operation.progress;
}
}
if (loadSceneMode == LoadSceneMode.Additive)
SceneManager.UnloadScene(currentScene.name);
else
operation.allowSceneActivation = true;
}
private void StartOperation(int levelNum)
{
Application.backgroundLoadingPriority = loadThreadPriority;
operation = SceneManager.LoadSceneAsync(levelNum, loadSceneMode);
if (loadSceneMode == LoadSceneMode.Single)
operation.allowSceneActivation = false;
}
private bool DoneLoading()
{
return (loadSceneMode == LoadSceneMode.Additive && operation.isDone) || (loadSceneMode == LoadSceneMode.Single && operation.progress >= 0.9f);
}
}
Yes, I can load new scene but there is a problem with progress bar when I build for Android devices First, the progress will load to 14%, then return to 0% then load to 100% and switch to new Scene
What's wrong with these code? Why does it return to 0%?
Thanks in advance!
Comment