StreamingAssetsLoader

作者:追风剑情 发布于:2024-6-19 15:29 分类:Unity3d

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. using UnityEngine.Serialization;
  8. /// <summary>
  9. /// 资源加载器
  10. /// 从 Application.streamingAssetsPath 下加载文件
  11. /// </summary>
  12. public class StreamingAssetsLoader : MonoBehaviour
  13. {
  14. //文件url列表,只填写相对路径
  15. [SerializeField]
  16. private List<string> m_urls = new List<string>();
  17. //是否执行Awake方法时自动开始加载
  18. [SerializeField]
  19. private bool m_LoadOnAwake = false;
  20.  
  21. public string url { get; private set; }
  22. public string error { get; private set; }
  23. public float progress { get; private set; }
  24. public AssetObject asset { get; private set; }
  25.  
  26. [Serializable]
  27. public class FileProgressEvent : UnityEvent<StreamingAssetsLoader> { }
  28. [Serializable]
  29. public class FileCompletedEvent : UnityEvent<StreamingAssetsLoader> { }
  30. [Serializable]
  31. public class FileErrorEvent : UnityEvent<StreamingAssetsLoader> { }
  32. [Serializable]
  33. public class ListCompletedEvent : UnityEvent<StreamingAssetsLoader> { }
  34.  
  35. //当前文件加载进度
  36. [FormerlySerializedAs("onFileProgress")]
  37. [SerializeField]
  38. private FileProgressEvent m_OnFileProgress = new FileProgressEvent();
  39. //当前文件加载完成
  40. [FormerlySerializedAs("onFileCompleted")]
  41. [SerializeField]
  42. private FileCompletedEvent m_OnFileCompleted = new FileCompletedEvent();
  43. //所有文件加载完成
  44. [FormerlySerializedAs("onListCompleted")]
  45. [SerializeField]
  46. private ListCompletedEvent m_OnListCompleted = new ListCompletedEvent();
  47. //当前文件加载出错
  48. [FormerlySerializedAs("onFileError")]
  49. [SerializeField]
  50. private FileErrorEvent m_OnFileError = new FileErrorEvent();
  51.  
  52. public FileProgressEvent OnFileProgress => m_OnFileProgress;
  53. public FileCompletedEvent OnFileCompleted => m_OnFileCompleted;
  54. public ListCompletedEvent OnListCompleted => m_OnListCompleted;
  55. public FileErrorEvent OnFileError => m_OnFileError;
  56.  
  57. //资源类型
  58. public enum AssetType
  59. {
  60. None = 0,//未知
  61. Text, //文本
  62. Texture,//图片
  63. Audio, //音频
  64. AssetBundle, //资源包
  65. }
  66.  
  67. //资源对象
  68. public class AssetObject
  69. {
  70. public AssetType type;
  71. public string url;
  72. public string text;
  73. public Texture2D texture;
  74. public AudioClip audioClip;
  75. public AssetBundle assetBundle;
  76.  
  77. public void Dispose()
  78. {
  79. texture = null;
  80. audioClip = null;
  81. assetBundle = null;
  82. }
  83. }
  84.  
  85. private void Awake()
  86. {
  87. if (m_LoadOnAwake)
  88. StartLoad();
  89. }
  90.  
  91. // 清空url
  92. public void Clear()
  93. {
  94. m_urls.Clear();
  95. if (asset != null)
  96. {
  97. asset.Dispose();
  98. asset = null;
  99. }
  100. }
  101.  
  102. // 设置urls
  103. public void SetUrls(List<string> urls)
  104. {
  105. m_urls = urls;
  106. }
  107.  
  108. // 添加url
  109. public void AddUrl(string url)
  110. {
  111. m_urls.Add(url);
  112. }
  113.  
  114. // 启动加载
  115. public void StartLoad()
  116. {
  117. StartCoroutine(LoadAsync());
  118. }
  119.  
  120. // 停止加载
  121. public void StopLoad()
  122. {
  123. StopCoroutine(LoadAsync());
  124. }
  125.  
  126. private IEnumerator LoadAsync()
  127. {
  128. yield return null;
  129. int index = 0;
  130. while (index < m_urls.Count)
  131. {
  132. url = m_urls[index++];
  133. url = string.Format("{0}/{1}", Application.streamingAssetsPath, url);
  134. Debug.LogFormat("Load: {0}", url);
  135. WWW www = new WWW(url);
  136. while (!www.isDone)
  137. {
  138. progress = www.progress;
  139. m_OnFileProgress?.Invoke(this);
  140. yield return www;
  141. }
  142. if (!string.IsNullOrEmpty(www.error))
  143. {
  144. error = www.error;
  145. m_OnFileError?.Invoke(this);
  146. yield break;
  147. }
  148. AssetObject obj = new AssetObject();
  149. obj.url = url;
  150. obj.type = GetAssetTypeByExtension(url);
  151. ReadFromWWW(obj, www);
  152. asset = obj;
  153. www.Dispose();
  154. m_OnFileCompleted?.Invoke(this);
  155. yield return new WaitForEndOfFrame();
  156. }
  157. m_OnListCompleted?.Invoke(this);
  158. }
  159.  
  160. private void OnDestroy()
  161. {
  162. StopAllCoroutines();
  163. }
  164.  
  165. // 从www中读取资源
  166. private void ReadFromWWW(AssetObject obj, WWW www)
  167. {
  168. switch (obj.type)
  169. {
  170. case AssetType.Text:
  171. obj.text = www.text;
  172. break;
  173. case AssetType.Texture:
  174. obj.texture = www.texture;
  175. break;
  176. case AssetType.Audio:
  177. obj.audioClip = www.GetAudioClip();
  178. break;
  179. case AssetType.AssetBundle:
  180. obj.assetBundle = www.assetBundle;
  181. break;
  182. }
  183. }
  184.  
  185. // 通过后缀判断资源类型
  186. private AssetType GetAssetTypeByExtension(string url)
  187. {
  188. string ext = Path.GetExtension(url).ToLower();
  189. if (ext.Equals(".tif") || ext.Equals(".jpg") || ext.Equals(".png"))
  190. return AssetType.Texture;
  191. if (ext.Equals(".txt") || ext.Equals(".json") || ext.Equals(".xml"))
  192. return AssetType.Text;
  193. if (ext.Equals(".wav") || ext.Equals(".mp3"))
  194. return AssetType.Audio;
  195. //资源包文件后缀,根据需求定义
  196. if (ext.Equals(".model"))
  197. return AssetType.AssetBundle;
  198. return AssetType.None;
  199. }
  200. }

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号