FguiNestedLoadAsync.cs 1.9 KB

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