Unity_异步加载场景

来源:互联网 发布:淘宝产品推销方案 编辑:程序博客网 时间:2024/06/16 01:45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
 
public class GameScenesManager : MonoBehaviour {
 
    AsyncOperation asynOp;
    public Scrollbar processBar;
    public Button enterScene;
    public Text tip;
 
    void Start () {
        processBar.size = 0;
        enterScene.onClick.AddListener(EnterGameScene);
        processBar.gameObject.SetActive(false);
        tip.gameObject.SetActive(false);
    }
 
    /// <summary>
    /// 进入游戏场景
    /// </summary>
    void EnterGameScene()
    {
        processBar.gameObject.SetActive(true);
        tip.gameObject.SetActive(true);
        StartCoroutine(LoadGameScene());
    }
 
 
    IEnumerator LoadGameScene()
    {
        tip.text = "正在加载...请稍后";
        asynOp = SceneManager.LoadSceneAsync("AsynLoadScene_1");
        while (!asynOp.isDone)
        {//如果场景没有加载完毕
            processBar.size = asynOp.progress;
            yield return null;
        }
 
        yield return new WaitForSeconds(0.5f);
        tip.text = "加载完成";
        asynOp.allowSceneActivation = true;
    }
}