LoadHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using TMPro;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. public class LoadHelper
  9. {
  10. #region 图片加载
  11. /// <summary>
  12. /// 从Streamming下加载图片
  13. /// </summary>
  14. /// <param name="filePath">Streamming下路径</param>
  15. /// <returns></returns>
  16. public static Sprite LoadSpriteFromStreamming(string filePath)
  17. {
  18. Sprite sprite = null;
  19. string tmpPath = Path.Combine(Application.streamingAssetsPath, filePath);
  20. sprite = LoadSprite(tmpPath);
  21. return sprite;
  22. }
  23. /// <summary>
  24. /// 加载精灵
  25. /// </summary>
  26. /// <param name="filePath"></param>
  27. /// <returns></returns>
  28. public static Sprite LoadSprite(string filePath)
  29. {
  30. Sprite sprite = null;
  31. Texture2D tmpTexture = LoadTexture(filePath);
  32. if (tmpTexture != null)
  33. {
  34. sprite = Sprite.Create(tmpTexture, new Rect(0f, 0f, tmpTexture.width, tmpTexture.height), Vector2.one * 0.5f);
  35. }
  36. return sprite;
  37. }
  38. /// <summary>
  39. /// 加载Texture
  40. /// </summary>
  41. /// <param name="filePath"></param>
  42. /// <returns></returns>
  43. public static Texture2D LoadTexture(string filePath)
  44. {
  45. Texture2D tmpTexture = null;
  46. filePath = GetSuffixExtension(filePath);
  47. if (!File.Exists(filePath))
  48. {
  49. Debug.Log("<color=red>" + filePath + "不存在" + "</color>");
  50. return null;
  51. }
  52. using (FileStream stream = new FileStream(filePath, FileMode.Open))
  53. {
  54. byte[] tmpArr = new byte[stream.Length];
  55. stream.Read(tmpArr, 0, tmpArr.Length);
  56. stream.Close();
  57. tmpTexture = new Texture2D(1, 1);
  58. tmpTexture.LoadImage(tmpArr);
  59. }
  60. return tmpTexture;
  61. }
  62. /// <summary>
  63. /// 检索图片类型
  64. /// </summary>
  65. /// <param name="filePath"></param>
  66. /// <returns></returns>
  67. private static string GetSuffixExtension(string filePath)
  68. {
  69. string tmpPath = filePath;
  70. if (File.Exists(filePath + ".png")) { tmpPath = filePath + ".png"; }
  71. if (File.Exists(filePath + ".PNG")) { tmpPath = filePath + ".PNG"; }
  72. if (File.Exists(filePath + ".JPG")) { tmpPath = filePath + ".JPG"; }
  73. if (File.Exists(filePath + ".jpg")) { tmpPath = filePath + ".jpg"; }
  74. if (File.Exists(filePath + ".jpeg")) { tmpPath = filePath + ".jpeg"; }
  75. return tmpPath;
  76. }
  77. #endregion
  78. #region 音频
  79. /// <summary>
  80. /// 加载音频
  81. /// </summary>
  82. /// <param name="filePath"></param>
  83. /// <param name="audioType"></param>
  84. /// <param name="callBack"></param>
  85. /// <returns></returns>
  86. IEnumerator LoadAudioClip(string filePath, AudioType audioType, Action<AudioClip> callBack = null)
  87. {
  88. filePath = GetAudioClipExtension(filePath);
  89. using (UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(filePath, GetAudioClipType(filePath)))
  90. {
  91. yield return unityWebRequest.SendWebRequest();
  92. if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
  93. {
  94. Debug.LogError(unityWebRequest.error);
  95. }
  96. else
  97. {
  98. AudioClip tmpAudioClip = DownloadHandlerAudioClip.GetContent(unityWebRequest);
  99. if (tmpAudioClip != null)
  100. {
  101. callBack?.Invoke(tmpAudioClip);
  102. }
  103. else
  104. {
  105. Debug.LogError("音频加载失败");
  106. }
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// 加载视频
  112. /// </summary>
  113. /// <param name="filePath"></param>
  114. /// <param name="audioType"></param>
  115. /// <param name="callBack"></param>
  116. /// <returns></returns>
  117. IEnumerator LoadVideoClip(string filePath, AudioType audioType, Action<AudioClip> callBack = null)
  118. {
  119. filePath = GetAudioClipExtension(filePath);
  120. using (UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(filePath, GetAudioClipType(filePath)))
  121. {
  122. yield return unityWebRequest.SendWebRequest();
  123. if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
  124. {
  125. Debug.LogError(unityWebRequest.error);
  126. }
  127. else
  128. {
  129. AudioClip tmpAudioClip = DownloadHandlerAudioClip.GetContent(unityWebRequest);
  130. if (tmpAudioClip != null)
  131. {
  132. callBack?.Invoke(tmpAudioClip);
  133. }
  134. else
  135. {
  136. Debug.LogError("音频加载失败");
  137. }
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// 获取音频格式
  143. /// </summary>
  144. /// <param name="filePath"></param>
  145. /// <returns></returns>
  146. private static AudioType GetAudioClipType(string filePath)
  147. {
  148. AudioType audioType = AudioType.UNKNOWN;
  149. if (File.Exists(filePath + ".mp3") || File.Exists(filePath + ".MP3")) { audioType = AudioType.MPEG; }
  150. if (File.Exists(filePath + ".wav") || File.Exists(filePath + ".WAV")) { audioType = AudioType.WAV; }
  151. if (File.Exists(filePath + ".vag") || File.Exists(filePath + ".VAG")) { audioType = AudioType.VAG; }
  152. return audioType;
  153. }
  154. /// <summary>
  155. /// 获取音频全路径
  156. /// </summary>
  157. /// <param name="filePath"></param>
  158. /// <returns></returns>
  159. private static string GetAudioClipExtension(string filePath)
  160. {
  161. string audioPath = string.Empty;
  162. if (File.Exists(filePath + ".mp3")) { audioPath = filePath + ".MP3";}
  163. if (File.Exists(filePath + ".MP3")) { audioPath = filePath + ".MP3"; }
  164. if (File.Exists(filePath + ".wav")) { audioPath = filePath + ".wav"; }
  165. if (File.Exists(filePath + ".WAV")) { audioPath = filePath + ".WAV"; }
  166. if (File.Exists(filePath + ".vag")) { audioPath = filePath + ".vag"; }
  167. if (File.Exists(filePath + ".VAG")) { audioPath = filePath + ".VAG"; }
  168. return audioPath;
  169. }
  170. #endregion
  171. }