Unity3D的加载场景的代码

来源:互联网 发布:python现实世界 编辑:程序博客网 时间:2024/06/05 14:59

在加载场景使用如下代码,前提是有一个slider作为进度的提示

using UnityEngine;using System.Collections;using UnityEngine.SceneManagement;using System;using UnityEngine.UI;public class Loading : MonoBehaviour{    private float fps = 10.0f;    private float time;    private int nowFram;    AsyncOperation async;    //下一个场景的名字    public static string Scenename;    public static string LastScenename;    public Slider m_pProgress;    private int progress = 0;    // Use this for initialization    void Start ()    {        StartCoroutine(LoadScenes());    }    IEnumerator LoadScenes()    {        int nDisPlayProgress = 0;        async = SceneManager.LoadSceneAsync(Scenename);        async.allowSceneActivation = false;        while (async.progress < 0.9f)        {            progress = (int)async.progress * 100;            while (nDisPlayProgress < progress)            {                ++nDisPlayProgress;                m_pProgress.value = (float)nDisPlayProgress / 100;                yield return new WaitForEndOfFrame();            }            yield return null;        }        progress = 100;        while(nDisPlayProgress < progress)        {            ++nDisPlayProgress;            m_pProgress.value = (float)nDisPlayProgress / 100;            yield return new WaitForEndOfFrame();       }        async.allowSceneActivation = true;       // yield return async;    }}

随便挂在一个物体上都可以。