unity的异步加载场景

**

UNITY的异步加载场景

**



using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine .UI;

using UnityEngine.SceneManagement ;

public class loadscene : MonoBehaviour {



    public Slider slider;

    public string scene_name;

    private int to_process;

    private int now_process;



    public void On_click(){

        

        StartCoroutine (load_scene());

    }

    IEnumerator load_scene(){

        AsyncOperation asy= SceneManager.LoadSceneAsync (scene_name);

        asy.allowSceneActivation = false;

        while (asy.progress < 0.9f) {

            to_process = (int)asy.progress * 100;

            now_process ++;

            slider.value = now_process  / 100;

            yield return new WaitForEndOfFrame ();

        }



        to_process = 100;



        while (now_process < to_process) {

            now_process ++;

            slider.value = now_process  / 100;

            yield return new WaitForEndOfFrame ();

        }



        if (now_process ==100)

            asy.allowSceneActivation = true;

        



    }



}



把代码挂在摄像机上;再创建一个按钮和slider;

这样简单的异步加载就完成了。

发布了10 篇原创文章 · 获赞 0 · 访问量 143

猜你喜欢

转载自blog.csdn.net/qq_39121279/article/details/104233358