纳金网

标题: 场景切换之进度条显示进度百分比 [打印本页]

作者: 狂风大尉    时间: 2015-2-28 23:22
标题: 场景切换之进度条显示进度百分比

场景切换之进度条显示进度百分比
  1. using UnityEngine;

  2. using System.Collections;

  3. //using Globe;

  4. public class Loading : MonoBehaviour {

  5. private float fps = 2.0f;

  6. private float time;

  7. //一组动画的贴图,在编辑器中赋值。

  8. public Texture2D[] animations;

  9. private int nowFram;

  10. //异步对象

  11. AsyncOperation async;

  12. //读取场景的进度,它的取值范围在0 - 1 之间。

  13. int progress = 0;

  14. void Start()

  15. {

  16. //在这里开启一个异步任务,

  17. //进入loadScene方法。

  18. Globe.loadName= “CompleteScene”;

  19. StartCoroutine(loadScene());

  20. }

  21. //注意这里返回值一定是 IEnumerator

  22. IEnumerator loadScene()

  23. {

  24. //异步读取场景。

  25. //Globe.loadName 就是A场景中需要读取的C场景名称。

  26. async = Application.LoadLevelAsync(Globe.loadName);

  27. //读取完毕后返回, 系统会自动进入C场景

  28. yield return async;

  29. }

  30. void OnGUI()

  31. {

  32. //因为在异步读取场景,

  33. //所以这里我们可以刷新UI

  34. DrawAnimation(animations);

  35. }

  36. void Update()

  37. {

  38. //在这里计算读取的进度,

  39. //progress 的取值范围在0.1 - 1之间, 但是它不会等于1

  40. //也就是说progress可能是0.9的时候就直接进入新场景了

  41. //所以在写进度条的时候需要注意一下。

  42. //为了计算百分比 所以直接乘以100即可

  43. progress =  (int)(async.progress *100);

  44. //有了读取进度的数值,大家可以自行制作进度条啦。

  45. // Debug.Log(“xuanyusong” +progress);

  46. }

  47. //这是一个简单绘制2D动画的方法,没什么好说的。

  48. void   DrawAnimation(Texture2D[] tex)

  49. {

  50. time += Time.deltaTime;

  51. if(time >= 1.0 / fps){

  52. nowFram++;

  53. time = 0;

  54. if(nowFram >= tex.Length)

  55. {

  56. nowFram = 0;

  57. }

  58. }

  59. GUI.DrawTexture(new Rect( Screen.width/2-100,200,120,30) ,tex[nowFram] );

  60. //在这里显示读取的进度。

  61. GUI.Label(new Rect( Screen.width/2-100,235,300,60), “加载进度为” + progress+“%”);

  62. }

  63. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5