DatabaseAllAssetsProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace YooAsset
  5. {
  6. internal sealed class DatabaseAllAssetsProvider : ProviderBase
  7. {
  8. public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
  9. {
  10. }
  11. internal override void InternalOnStart()
  12. {
  13. DebugBeginRecording();
  14. }
  15. internal override void InternalOnUpdate()
  16. {
  17. #if UNITY_EDITOR
  18. if (IsDone)
  19. return;
  20. if (_steps == ESteps.None)
  21. {
  22. // 检测资源文件是否存在
  23. string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
  24. if (string.IsNullOrEmpty(guid))
  25. {
  26. string error = $"Not found asset : {MainAssetInfo.AssetPath}";
  27. YooLogger.Error(error);
  28. InvokeCompletion(error, EOperationStatus.Failed);
  29. return;
  30. }
  31. _steps = ESteps.CheckBundle;
  32. // 注意:模拟异步加载效果提前返回
  33. if (IsWaitForAsyncComplete == false)
  34. return;
  35. }
  36. // 1. 检测资源包
  37. if (_steps == ESteps.CheckBundle)
  38. {
  39. if (IsWaitForAsyncComplete)
  40. {
  41. OwnerBundle.WaitForAsyncComplete();
  42. }
  43. if (OwnerBundle.IsDone() == false)
  44. return;
  45. if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
  46. {
  47. string error = OwnerBundle.LastError;
  48. InvokeCompletion(error, EOperationStatus.Failed);
  49. return;
  50. }
  51. _steps = ESteps.Loading;
  52. }
  53. // 2. 加载资源对象
  54. if (_steps == ESteps.Loading)
  55. {
  56. if (MainAssetInfo.AssetType == null)
  57. {
  58. List<UnityEngine.Object> result = new List<Object>();
  59. foreach (var assetPath in OwnerBundle.MainBundleInfo.IncludeAssetsInEditor)
  60. {
  61. UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetPath);
  62. if (mainAsset != null)
  63. result.Add(mainAsset);
  64. }
  65. AllAssetObjects = result.ToArray();
  66. }
  67. else
  68. {
  69. List<UnityEngine.Object> result = new List<Object>();
  70. foreach (var assetPath in OwnerBundle.MainBundleInfo.IncludeAssetsInEditor)
  71. {
  72. UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, MainAssetInfo.AssetType);
  73. if (mainAsset != null)
  74. result.Add(mainAsset);
  75. }
  76. AllAssetObjects = result.ToArray();
  77. }
  78. _steps = ESteps.Checking;
  79. }
  80. // 3. 检测加载结果
  81. if (_steps == ESteps.Checking)
  82. {
  83. if (AllAssetObjects == null)
  84. {
  85. string error;
  86. if (MainAssetInfo.AssetType == null)
  87. error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null";
  88. else
  89. error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
  90. YooLogger.Error(error);
  91. InvokeCompletion(error, EOperationStatus.Failed);
  92. }
  93. else
  94. {
  95. InvokeCompletion(string.Empty, EOperationStatus.Succeed);
  96. }
  97. }
  98. #endif
  99. }
  100. }
  101. }