您的位置:首页 > 移动开发 > Unity3D

unity3d,异步加载场景

2016-02-29 10:04 232 查看
很简单,代码如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Loading : MonoBehaviour {

public Slider loading_bar;
private AsyncOperation async_operation;

// Use this for initialization
void Start () {
StartCoroutine ("LoadScene");
}

// Update is called once per frame
void Update () {
loading_bar.value = async_operation.progress;
}

IEnumerator LoadScene(){
async_operation = Application.LoadLevelAsync ("scene name");
yield return async_operation;
}
}


unity5.3以后,推荐用scene manage,

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LoadSlider : MonoBehaviour
{

public Slider slider;
public GameObject panel;
private AsyncOperation async_operation;
private bool load_switch;

// Use this for initialization
void Start ()
{
panel.SetActive (false);
load_switch = false;
}

//显示加载进度
void Update ()
{
if (load_switch) {
slider.value = async_operation.progress;
}
}

//根据输入名加载场景
public void StartScene (string scene_name)
{
panel.SetActive (true);
load_switch = true;
StartCoroutine ("LoadScene", scene_name);
}

//异步加载场景
IEnumerator LoadScene (string scene_name)
{
async_operation = SceneManager.LoadSceneAsync (scene_name);
yield return async_operation;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: