UIHelper.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. public static async void ShowLoadingUI()
  57. {
  58. var loading = UIHelper.GetUI("Loading");
  59. if (loading == null)
  60. {
  61. await UIHelper.Create("Loading");
  62. }
  63. else
  64. {
  65. loading.visible = true;
  66. }
  67. }
  68. public static void HideLoadingUI()
  69. {
  70. SetVisible("Loading", false);
  71. }
  72. }
  73. }