DatabaseAssetProvider.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(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. AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(MainAssetInfo.AssetPath);
  56. else
  57. AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
  58. Status = EStatus.Checking;
  59. }
  60. // 3. 检测加载结果
  61. if (Status == EStatus.Checking)
  62. {
  63. Status = AssetObject == null ? EStatus.Failed : EStatus.Succeed;
  64. if (Status == EStatus.Failed)
  65. {
  66. if (MainAssetInfo.AssetType == null)
  67. LastError = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null";
  68. else
  69. LastError = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
  70. YooLogger.Error(LastError);
  71. }
  72. InvokeCompletion();
  73. }
  74. #endif
  75. }
  76. }
  77. }