DatabaseAssetProvider.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace YooAsset
  5. {
  6. internal sealed class DatabaseAssetProvider : ProviderBase
  7. {
  8. public DatabaseAssetProvider(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. AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(MainAssetInfo.AssetPath);
  58. else
  59. AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
  60. _steps = ESteps.Checking;
  61. }
  62. // 3. 检测加载结果
  63. if (_steps == ESteps.Checking)
  64. {
  65. if (AssetObject == null)
  66. {
  67. string error;
  68. if (MainAssetInfo.AssetType == null)
  69. error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null";
  70. else
  71. error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
  72. YooLogger.Error(error);
  73. InvokeCompletion(error, EOperationStatus.Failed);
  74. }
  75. else
  76. {
  77. InvokeCompletion(string.Empty, EOperationStatus.Succeed);
  78. }
  79. }
  80. #endif
  81. }
  82. }
  83. }