美文网首页
Unity异步加载场景

Unity异步加载场景

作者: 李狗多 | 来源:发表于2020-03-24 21:28 被阅读0次

在一些过于庞大的场景加载中,首先跳转到一个空场景,空场景中有加载条,使用异步加载到另一个场景中,增加用户体验。

using UnityEngine;

using System.Collections;

using UnityEngine.SceneManagement;

using UnityEngine.UI;

public class LoadingScene : MonoBehaviour

{

    public Image progressImg;

    private AsyncOperation async;

    public Text Percentage;

    public float min_time = 5;

    public float timer = 0;

    private int curProgressVaule = 0;//计数器

    // Use this for initialization

    void Start()

    {

        StartCoroutine(LoadScene());

    }

    IEnumerator LoadScene()

    {

        async = SceneManager.LoadSceneAsync("Scene2");//异步跳转到game场景

        async.allowSceneActivation = false;//当game场景加载到90%时,不让它直接跳转到game场景。

        yield return async;

    }

    // Update is called once per frame

    void Update()

    {

        timer += Time.deltaTime;

        if (async == null)

        {

            return;

        }

        int progressVaule = 0;

        if (async.progress < 0.9f)

        {

            progressVaule = (int)async.progress * 100;

        }

        else

        {

            progressVaule = 100;

        }

        if (curProgressVaule < progressVaule)

        {

            curProgressVaule++;

        }

        Percentage.text = curProgressVaule + "%";

        progressImg.fillAmount = curProgressVaule / 100f;

        if (curProgressVaule == 100&& timer>min_time)

        {

            async.allowSceneActivation = true;

        }

    }

}

相关文章

网友评论

      本文标题:Unity异步加载场景

      本文链接:https://www.haomeiwen.com/subject/cznayhtx.html