AssetDownloader.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. namespace TriLib
  6. {
  7. public class AssetDownloader : MonoBehaviour
  8. {
  9. public bool HasStarted
  10. {
  11. get
  12. {
  13. return this._unityWebRequest != null;
  14. }
  15. }
  16. public bool IsDone
  17. {
  18. get
  19. {
  20. return this._unityWebRequest != null && this._unityWebRequest.isDone;
  21. }
  22. }
  23. public string Error
  24. {
  25. get
  26. {
  27. if (this.HasStarted)
  28. {
  29. return this._unityWebRequest.error ?? this._error;
  30. }
  31. return this._error;
  32. }
  33. }
  34. public float Progress
  35. {
  36. get
  37. {
  38. if (!this.HasStarted)
  39. {
  40. return 0f;
  41. }
  42. return this._unityWebRequest.downloadProgress;
  43. }
  44. }
  45. protected void Start()
  46. {
  47. if (!string.IsNullOrEmpty(this.AssetURI) && !string.IsNullOrEmpty(this.AssetExtension))
  48. {
  49. this.DownloadAsset(this.AssetURI, this.AssetExtension, null, null, null, this.WrapperGameObject);
  50. }
  51. }
  52. protected void OnGUI()
  53. {
  54. if (!this.ShowProgress || !this.HasStarted || this.IsDone)
  55. {
  56. return;
  57. }
  58. if (this._centeredStyle == null)
  59. {
  60. this._centeredStyle = GUI.skin.GetStyle("Label");
  61. this._centeredStyle.alignment = TextAnchor.UpperCenter;
  62. }
  63. Rect position = new Rect((float)Screen.width / 2f - 100f, (float)Screen.height / 2f - 25f, 200f, 50f);
  64. GUI.Label(position, string.Format("Downloaded {0:P2}", this.Progress), this._centeredStyle);
  65. }
  66. public bool DownloadAsset(string assetURI, string assetExtension, ObjectLoadedHandle onAssetLoaded = null, TexturePreLoadHandle onTexturePreLoad = null, AssetLoaderOptions options = null, GameObject wrapperGameObject = null)
  67. {
  68. if (this.HasStarted && !this.IsDone)
  69. {
  70. return false;
  71. }
  72. this.AssetURI = assetURI;
  73. this.AssetExtension = assetExtension;
  74. this.WrapperGameObject = wrapperGameObject;
  75. base.StartCoroutine(this.DoDownloadAsset(assetURI, assetExtension, onAssetLoaded, onTexturePreLoad, options, wrapperGameObject));
  76. return true;
  77. }
  78. private IEnumerator DoDownloadAsset(string assetURI, string assetExtension, ObjectLoadedHandle onAssetLoaded, TexturePreLoadHandle onTexturePreLoad = null, AssetLoaderOptions options = null, GameObject wrapperGameObject = null)
  79. {
  80. this._unityWebRequest = UnityWebRequest.Get(assetURI);
  81. yield return this._unityWebRequest.Send();
  82. if (string.IsNullOrEmpty(this._unityWebRequest.error))
  83. {
  84. byte[] data = this._unityWebRequest.downloadHandler.data;
  85. using (AssetLoader assetLoader = new AssetLoader())
  86. {
  87. assetLoader.LoadFromMemoryWithTextures(data, assetExtension, onAssetLoaded, out this._error, onTexturePreLoad, options, wrapperGameObject);
  88. }
  89. }
  90. this._unityWebRequest.Dispose();
  91. this._unityWebRequest = null;
  92. yield break;
  93. }
  94. public string AssetURI;
  95. public string AssetExtension;
  96. public GameObject WrapperGameObject;
  97. public bool ShowProgress;
  98. private UnityWebRequest _unityWebRequest;
  99. private GUIStyle _centeredStyle;
  100. private string _error;
  101. }
  102. }