Dependencies.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace VEngine
  4. {
  5. public class Dependencies : Loadable
  6. {
  7. protected readonly List<Bundle> bundles = new List<Bundle>();
  8. protected Bundle mainBundle;
  9. public AssetBundle assetBundle => mainBundle?.assetBundle;
  10. protected override void OnLoad()
  11. {
  12. if (!Versions.GetDependencies(pathOrURL, out var info, out var infos))
  13. {
  14. Finish("Dependencies not found");
  15. return;
  16. }
  17. if (info == null)
  18. {
  19. Finish("info == null");
  20. return;
  21. }
  22. mainBundle = Bundle.LoadInternal(info, mustCompleteOnNextFrame);
  23. bundles.Add(mainBundle);
  24. if (infos != null && infos.Length > 0)
  25. foreach (var item in infos)
  26. bundles.Add(Bundle.LoadInternal(item, mustCompleteOnNextFrame));
  27. }
  28. public override void LoadImmediate()
  29. {
  30. if (isDone) return;
  31. foreach (var request in bundles) request.LoadImmediate();
  32. }
  33. protected override void OnUnload()
  34. {
  35. if (bundles.Count > 0)
  36. {
  37. foreach (var item in bundles)
  38. if (string.IsNullOrEmpty(item.error))
  39. item.Release();
  40. bundles.Clear();
  41. }
  42. mainBundle = null;
  43. }
  44. protected override void OnUpdate()
  45. {
  46. if (status == LoadableStatus.Loading)
  47. {
  48. var totalProgress = 0f;
  49. var allDone = true;
  50. foreach (var child in bundles)
  51. {
  52. totalProgress += child.progress;
  53. if (!string.IsNullOrEmpty(child.error))
  54. {
  55. status = LoadableStatus.FailedToLoad;
  56. error = child.error;
  57. progress = 1;
  58. return;
  59. }
  60. if (child.isDone) continue;
  61. allDone = false;
  62. break;
  63. }
  64. progress = totalProgress / bundles.Count * 0.5f;
  65. if (allDone)
  66. {
  67. if (assetBundle == null)
  68. {
  69. Finish("assetBundle == null");
  70. return;
  71. }
  72. Finish();
  73. }
  74. }
  75. }
  76. }
  77. }