ResourcesAsync.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using XGame.Framework.Asyncs;
  2. using UnityEngine;
  3. namespace XGame.Framework.Asset
  4. {
  5. internal class ResourcesAsync : Async, IAssetAsync
  6. {
  7. public string Path { get; internal set; }
  8. public string AddressableName { get; internal set; }
  9. ResourceRequest request;
  10. public override float Progress
  11. {
  12. get => request?.progress ?? 0;
  13. protected set => base.Progress = value;
  14. }
  15. public object GetResult()
  16. {
  17. object result = null;
  18. if (request != null)
  19. result = request.asset;
  20. Clear();
  21. return result;
  22. }
  23. public void Start()
  24. {
  25. //int rootIndex = Path.IndexOf("Resources/", StringComparison.Ordinal);
  26. //string path = rootIndex != -1 ? Path.Substring(rootIndex + 10) : Path;
  27. //if (System.IO.Path.HasExtension(path))
  28. //{
  29. // path = path.Substring(0, path.LastIndexOf('.'));
  30. //}
  31. request = Resources.LoadAsync(Path);
  32. request.completed += OnCompleted;
  33. }
  34. void OnCompleted(AsyncOperation async)
  35. {
  36. if (!async.isDone || request.asset == null)
  37. AssetsLog.Error($"{Path} is not a valid asset. name:{AddressableName} instance:{request.asset?.GetInstanceID()}");
  38. Completed();
  39. }
  40. void Clear()
  41. {
  42. request = null;
  43. }
  44. }
  45. }