DatabaseAllAssetsProvider.cs 2.7 KB

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