Loadable.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace VEngine
  5. {
  6. public enum LoadableStatus
  7. {
  8. Wait,
  9. Loading,
  10. DependentLoading,
  11. SuccessToLoad,
  12. FailedToLoad,
  13. Unloaded,
  14. CheckVersion,
  15. Downloading
  16. }
  17. public class Loadable
  18. {
  19. protected internal static readonly List<Loadable> Loading = new List<Loadable>();
  20. protected readonly Reference reference = new Reference();
  21. public LoadableStatus status { get; protected set; } = LoadableStatus.Wait;
  22. public string pathOrURL { get; set; }
  23. protected bool mustCompleteOnNextFrame { get; set; }
  24. public string error { get; internal set; }
  25. public bool isDone => status == LoadableStatus.SuccessToLoad || status == LoadableStatus.Unloaded ||
  26. status == LoadableStatus.FailedToLoad;
  27. public float progress { get; protected set; }
  28. protected void Finish(string errorCode = null)
  29. {
  30. error = errorCode;
  31. status = string.IsNullOrEmpty(errorCode) ? LoadableStatus.SuccessToLoad : LoadableStatus.FailedToLoad;
  32. progress = 1;
  33. }
  34. public static void UpdateAll()
  35. {
  36. for (var index = 0; index < Loading.Count; index++)
  37. {
  38. var item = Loading[index];
  39. if (Updater.Instance.busy) return;
  40. item.Update();
  41. if (!item.isDone) continue;
  42. Loading.RemoveAt(index);
  43. index--;
  44. item.Complete();
  45. }
  46. Asset.UpdateAssets();
  47. Scene.UpdateScenes();
  48. Bundle.UpdateBundles();
  49. ManifestAsset.UpdateManifestAssets();
  50. }
  51. internal static void Add(Loadable loadable)
  52. {
  53. Loading.Add(loadable);
  54. }
  55. internal void Update()
  56. {
  57. OnUpdate();
  58. }
  59. internal void Complete()
  60. {
  61. if (status == LoadableStatus.FailedToLoad)
  62. {
  63. Logger.E("Unable to load {0} {1} with error: {2}", GetType().Name, pathOrURL, error);
  64. Release();
  65. }
  66. OnComplete();
  67. }
  68. protected virtual void OnUpdate()
  69. {
  70. }
  71. protected virtual void OnLoad()
  72. {
  73. }
  74. protected virtual void OnUnload()
  75. {
  76. }
  77. protected virtual void OnComplete()
  78. {
  79. }
  80. public virtual void LoadImmediate()
  81. {
  82. throw new InvalidOperationException();
  83. }
  84. protected internal void Load()
  85. {
  86. reference.Retain();
  87. Add(this);
  88. if (status != LoadableStatus.Wait) return;
  89. Logger.I("Load {0} {1}.{2}", GetType().Name, Path.GetFileName(pathOrURL), UnityEngine.Random.Range(0, int.MaxValue));
  90. status = LoadableStatus.Loading;
  91. progress = 0;
  92. OnLoad();
  93. }
  94. protected internal void Unload()
  95. {
  96. if (status == LoadableStatus.Unloaded) return;
  97. Logger.I("Unload {0} {1}.{2} {3}", GetType().Name, Path.GetFileName(pathOrURL), error, UnityEngine.Random.Range(0, int.MaxValue));
  98. OnUnload();
  99. status = LoadableStatus.Unloaded;
  100. }
  101. public void Release()
  102. {
  103. if (reference.count <= 0)
  104. {
  105. Logger.W("Release {0} {1}.", GetType().Name, Path.GetFileName(pathOrURL));
  106. return;
  107. }
  108. reference.Release();
  109. if (!reference.unused) return;
  110. OnUnused();
  111. }
  112. protected virtual void OnUnused()
  113. {
  114. }
  115. }
  116. }