DatabaseSubAssetsProvider.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(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. AllAssetObjects = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath);
  57. }
  58. else
  59. {
  60. UnityEngine.Object[] findAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath);
  61. List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
  62. foreach (var findAsset in findAssets)
  63. {
  64. if (MainAssetInfo.AssetType.IsAssignableFrom(findAsset.GetType()))
  65. result.Add(findAsset);
  66. }
  67. AllAssetObjects = result.ToArray();
  68. }
  69. Status = EStatus.Checking;
  70. }
  71. // 3. 检测加载结果
  72. if (Status == EStatus.Checking)
  73. {
  74. Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed;
  75. if (Status == EStatus.Failed)
  76. {
  77. if (MainAssetInfo.AssetType == null)
  78. LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null";
  79. else
  80. LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
  81. YooLogger.Error(LastError);
  82. }
  83. InvokeCompletion();
  84. }
  85. #endif
  86. }
  87. }
  88. }