SceneAssetRequest.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEngine.SceneManagement;
  4. public class SceneAssetRequest : AssetRequest
  5. {
  6. protected readonly string sceneName;
  7. public string assetBundleName;
  8. protected BundleRequest BundleRequest;
  9. protected List<BundleRequest> children = new List<BundleRequest>();
  10. public SceneAssetRequest(string path, bool addictive)
  11. {
  12. name = path;
  13. LoadModule.GetAssetBundleName(path, out assetBundleName);
  14. sceneName = Path.GetFileNameWithoutExtension(name);
  15. loadSceneMode = addictive ? LoadSceneMode.Additive : LoadSceneMode.Single;
  16. }
  17. public LoadSceneMode loadSceneMode { get; protected set; }
  18. public override float progress
  19. {
  20. get { return 1; }
  21. }
  22. internal override void Load()
  23. {
  24. if (!string.IsNullOrEmpty(assetBundleName))
  25. {
  26. BundleRequest = LoadModule.LoadBundle(assetBundleName);
  27. if (BundleRequest != null)
  28. {
  29. var bundles = LoadModule.GetAllDependencies(assetBundleName);
  30. foreach (var item in bundles) children.Add(LoadModule.LoadBundle(item));
  31. SceneManager.LoadScene(sceneName, loadSceneMode);
  32. }
  33. }
  34. else
  35. {
  36. SceneManager.LoadScene(sceneName, loadSceneMode);
  37. }
  38. LoadState = AssetLoadState.Loaded;
  39. }
  40. internal override void Unload()
  41. {
  42. if (BundleRequest != null)
  43. BundleRequest.Release();
  44. if (children.Count > 0)
  45. {
  46. foreach (var item in children) item.Release();
  47. children.Clear();
  48. }
  49. if (loadSceneMode == LoadSceneMode.Additive)
  50. if (SceneManager.GetSceneByName(sceneName).isLoaded)
  51. SceneManager.UnloadSceneAsync(sceneName);
  52. BundleRequest = null;
  53. LoadState = AssetLoadState.Unload;
  54. }
  55. }