BundleRequestAsync.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class BundleRequestAsync : BundleRequest
  5. {
  6. private AssetBundleCreateRequest _request;
  7. public override float progress
  8. {
  9. get
  10. {
  11. if (isDone)
  12. return 1;
  13. if (LoadState == AssetLoadState.Init)
  14. return 0;
  15. if (_request == null)
  16. return 1;
  17. return _request.progress;
  18. }
  19. }
  20. internal override bool Update()
  21. {
  22. if (!base.Update())
  23. return false;
  24. if(LoadState == AssetLoadState.LoadAsset)
  25. {
  26. if (_request.isDone)
  27. {
  28. assetBundle = _request.assetBundle;
  29. if (assetBundle == null)
  30. error = string.Format("unable to load assetBundle:{0}", name);
  31. LoadState = AssetLoadState.Loaded;
  32. return false;
  33. }
  34. }
  35. return true;
  36. }
  37. internal override void Load()
  38. {
  39. if(_request == null)
  40. {
  41. _request = AssetBundle.LoadFromFileAsync(name);
  42. if(_request == null)
  43. {
  44. error = name + "LoadFromFile failed.";
  45. return;
  46. }
  47. }
  48. LoadState = AssetLoadState.LoadAsset;
  49. }
  50. internal override void Unload()
  51. {
  52. _request = null;
  53. LoadState = AssetLoadState.Unload;
  54. base.Unload();
  55. }
  56. internal override void LoadImmediate()
  57. {
  58. Load();
  59. assetBundle = _request.assetBundle;
  60. if (assetBundle != null)
  61. {
  62. Debug.LogWarning("LoadImmediate:" + assetBundle.name);
  63. LoadState = AssetLoadState.Loaded;
  64. }
  65. }
  66. }