This is Loading script, I found this scrip on internet and i want to make this script autoload next scene ? I must place script in button and click it to activate script(loading next scene) I want to this script can autoload ok,maybe with timer , can someone help, thanks
using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Loading : MonoBehaviour { public GameObject loadingScreen; public Slider slider; public Text progressText; public void LoadLevel (int sceneIndex) { StartCoroutine(LoadAsynchronously(sceneIndex)); } IEnumerator LoadAsynchronously (int sceneIndex) { AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex); loadingScreen.SetActive(true); while (!operation.isDone) { float progress = Mathf.Clamp01(operation.progress / .9f); slider.value = progress; progressText.text = progress * 100f + “%”; yield return null; } } }
the code looks good to me. I added some notes to help you understand. attach this script to an empty GameObject, not the button gameObject.
You need a few UI gameObjects in the scene. and you need to link the button with an OnClick() event
I added a wait for 5 sec.
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Loading : MonoBehaviour
{
public GameObject loadingScreen; //not sure what object this should attached to, the UI slider?, by default it's not active, per line 22
public Slider slider; //some UI slider Slider object
public Text progressText; //some UI Text object
public void LoadLevel (int sceneIndex) //the button uses this with the OnClick() Event, you send an int(the int is the scene number in unity "build settings") to this via the button
{
StartCoroutine(LoadAsynchronously(sceneIndex)); //this start a Coroutine
}
IEnumerator LoadAsynchronously (int sceneIndex)
{
yield return new WaitForSeconds(5); // a wait timer, wait for 5 sec befor we load the scene
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
loadingScreen.SetActive(true); // active/enable the gameobject
while (!operation.isDone)//if the scene is not complete it's load, then stay in the loop
{
float progress = Mathf.Clamp01(operation.progress / .9f);
slider.value = progress; //move the UI slider bar
progressText.text = progress * 100f + "%";
yield return null;
}
}
}
This is video ( https://www.youtube.com/watch?v=YMj2qPq9CP8
)… I take script used in video. In this video u need to click button to start loading next scene, I want when I start game my first scene is “loading scene to Main Menu” and I dont want to click button, I want to autoload MainMenu scene.Ok?(That mean I dont need to click any button to start loading next scene , maybe after starting game few sec later is automatic start loading next scene(ManiMenu scene))
Watch these video’s on UI buttons, they go over the OnClick() Event system
other then that, Do you have the loading scene? IE a scene that starts before the main menu scene. if not you need to create one. that scene can then enable the UI loading bar stuff.
use a script to auto load the main menu
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadingScene : MonoBehaviour
{
public GameObject loadingScreen;
public Slider slider;
public Text progressText;
public int loadSceneIndex = 1; //what ever the scene number is
void Start() {
StartCoroutine(LoadAsynchronously(loadSceneIndex));
}
IEnumerator LoadAsynchronously (int sceneIndex)
{
yield return new WaitForSeconds(5);
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
loadingScreen.SetActive(true);
while (!operation.isDone)
{
float progress = Mathf.Clamp01(operation.progress / .9f);
slider.value = progress; //move the UI slider bar
progressText.text = progress * 100f + "%";
yield return null;
}
}
}
U dont understand me.This script go over OnClick() system, I dont want that .I want to autostart that script. (This script is loading script| When I start my game i want to first scene is loading scene to scene 2 (MainMenu) i dont want to click button to start loading I want to autostart this script…)Thank for everything!
Now it work, last time this script dont work when I use it, sorry and thanks. And how i can set a WaitForSeconds(5) to 0.5 or 0.1 when i set it there is error and sorry one more time…