Unity:游戏场景异步加载

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;


public class LoadP : MonoBehaviour {

	//float timer = 0f;
	UISlider slider;//进度条
	int nowProgress, toProgress;//当前进度、异步加载进度
	AsyncOperation ao;//异步加载对象
	void Start () {
		slider = transform .GetChild(1).GetChild (0).GetComponent <UISlider >();//获取UI界面进度条
		StartCoroutine(LoadScene ());//开启异步加载协程
	}
		
	void Update () {
		//timer += Time.deltaTime;
		//slider.value = timer / 10;
		// async.progress 表示正在读取的场景的进度值  0---0.9    
		// 如果当前的进度小于0.9,说明它还没有加载完成,就说明进度条还需要移动    
		// 如果,场景的数据加载完毕,async.progress 的值就会等于0.9 
		if (ao.progress <0.9f)
        {
			toProgress = (int)(ao.progress *100);
        }
        else
        {
			toProgress = 100;
        }
        if (nowProgress <toProgress )
        {
			nowProgress++;//当前进度累加 注:在UI界面只能看到当前进度
        }
		slider.value = nowProgress / 100f;
        if (nowProgress ==100)//当当前进度达到100,跳转场景
        {
			ao.allowSceneActivation = true;
        }
    }
    IEnumerator LoadScene()
    {
		ao = SceneManager.LoadSceneAsync(2);//异步加载完成,跳转到场景2
		ao.allowSceneActivation = false;//阻止加载完自动跳转场景
		yield return ao;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值