Today's project is coming to an end, but I encountered a problem, the loading of the scene is relatively slow, and I immediately thought of asynchronous loading.
Then it took a while to study it.
The core class involved: AsyncOperation
The method attributes involved:
SceneManager.LoadSceneAsync() : The method of asynchronous loading, the parameter can be filled with serial number or scene name
allowSceneActivation : Whether the scene can be activated after loading
progress : value, the progress of scene loading, from 0.1 to 1
The front needs to make a slider, a text, and a button added in the previous scene. At the same time, the scene of the loading progress bar needs to be saved, and then added to the build setting
First scene:
Transition scene:
Second scene:
It is relatively simple to write, directly on the code:
private Slider slider;
private AsyncOperation async;
private int asyncValue;
private Text text_Slider;
// Use this for initialization
void Start () {
slider = GameObject.Find("Canvas/LoadSlider").GetComponent<Slider>();
text_Slider = GameObject.Find("Canvas/SliderText").GetComponent<Text>();
asyncValue = 0;
async = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex+1);
}
// Update is called once per frame
void Update () {
async.allowSceneActivation = false;
slider.value = Mathf.Lerp(slider.value,async.progress/9F*10F,Time.deltaTime);
text_Slider.text = "Loading:" + ((int)(slider.value * 100f)).ToString() + "%";
if (slider.value>=0.99f)
{
async.allowSceneActivation = true;
}
}