unity3d异步加载场景

根据宣雨松前辈的教程来做的,因为我用到的场景不是在游戏里的那种(本人做增强现实的,完全把unity拿来做应用了=。=),所以这里的方法不是很全面,原文戳这里:点击打开链接


异步加载流程:


    lovdlevel                  异步读取
A---------------> B ------------------------>C
                                 播放加载动画


示例背景介绍:A场景是一个菜单,鼠标选中其中一项,则跳转到相应场景(C场景),B场景为加载动画播放场景

A场景:摄像机添加menu.cs
B场景:绑定loading.cs,把loading图片附到loading.cs

menu.cs:

using UnityEngine;
using System.Collections;

public class Globe{
         //在这里记录当前切换场景的名称
	 public static string loadName;
}
public class menu : MonoBehaviour {

	// Use this for initialization
	void Start () 
    {
        
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (Input.GetKey(KeyCode.Escape))
            Application.Quit();
        Camera cam = Camera.mainCamera;
        if (Input.GetMouseButtonDown(0))
        { 
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit) )
            {
                switch (hit.collider.name)
                {
                    
                    case "***":
                        Globe.loadName = "C";
			Application.LoadLevel ("B");    
                        break;
                    case "***":
			Globe.loadName = "CCC";
			Application.LoadLevel ("B");
			break;  
				
                }                
            }
        }    	
    }
}

loading.cs

using UnityEngine;
using System.Collections;

public class loading : MonoBehaviour {

	private float fps = 1000.0f;
	private float time;
	//一组动画的贴图,在编辑器中赋值。
	public Texture2D[] animations;
	private int nowFram;
	//异步对象
	AsyncOperation async;

	//读取场景的进度,它的取值范围在0 - 1 之间。
	int progress = 1;

	void Start()
	{
		//在这里开启一个异步任务,
		//进入loadScene方法。
	  	StartCoroutine(loadScene());
	}

	//注意这里返回值一定是 IEnumerator
	IEnumerator loadScene()
	{
		//异步读取场景。
		//Globe.loadName 就是A场景中需要读取的C场景名称。
		async = Application.LoadLevelAsync(Globe.loadName);

		//读取完毕后返回, 系统会自动进入C场景
		yield return async;

	}

	void OnGUI()
	{
		//因为在异步读取场景,
		//所以这里我们可以刷新UI
		DrawAnimation(animations);

	}

	void Update()
	{

		//在这里计算读取的进度,
		//progress 的取值范围在0.1 - 1之间, 但是它不会等于1
		//也就是说progress可能是0.9的时候就直接进入新场景了
		//所以在写进度条的时候需要注意一下。
		//为了计算百分比 所以直接乘以100即可
		progress =  (int)(async.progress *100);

		//有了读取进度的数值,大家可以自行制作进度条啦。
		Debug.Log(progress);

	}
	//简单绘制2D动画
	void  DrawAnimation(Texture2D[] tex)
	{

		time += Time.deltaTime;

		 if(time >= 1.0 / fps){

      	 	 nowFram++;

      	 	 time = 0;

      	 	 if(nowFram >= tex.Length)
      	 	 {
      	 	 	nowFram = 0;
      	 	 }
        }
		
		GUI.DrawTexture(new Rect( (Screen.width-60)*0.5f,(Screen.height-60)*0.5f,60,60) ,tex[nowFram] );
                //在这里显示读取的进度。
		GUI.Label(new Rect( (Screen.width-100)*0.5f,(Screen.height-60)*0.5f+100,100,60), "LOADING..." + progress+"%");

	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值