unity3d异步加载场景

111 篇文章 3 订阅
20 篇文章 1 订阅

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


异步加载流程:


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


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

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

menu.cs:

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Globe{  
  5.          //在这里记录当前切换场景的名称  
  6.      public static string loadName;  
  7. }  
  8. public class menu : MonoBehaviour {  
  9.   
  10.     // Use this for initialization  
  11.     void Start ()   
  12.     {  
  13.           
  14.     }  
  15.       
  16.     // Update is called once per frame  
  17.     void Update ()   
  18.     {  
  19.         if (Input.GetKey(KeyCode.Escape))  
  20.             Application.Quit();  
  21.         Camera cam = Camera.mainCamera;  
  22.         if (Input.GetMouseButtonDown(0))  
  23.         {   
  24.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);  
  25.             RaycastHit hit;  
  26.             if (Physics.Raycast(ray, out hit) )  
  27.             {  
  28.                 switch (hit.collider.name)  
  29.                 {  
  30.                       
  31.                     case "***":  
  32.                         Globe.loadName = "C";  
  33.             Application.LoadLevel ("B");      
  34.                         break;  
  35.                     case "***":  
  36.             Globe.loadName = "CCC";  
  37.             Application.LoadLevel ("B");  
  38.             break;    
  39.                   
  40.                 }                  
  41.             }  
  42.         }         
  43.     }  
  44. }  

loading.cs

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class loading : MonoBehaviour {  
  5.   
  6.     private float fps = 1000.0f;  
  7.     private float time;  
  8.     //一组动画的贴图,在编辑器中赋值。  
  9.     public Texture2D[] animations;  
  10.     private int nowFram;  
  11.     //异步对象  
  12.     AsyncOperation async;  
  13.   
  14.     //读取场景的进度,它的取值范围在0 - 1 之间。  
  15.     int progress = 1;  
  16.   
  17.     void Start()  
  18.     {  
  19.         //在这里开启一个异步任务,  
  20.         //进入loadScene方法。  
  21.         StartCoroutine(loadScene());  
  22.     }  
  23.   
  24.     //注意这里返回值一定是 IEnumerator  
  25.     IEnumerator loadScene()  
  26.     {  
  27.         //异步读取场景。  
  28.         //Globe.loadName 就是A场景中需要读取的C场景名称。  
  29.         async = Application.LoadLevelAsync(Globe.loadName);  
  30.   
  31.         //读取完毕后返回, 系统会自动进入C场景  
  32.         yield return async;  
  33.   
  34.     }  
  35.   
  36.     void OnGUI()  
  37.     {  
  38.         //因为在异步读取场景,  
  39.         //所以这里我们可以刷新UI  
  40.         DrawAnimation(animations);  
  41.   
  42.     }  
  43.   
  44.     void Update()  
  45.     {  
  46.   
  47.         //在这里计算读取的进度,  
  48.         //progress 的取值范围在0.1 - 1之间, 但是它不会等于1  
  49.         //也就是说progress可能是0.9的时候就直接进入新场景了  
  50.         //所以在写进度条的时候需要注意一下。  
  51.         //为了计算百分比 所以直接乘以100即可  
  52.         progress =  (int)(async.progress *100);  
  53.   
  54.         //有了读取进度的数值,大家可以自行制作进度条啦。  
  55.         Debug.Log(progress);  
  56.   
  57.     }  
  58.     //简单绘制2D动画  
  59.     void  DrawAnimation(Texture2D[] tex)  
  60.     {  
  61.   
  62.         time += Time.deltaTime;  
  63.   
  64.          if(time >= 1.0 / fps){  
  65.   
  66.              nowFram++;  
  67.   
  68.              time = 0;  
  69.   
  70.              if(nowFram >= tex.Length)  
  71.              {  
  72.                 nowFram = 0;  
  73.              }  
  74.         }  
  75.           
  76.         GUI.DrawTexture(new Rect( (Screen.width-60)*0.5f,(Screen.height-60)*0.5f,60,60) ,tex[nowFram] );  
  77.                 //在这里显示读取的进度。  
  78.         GUI.Label(new Rect( (Screen.width-100)*0.5f,(Screen.height-60)*0.5f+100,100,60), "LOADING..." + progress+"%");  
  79.   
  80.     }  
  81.   
  82. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值