UINestedLoadAsync.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using UnityEngine;
  3. using XGame.Framework.Asset;
  4. using XGame.Framework.Asyncs;
  5. namespace XGame.Framework.UI
  6. {
  7. internal class UINestedLoadAsync : Async, IAssetLoadAsync<INestedView>
  8. {
  9. public string AddressableName => _loadAsync?.AddressableName;
  10. private IAssetLoadAsync<UINested> _loadAsync;
  11. private UIKey _nestedKey;
  12. private Transform _parent;
  13. private UIContext _context;
  14. public INestedView Result { get; private set; }
  15. public UINestedLoadAsync(IAssetLoadAsync<UINested> loadAsync, UIKey nestedKey, Transform parent, UIContext context)
  16. {
  17. _loadAsync = loadAsync;
  18. _nestedKey = nestedKey;
  19. _parent = parent;
  20. _context = context;
  21. loadAsync.On(OnLoaded);
  22. }
  23. protected override void OnRemoveAll()
  24. {
  25. _loadAsync?.RemoveAll();
  26. Clear();
  27. }
  28. private void OnLoaded(IAsync _)
  29. {
  30. var nested = _loadAsync.Result;
  31. if (nested == null)
  32. {
  33. Log.Error($"UINested加载失败. Key:{_nestedKey}");
  34. return;
  35. }
  36. var view = Activator.CreateInstance(_nestedKey.UIViewType) as INestedView;
  37. (view as INestedInternal).Init(_context, nested, false);
  38. if (_parent != null)
  39. {
  40. nested.transform.SetParent(_parent, false);
  41. nested.transform.position = Vector3.zero;
  42. view.Enable();
  43. }
  44. Result = view;
  45. Completed();
  46. Clear();
  47. }
  48. private void Clear()
  49. {
  50. _loadAsync = null;
  51. _nestedKey = null;
  52. _parent = null;
  53. _context = null;
  54. Result = null;
  55. }
  56. }
  57. }