unity3d,异步加载场景

很简单,代码如下:

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;
	}
}


©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页