unity3d异步加载场景
2016-06-24 09:27
555 查看
根据宣雨松前辈的教程来做的,因为我用到的场景不是在游戏里的那种(本人做增强现实的,完全把unity拿来做应用了=。=),所以这里的方法不是很全面,原文戳这里:点击打开链接
异步加载流程:
lovdlevel 异步读取
A---------------> B ------------------------>C
播放加载动画
示例背景介绍:A场景是一个菜单,鼠标选中其中一项,则跳转到相应场景(C场景),B场景为加载动画播放场景
A场景:摄像机添加menu.cs
B场景:绑定loading.cs,把loading图片附到loading.cs
menu.cs:
[csharp] view
plain copy
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
[csharp] view
plain copy
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+"%");
}
}
异步加载流程:
lovdlevel 异步读取
A---------------> B ------------------------>C
播放加载动画
示例背景介绍:A场景是一个菜单,鼠标选中其中一项,则跳转到相应场景(C场景),B场景为加载动画播放场景
A场景:摄像机添加menu.cs
B场景:绑定loading.cs,把loading图片附到loading.cs
menu.cs:
[csharp] view
plain copy
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
[csharp] view
plain copy
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+"%");
}
}
相关文章推荐
- unity导入模型后贴图丢失
- Unity与注册登录服务器交互原理及code
- 【unityshader】径向模糊
- unity3d 碰撞器和触发器的区别
- unity通过android adb查看真机日志
- 如何将Unity中的脚本文件转为dll文件
- Unity3d坐标与NGUI坐标转换
- unity引用dll文件方法
- unity3d屏蔽Windows10输入法
- 【Unity3D实战】零基础一步一步教你制作酷跑类游戏(1)
- unity 获取某个文件夹下的所有图片
- Unity3d制作一个简单的打开保存对话框-按钮2
- unity shader(二)
- Unity Shape sdk 安卓问题
- 深入浅出聊Unity3D项目优化:从Draw Calls到GC
- mysql-community-server installaton
- Unity 脚本生命周期流程图
- 【Unity Shaders】学习笔记——SurfaceShader(五)让纹理动起来
- Unity3d Ar Vuforia +zxing 实现二维码扫描
- Unity HSV色彩模式下 校色Shader