UIHelper.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using FairyGUI;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace ET.Client
  5. {
  6. public static class UIHelper
  7. {
  8. public static async ETTask<GComponent> Create(string packName, string uiName = null, int index = -1, GComponent parent = null)
  9. {
  10. var ass = await YooAssetProxy.LoadAssetAsync<TextAsset>($"FGUI_{packName}_fui");
  11. UIPackage.AddPackage(ass.GetAssetObject<TextAsset>().bytes, packName, LoadPackageInternalAsync);
  12. var view = UIPackage.CreateObject(packName, uiName ?? packName).asCom;
  13. view.name = uiName ?? packName;
  14. if(parent == null)
  15. {
  16. parent = GRoot.inst;
  17. }
  18. if (index >= 0)
  19. {
  20. parent.AddChildAt(view, index);
  21. }
  22. else
  23. {
  24. parent.AddChild(view);
  25. }
  26. return view;
  27. }
  28. private static async void LoadPackageInternalAsync(string name, string extension, System.Type type, PackageItem item)
  29. {
  30. var tex = await YooAssetProxy.LoadAssetAsync<Texture>($"FGUI_{name}");
  31. item.owner.SetItemAsset(item, tex.GetAsset<Texture>(), DestroyMethod.Unload);
  32. }
  33. public static bool Remove(string uiName, bool release = true)
  34. {
  35. var view = GRoot.inst.GetChild(uiName);
  36. if (view != null)
  37. {
  38. GRoot.inst.RemoveChild(view, release);
  39. return true;
  40. }
  41. return false;
  42. }
  43. public static bool SetVisible(string uiName, bool flag)
  44. {
  45. var view = GRoot.inst.GetChild(uiName);
  46. if (view != null)
  47. {
  48. view.visible = flag;
  49. return true;
  50. }
  51. return false;
  52. }
  53. public static GObject GetUI(string uiName)
  54. {
  55. return GRoot.inst.GetChild(uiName);
  56. }
  57. public static void RemoveAllUiExceptSth()
  58. {
  59. string[] excepts = { "HeadBarRoot" };
  60. var chds = GRoot.inst.GetChildren();
  61. foreach(var ch in chds)
  62. {
  63. if(!excepts.Contains<string>(ch.name))
  64. {
  65. GRoot.inst.RemoveChild(ch, false);
  66. }
  67. }
  68. }
  69. public static async void ShowLoadingUI()
  70. {
  71. var loading = UIHelper.GetUI("Loading");
  72. if (loading == null)
  73. {
  74. await UIHelper.Create("Loading");
  75. }
  76. else
  77. {
  78. loading.visible = true;
  79. }
  80. }
  81. public static void HideLoadingUI()
  82. {
  83. SetVisible("Loading", false);
  84. }
  85. }
  86. }