[Unity3D] 载入游戏地图时背景图片随机切换 & 数字百分比进度条

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.UI;
 4 using UnityEngine.SceneManagement;
8 public class LoadingGame : MonoBehaviour { 9 10 /*载入时显示的文字*/ 11 public Text LoadingText; 12 13 /*用于做判断的百分比进度条当前值*/ 14 private int currentValue = 0; 15 16 /*用于做判断的百分比进度条最大值*/ 17 private int LoadingFullValue = 100; 18 19 /* 20 创建一个纹理图片集合,用于载入地图时随机获取图片使用, 21 创建后手动添加自己要随机的数量和图片,记得图片类型要改成Sprite 22 */ 23 public Texture[] LoadingTextureList; 24 25 /*用于要修改对象的公开变量, 26 因为我们要修改的是画布的图片纹理,所以直接将画布下的RawImage拖入即可 27 */ 28 public Transform CanvasTexture; 29 30 private void Start() { 31 /*因为只需要在载入地图时获取一次随机图片,所以写在Start方法内*/ 32 CanvasTexture.GetComponent<RawImage>().texture = GetRandomLoadingTexture(); 33 } 34 35 36 /// <summary> 37 /// 从图片纹理集合中随机获取一张图片 38 /// </summary> 39 /// <returns>一张图片</returns> 40 private Texture GetRandomLoadingTexture() { 41 if (LoadingTextureList.Length > 0) { 42 int r = Random.Range(0, LoadingTextureList.Length); 43 return LoadingTextureList[r]; 44 45 } else { 46 return null; 47 } 48 49 } 50 51 52 private void FixedUpdate() { 53 54 /*调用载入地图时显示字体进度条的方法*/ 55 LoadingShowText(); 56 } 57 58 59 /// <summary> 60 /// 载入时显示文字和百分比数字进度条及切换场景 61 /// </summary> 62 private void LoadingShowText() { 63 if (currentValue < LoadingFullValue) { 64 currentValue++; 65 } 66 LoadingText.text = string.Format("正在加载游戏,进度[ {0}% ] ", currentValue); 67 68 if (currentValue == 100) { 69 LoadingText.text = "载入成功"; 70 SceneManager.LoadScene("FFGame"); 71 } 72 } 73 }

附:效果GIF,注意看背景图片和右下角载入百分比数字进度条

时间若流水,恍惚间逝去
原文地址:https://www.cnblogs.com/alanshreck/p/13583970.html