UIHelper.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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)
  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. GRoot.inst.AddChild(view);
  14. return view;
  15. }
  16. private static async void LoadPackageInternalAsync(string name, string extension, System.Type type, PackageItem item)
  17. {
  18. var tex = await YooAssetProxy.LoadAssetAsync<Texture>($"FGUI_{name}");
  19. item.owner.SetItemAsset(item, tex.GetAsset<Texture>(), DestroyMethod.Unload);
  20. }
  21. public static bool Remove(string uiName, bool release = true)
  22. {
  23. var view = GRoot.inst.GetChild(uiName);
  24. if (view != null)
  25. {
  26. GRoot.inst.RemoveChild(view, release);
  27. return true;
  28. }
  29. return false;
  30. }
  31. public static bool SetVisible(string uiName, bool flag)
  32. {
  33. var view = GRoot.inst.GetChild(uiName);
  34. if (view != null)
  35. {
  36. view.visible = flag;
  37. return true;
  38. }
  39. return false;
  40. }
  41. public static GObject GetUI(string uiName)
  42. {
  43. return GRoot.inst.GetChild(uiName);
  44. }
  45. }
  46. }