ResourcesLoader.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using XGame.Framework.Asset.Addressable;
  2. using System;
  3. using System.IO;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. namespace XGame.Framework.Asset
  7. {
  8. internal class ResourcesLoader : BaseLoader, IAssetLoader
  9. {
  10. private IAddressableManifest _addressableManifest;
  11. public ResourcesLoader(IAddressableManifest addressableManifest)
  12. {
  13. _addressableManifest = addressableManifest;
  14. }
  15. public IAssetAsync LoadAsync<TResult>(string addressableName)
  16. {
  17. return GetAssetAsync(addressableName);
  18. }
  19. public object LoadSync<TResult>(string addressableName)
  20. {
  21. //int rootIndex = addressableName.IndexOf("Resources/", StringComparison.Ordinal);
  22. //string path = rootIndex != -1 ? addressableName.Substring(rootIndex + 10) : addressableName;
  23. //if (Path.HasExtension(path))
  24. //{
  25. // path = path.Substring(0, path.LastIndexOf('.'));
  26. //}
  27. return Resources.Load(GetPathByName(addressableName));
  28. }
  29. public IAssetAsync LoadSceneBundleAsync(string sceneName)
  30. {
  31. throw new System.NotImplementedException();
  32. }
  33. public void UnLoadSceneBundle(string sceneName)
  34. {
  35. throw new System.NotImplementedException();
  36. }
  37. public void Unload(string assetPath, object source)
  38. {
  39. if (source is Object obj)
  40. {
  41. obj.Unload();
  42. //Resources.UnloadAsset(obj);
  43. }
  44. }
  45. public void UnloadUnusedAssets()
  46. {
  47. //throw new System.NotImplementedException();
  48. }
  49. protected override void OnDispose()
  50. {
  51. _addressableManifest = null;
  52. }
  53. private string GetPathByName(string addressableName)
  54. {
  55. var relativePath = _addressableManifest.GetRelativePath(addressableName);
  56. if (string.IsNullOrEmpty(relativePath))
  57. {
  58. AssetsLog.Error($"Resources Loader can't find relativePath. Name:{addressableName}");
  59. }
  60. return relativePath;
  61. }
  62. private IAssetAsync GetAssetAsync(string addressableName)
  63. {
  64. var assetAsync = AsyncPool.GetAssetAsync(addressableName);
  65. if (assetAsync == null)
  66. {
  67. var async = new ResourcesAsync()
  68. {
  69. AddressableName = addressableName,
  70. Path = GetPathByName(addressableName)
  71. };
  72. assetAsync = async;
  73. AsyncPool.AddAsync(assetAsync);
  74. }
  75. return assetAsync;
  76. }
  77. }
  78. }