AssetDatabaseAsync.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #if UNITY_EDITOR
  2. using XGame.Framework.Asyncs;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace XGame.Framework.Asset
  6. {
  7. /// <summary>
  8. /// 使用EditorApplication.update来实现AssetData的异步加载
  9. /// </summary>
  10. internal class AssetDatabaseAsync : Async, IAssetAsync
  11. {
  12. public string Path { get; internal set; }
  13. public string AddressableName { get; internal set; }
  14. internal bool IsSprite { get; set; }
  15. const int WAIT_FOR_FRAME = 1;
  16. public object GetResult()
  17. {
  18. if (!System.IO.File.Exists(Path))
  19. {
  20. AssetsLog.Warn($"AssetDatabaseAsync GetResult Error. File is not exists. Name:{AddressableName} Path:{Path}");
  21. return null;
  22. }
  23. return IsSprite ? AssetDatabase.LoadAssetAtPath(Path, typeof(Sprite)) : AssetDatabase.LoadMainAssetAtPath(Path);
  24. }
  25. public void Start()
  26. {
  27. SimulateAsyncMono.Instance.StartCoroutine(OnLoop());
  28. }
  29. System.Collections.IEnumerator OnLoop()
  30. {
  31. yield return WAIT_FOR_FRAME;
  32. Progress = 1f;
  33. Completed();
  34. }
  35. }
  36. }
  37. #endif