UIHelper.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using FairyGUI;
  2. using UnityEngine;
  3. namespace ET.Client
  4. {
  5. public static class UIHelper
  6. {
  7. public static async ETTask<GComponent> Create(string packName, string uiName = null, int index = -1, GComponent parent = null)
  8. {
  9. var ass = await YooAssetProxy.LoadAssetAsync<TextAsset>($"FGUI_{packName}_fui");
  10. UIPackage.AddPackage(ass.GetAssetObject<TextAsset>().bytes, packName, LoadPackageInternalAsync);
  11. var view = UIPackage.CreateObject(packName, uiName ?? packName).asCom;
  12. view.name = uiName ?? packName;
  13. if(parent == null)
  14. {
  15. parent = GRoot.inst;
  16. }
  17. if (index >= 0)
  18. {
  19. parent.AddChildAt(view, index);
  20. }
  21. else
  22. {
  23. parent.AddChild(view);
  24. }
  25. return view;
  26. }
  27. private static async void LoadPackageInternalAsync(string name, string extension, System.Type type, PackageItem item)
  28. {
  29. var tex = await YooAssetProxy.LoadAssetAsync<Texture>($"FGUI_{name}");
  30. item.owner.SetItemAsset(item, tex.GetAsset<Texture>(), DestroyMethod.Unload);
  31. }
  32. public static bool Remove(string uiName, bool release = true)
  33. {
  34. var view = GRoot.inst.GetChild(uiName);
  35. if (view != null)
  36. {
  37. GRoot.inst.RemoveChild(view, release);
  38. return true;
  39. }
  40. return false;
  41. }
  42. public static bool SetVisible(string uiName, bool flag)
  43. {
  44. var view = GRoot.inst.GetChild(uiName);
  45. if (view != null)
  46. {
  47. view.visible = flag;
  48. return true;
  49. }
  50. return false;
  51. }
  52. public static GObject GetUI(string uiName)
  53. {
  54. return GRoot.inst.GetChild(uiName);
  55. }
  56. }
  57. }