OOIII/

Unity场景Loading过渡界面

目前项目有两个场景,从第一个场景跳转第二个场景是会卡很久,因为第二个场景资源比较多,所以考虑异步加载并添加一个高大上的loading条。这需要一个中间场景来过渡,具体操作网上有,关键代码如下:

using UnityEngine;
using System.Collections;

public class Loading : MonoBehaviour
{
    public UISlider us;
    // Use this for initialization
    void Start ()
    {
        StartCoroutine(LoadScene (2));
    }

    // Update is called once per frame
    private IEnumerator LoadScene( int scene)
    {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation status = Application .LoadLevelAsync(scene );

        status.allowSceneActivation = false ;

        while (status.progress < 0.9f)
        {
            toProgress = (int)status.progress * 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                us.value = displayProgress * 0.01f;
                yield return null;
            }
            yield return null;
        }
        toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            us.value = displayProgress * 0.01f;
            yield return null;
        }
        status.allowSceneActivation = true ;
        yield return status;
    }
}

这段代码的关键在于,LoadScene函数每种状态下都会有返回值,否则edit模式下不报错,导出exe或者apk后会卡死在loading界面。
以上代码根据网络资源略作修改而来。