当前位置:   article > 正文

Unity 异步加载场景 动画Loading

unity异步加载动画

参考:Application.LoadLevelAsync 异步加载关卡
static function LoadLevelAsync (levelName : string) : AsyncOperation
http://www.ceeger.com/Script/Application/Application.LoadLevelAsync.html

建2个C#脚本:LoadingComponent.cs 和 LoadLevelOnClick.cs


LoadingComponent.cs:

using UnityEngine;

[ExecuteInEditMode]
public class LoadingComponent : MonoBehaviour
{
private AsyncOperation asy;
public Texture2D backgroundImage;
public Font fontType;
public string levelName;
public AudioClip loadingAudio;
public float loadingBarHeight = 25f;
public Texture2D loadingBarImage;
public Vector2 loadingBarPosition;
public string loadingText = "Loading Now...";
public Color loadingTextColor;
public FontStyle loadingTextFont;
public int loadingTextSize = 5;
public bool normalized;
public bool showProgressPercentage = true;
public float startDelay = 1.0f;
private GUIStyle style;
private GUIStyle textStyle;
public Texture2D emptyBarImage;
private GUIStyle emptyStyle;

  1. private void Start()
  2. {
  3. if (Application.isPlaying)
  4. {
  5. if (loadingAudio)
  6. {
  7. if (!GetComponent<AudioSource>())
  8. {
  9. gameObject.AddComponent("AudioSource");
  10. }
  11. GetComponent<AudioSource>().loop = true;
  12. GetComponent<AudioSource>().playOnAwake = false;
  13. GetComponent<AudioSource>().clip = loadingAudio;
  14. }
  15. gameObject.AddComponent("GUITexture");
  16. style = new GUIStyle();
  17. style.normal.background = loadingBarImage;
  18. style.alignment = TextAnchor.MiddleCenter;
  19. style.normal.textColor = loadingTextColor;
  20. style.fontStyle = loadingTextFont;
  21. style.fontSize = loadingTextSize;
  22. textStyle = new GUIStyle();
  23. textStyle.alignment = TextAnchor.MiddleCenter;
  24. textStyle.fontStyle = loadingTextFont;
  25. textStyle.normal.textColor = loadingTextColor;
  26. textStyle.fontSize = loadingTextSize;
  27. textStyle.font = fontType;
  28. emptyStyle = new GUIStyle();
  29. emptyStyle.normal.background = emptyBarImage;
  30. }
  31. }
  32. private void Update()
  33. {
  34. if (normalized)
  35. {
  36. loadingBarPosition.x = Mathf.Clamp(loadingBarPosition.x, 0f, 1f);
  37. loadingBarPosition.y = Mathf.Clamp(loadingBarPosition.y, 0f, 1f);
  38. }
  39. }
  40. private void LoadNextLevel()
  41. {
  42. transform.position = new Vector3(0, 0, 100);
  43. transform.localScale = Vector3.zero;
  44. guiTexture.pixelInset = new Rect(0, 0, Screen.width, Screen.height);
  45. guiTexture.texture = backgroundImage;
  46. if (loadingAudio)
  47. {
  48. audio.Play();
  49. }
  50. DontDestroyOnLoad(this);
  51. asy = Application.LoadLevelAsync(levelName);
  52. }
  53. private void OnGUI()
  54. {
  55. if (asy != null)
  56. {
  57. if (!asy.isDone)
  58. {
  59. if (!normalized)
  60. {
  61. GUI.Label(new Rect(Screen.width/2, loadingBarPosition.y - 20, 50, 50), loadingText, textStyle);
  62. if (showProgressPercentage)
  63. {
  64. GUI.Box(
  65. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  66. Screen.width - (loadingBarPosition.x * 2),
  67. loadingBarHeight),
  68. "", emptyStyle);
  69. GUI.Label(
  70. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  71. ((Screen.width - (loadingBarPosition.x*2))*(asy.progress*100f))/100f,
  72. loadingBarHeight),
  73. "", style);
  74. GUI.Label(
  75. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  76. Screen.width - (loadingBarPosition.x * 2),
  77. loadingBarHeight),
  78. Mathf.RoundToInt(asy.progress * 100f) + "%", emptyStyle);
  79. }
  80. else
  81. {
  82. GUI.Box(
  83. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  84. Screen.width - (loadingBarPosition.x * 2),
  85. loadingBarHeight),
  86. "", emptyStyle);
  87. GUI.Label(
  88. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  89. ((Screen.width - (loadingBarPosition.x*2))*(asy.progress*100f))/100f,
  90. loadingBarHeight),
  91. "", style);
  92. }
  93. }
  94. else
  95. {
  96. GUI.Label(new Rect(Screen.width/2, (loadingBarPosition.y*Screen.height) - 50, 50, 50),
  97. loadingText, textStyle);
  98. if (showProgressPercentage)
  99. {
  100. GUI.Box(
  101. new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
  102. Screen.width - (loadingBarPosition.x * Screen.width * 2),
  103. loadingBarHeight),
  104. "", emptyStyle);
  105. GUI.Label(
  106. new Rect(loadingBarPosition.x*Screen.width, loadingBarPosition.y*Screen.height,
  107. ((Screen.width - (loadingBarPosition.x*Screen.width*2))*(asy.progress*100f))/
  108. 100f,
  109. loadingBarHeight),
  110. "", style);
  111. GUI.Label(
  112. new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
  113. Screen.width - (loadingBarPosition.x * Screen.width * 2),
  114. loadingBarHeight),
  115. Mathf.RoundToInt(asy.progress * 100f) + "%", textStyle);
  116. }
  117. else
  118. {
  119. GUI.Box(
  120. new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
  121. Screen.width - (loadingBarPosition.x * Screen.width * 2),
  122. loadingBarHeight),
  123. "", emptyStyle);
  124. GUI.Label(
  125. new Rect(loadingBarPosition.x*Screen.width, loadingBarPosition.y*Screen.height,
  126. ((Screen.width - (loadingBarPosition.x*Screen.width*2))*(asy.progress*100f))/
  127. 100f,
  128. loadingBarHeight),
  129. "", style);
  130. }
  131. }
  132. }
  133. else
  134. {
  135. if (!normalized)
  136. {
  137. GUI.Label(new Rect(Screen.width/2, loadingBarPosition.y - 50, 50, 50), loadingText, textStyle);
  138. if (showProgressPercentage)
  139. {
  140. GUI.Box(
  141. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  142. Screen.width - (loadingBarPosition.x * 2),
  143. loadingBarHeight),
  144. "", emptyStyle);
  145. GUI.Label(
  146. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  147. ((Screen.width - (loadingBarPosition.x*2))*(asy.progress*100f))/100f,
  148. loadingBarHeight),
  149. "", style);
  150. GUI.Label(
  151. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  152. Screen.width - (loadingBarPosition.x * 2),
  153. loadingBarHeight),
  154. Mathf.RoundToInt(asy.progress * 100f) + "%", textStyle);
  155. }
  156. else
  157. {
  158. GUI.Box(
  159. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  160. Screen.width - (loadingBarPosition.x * 2),
  161. loadingBarHeight),
  162. "", emptyStyle);
  163. GUI.Label(
  164. new Rect(loadingBarPosition.x, loadingBarPosition.y,
  165. ((Screen.width - (loadingBarPosition.x*2))*(asy.progress*100f))/100f,
  166. loadingBarHeight),
  167. "", style);
  168. }
  169. }
  170. else
  171. {
  172. GUI.Label(new Rect(Screen.width/2, (loadingBarPosition.y*Screen.height) - 50, 50, 50),
  173. loadingText, textStyle);
  174. if (showProgressPercentage)
  175. {
  176. GUI.Box(
  177. new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
  178. Screen.width - (loadingBarPosition.x * Screen.width * 2),
  179. loadingBarHeight),
  180. "", emptyStyle);
  181. GUI.Label(
  182. new Rect(loadingBarPosition.x*Screen.width, loadingBarPosition.y*Screen.height,
  183. ((Screen.width - loadingBarPosition.x*Screen.width*2)*(asy.progress*100f))/100f,
  184. loadingBarHeight),
  185. "", style);
  186. GUI.Label(
  187. new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
  188. Screen.width - (loadingBarPosition.x * Screen.width * 2),
  189. loadingBarHeight),
  190. Mathf.RoundToInt(asy.progress * 100f) + "%", textStyle);
  191. }
  192. else
  193. {
  194. GUI.Box(
  195. new Rect(loadingBarPosition.x * Screen.width, loadingBarPosition.y * Screen.height,
  196. Screen.width - (loadingBarPosition.x * Screen.width * 2),
  197. loadingBarHeight),
  198. "", emptyStyle);
  199. GUI.Label(
  200. new Rect(loadingBarPosition.x*Screen.width, loadingBarPosition.y*Screen.height,
  201. ((Screen.width - loadingBarPosition.x*Screen.width*2)*(asy.progress*100f))/100f,
  202. loadingBarHeight),
  203. "", style);
  204. }
  205. }
  206. Destroy(gameObject, startDelay);
  207. }
  208. }
  209. }

}


LoadLevelOnClick.cs

using UnityEngine;

public class LoadLevelOnClick : MonoBehaviour
{
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
gameObject.SendMessage("LoadNextLevel");
}
}
}

源码下载: http://pan.baidu.com/s/1kUik3lH 密码: f5eh

转载于:https://www.cnblogs.com/wgscd/articles/5094886.html

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号