DatabaseSubAssetsProvider.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace YooAsset
  5. {
  6. internal sealed class DatabaseSubAssetsProvider : ProviderBase
  7. {
  8. public DatabaseSubAssetsProvider(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. AllAssetObjects = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath);
  59. }
  60. else
  61. {
  62. UnityEngine.Object[] findAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath);
  63. List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
  64. foreach (var findAsset in findAssets)
  65. {
  66. if (MainAssetInfo.AssetType.IsAssignableFrom(findAsset.GetType()))
  67. result.Add(findAsset);
  68. }
  69. AllAssetObjects = result.ToArray();
  70. }
  71. _steps = ESteps.Checking;
  72. }
  73. // 3. 检测加载结果
  74. if (_steps == ESteps.Checking)
  75. {
  76. if (AllAssetObjects == null)
  77. {
  78. string error;
  79. if (MainAssetInfo.AssetType == null)
  80. error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null";
  81. else
  82. error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
  83. YooLogger.Error(error);
  84. InvokeCompletion(error, EOperationStatus.Failed);
  85. }
  86. else
  87. {
  88. InvokeCompletion(string.Empty, EOperationStatus.Succeed);
  89. }
  90. }
  91. #endif
  92. }
  93. }
  94. }