[置顶] unity中关于异步loading场景的加载
2016-06-07 16:20
609 查看
当我们在加载一个场景时,如果场景的资源比较小的话是很快就跳入下一个场景的,如果场景资源多的话加载就会很慢,这时如果让用户一直等待会造成用户的体验很差,所以我们采用异步加载。
这里我使用的是UGUI做的,当然用NGUI也可以,只是换一下组件而已。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ExcessiveMenuPanel : MonoBehaviour {
public Scrollbar loadScrollbar;//场景中的进度条
public Text loadText;//显示进度的文本
// Use this for initialization
void Start () {
//这里传在buildSetting中的id,或者传字符串,把下面的int改为string
StartCoroutine(StartLoading_4(2));
}
/// <summary>
/// 加载场景,只能加载在BuildSettings设置的场景,我在做加载AssetBundle加载场景是不行的
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
private IEnumerator StartLoading_4(int scene)
{
int displayProgress = 0;
int toProgress = 0;
AsyncOperation op = Application.LoadLevelAsync(scene);
op.allowSceneActivation = false;
while (op.progress < 0.9f)
{
toProgress = (int)op.progress * 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
}
toProgress = 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
op.allowSceneActivation = true;
}
/// <summary>
/// 设置进度条和文本
/// </summary>
/// <param name="num"></param>
private void SetLoadingPercentage(float num)
{
loadScrollbar.size = num * 0.01f;
loadText.text = num.ToString() + "%";
}
}
这里我使用的是UGUI做的,当然用NGUI也可以,只是换一下组件而已。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ExcessiveMenuPanel : MonoBehaviour {
public Scrollbar loadScrollbar;//场景中的进度条
public Text loadText;//显示进度的文本
// Use this for initialization
void Start () {
//这里传在buildSetting中的id,或者传字符串,把下面的int改为string
StartCoroutine(StartLoading_4(2));
}
/// <summary>
/// 加载场景,只能加载在BuildSettings设置的场景,我在做加载AssetBundle加载场景是不行的
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
private IEnumerator StartLoading_4(int scene)
{
int displayProgress = 0;
int toProgress = 0;
AsyncOperation op = Application.LoadLevelAsync(scene);
op.allowSceneActivation = false;
while (op.progress < 0.9f)
{
toProgress = (int)op.progress * 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
}
toProgress = 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
op.allowSceneActivation = true;
}
/// <summary>
/// 设置进度条和文本
/// </summary>
/// <param name="num"></param>
private void SetLoadingPercentage(float num)
{
loadScrollbar.size = num * 0.01f;
loadText.text = num.ToString() + "%";
}
}
相关文章推荐
- [置顶] Unity 中关于获取到物体的方式
- Unity 5 事件/消息机制之UnityEvent类型
- 【Unity】C#特性Attribute与自动化
- Unity APP接入百度移动广告
- Unity中网格合并示例研究。针对合并子网格列表后再合并网格
- Unity查找Editor下Project视图中特定的资源
- unity3d泰斗破坏神2----课程列表
- Unity学习日常问题记录七--尝试编写部分自己简单的碰撞规则一
- 如何在 Unity 2D 和 3D 中放大或缩小以及点击屏幕
- Unity Sprite Packer 问题集合
- [Unity3D]脚本中Start()和Awake()的区别
- 【我的书】《Unity Shader入门精要》出版上市
- Unity Shader简单Shader一
- unity5.4 UGUI 手机屏幕自适应分辨率
- 【Unity】修复因GameObject层级变化而导致的animationclip内容丢失
- Unity 像子弹一样的方块+计数功能
- UnityShader快速上手指南(四)
- UnityShader快速上手指南(四)
- 在Unity中使用代码类来管理多个Panel(面板)之间的切换
- 更换vuforia中ImageTarget功能的识别目标图像