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


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值