using System; using FairyGUI; namespace ET { public class FUIComponentAwakeSystem : AwakeSystem { public override void Awake(FUIComponent self) { self.Awake(); } } public class FUIComponentDestroySystem : DestroySystem { public override void Destroy(FUIComponent self) { self.Destroy(); } } [Timer(TimerType.ButtonAsyncEventTimer)] public class ButtonAsyncEventTimer : ATimer { public override void Run(FUIComponent self) { try { self.IsButtonClicked = false; } catch (Exception e) { Log.Error(e); } } } [FriendClass(typeof(FUIComponent))] public static class FUIComponentSystem { public static void Awake(this FUIComponent self) { FUIComponent.Instance = self; self.Root = self.Domain.AddChild(GRoot.inst); // 设置加载器 UIObjectFactory.SetLoaderExtension(typeof(FUIGLoader)); } public static void Destroy(this FUIComponent self) { if (self.IsDisposed) { return; } self.Root?.Dispose(); self.Root = null; } public static void Add(this FUIComponent self, FUI ui, bool asChildGObject) { self.Root?.Add(ui, asChildGObject); } public static void Remove(this FUIComponent self, string name) { self.Root?.Remove(name); } public static FUI Get(this FUIComponent self, string name) { return self.Root?.Get(name); } public static FUI[] GetAll(this FUIComponent self) { return self.Root?.GetAll(); } public static void Clear(this FUIComponent self) { var childrens = self.GetAll(); if (childrens != null) { foreach (var fui in childrens) { self.Remove(fui.Name); } } } public static void StartButtonEventAsyncTimer(this FUIComponent self) { self.ButtonAsyncEventTimer = TimerComponent.Instance.NewOnceTimer(TimeHelper.ClientNow() + 2000, TimerType.ButtonAsyncEventTimer, self); } public static void StopButtonEventAsyncTimer(this FUIComponent self) { TimerComponent.Instance.Remove(ref self.ButtonAsyncEventTimer); } public static void AddWindow(this FUIComponent self, string name, IFUIWindow window) { self.AvailableWindows.Add(name, window); } public static IFUIWindow GetWindow(this FUIComponent self, string name) { if (self.AvailableWindows.ContainsKey(name)) { return self.AvailableWindows[name]; } return null; } public static void HideWindow(this FUIComponent self, string name) { if (self.AvailableWindows.TryGetValue(name, out IFUIWindow win)) { win.Window?.Hide(); } } /// /// 异步显示一个UI组件 /// /// 组件所在的ab包 /// FUI组件类型 /// 对应的自定义逻辑组件类型 /// 对应的自定义逻辑组件 public static async ETTask ShowUIAsync(this FUIComponent self, string packageBundle) where T : Entity, IAwake, new() where K : Entity, IAwake, new() { // 对应fairgui中的包内部的资源名 string uiResName = (string) typeof (T).GetField("UIResName").GetValue(null); if (self.AvailableUI.TryGetValue(uiResName, out Entity comp)) { FUI fui = comp.GetParent(); fui.Visible = true; return comp as K; } else { var zoneScene = self.ZoneScene(); await zoneScene.GetComponent().AddPackageAsync(packageBundle); // 对应fairgui中的包名 string uiPackageName = (string) typeof(T).GetField("UIPackageName").GetValue(null); T dataComp = FUIHelper.CreateInstance(zoneScene, uiPackageName, uiResName); FUI fui = dataComp.GetParent(); fui.Name = uiResName; fui.MakeFullScreen(); var logicComp = fui.AddComponent(); self.Add(fui, true); self.AvailableUI.Add(uiResName, logicComp); return logicComp; } } public static K ShowUI(this FUIComponent self, string packageBundle) where T : Entity, IAwake, new() where K : Entity, IAwake, new() { string uiResName = (string) typeof (T).GetField("UIResName").GetValue(null); if (self.AvailableUI.TryGetValue(uiResName, out Entity comp)) { FUI fui = comp.GetParent(); fui.Visible = true; return comp as K; } else { var zoneScene = self.ZoneScene(); zoneScene.GetComponent().AddPackage(packageBundle); // 对应fairgui中的包名 string uiPackageName = (string) typeof(T).GetField("UIPackageName").GetValue(null); // 对应fairgui中的包内部的资源名 T dataComp = FUIHelper.CreateInstance(zoneScene, uiPackageName, uiResName); FUI fui = dataComp.GetParent(); fui.Name = uiResName; fui.MakeFullScreen(); var logicComp = fui.AddComponent(); self.Add(fui, true); self.AvailableUI.Add(uiResName, logicComp); return logicComp; } } public static Entity GetUI(this FUIComponent self, string name) { if (self.AvailableUI.TryGetValue(name, out Entity comp)) { return comp; } return null; } public static void HideUI(this FUIComponent self, string name) { if (self.AvailableUI.TryGetValue(name, out Entity comp)) { FUI fui = comp.GetParent(); fui.Visible = false; } } public static void RemoveUI(this FUIComponent self, string name) { self.AvailableUI.Remove(name); self.Remove(name); } public static K ShowWindow(this FUIComponent self, string packageBundle = null) where T : Entity, IAwake, new() where K : Entity, IAwake, IFUIWindow, new() { var zoneScene = self.ZoneScene(); var type = typeof (T); // 对应fairgui中的包内部的资源名 string uiResName = (string) type.GetField("UIResName").GetValue(null); var win = self.GetWindow(uiResName); if (win != null) { win.Window?.Show(); return win as K; } else { // 对应fairgui中的包名 string uiPackageName = (string) type.GetField("UIPackageName").GetValue(null); if (packageBundle == null) { // 默认使用fui提供的包名 packageBundle = uiPackageName; } zoneScene.GetComponent().AddPackage(packageBundle); T dataComp = FUIHelper.CreateInstance(zoneScene, uiPackageName, uiResName); FUI fui = dataComp.GetParent(); fui.Name = uiResName; fui.MakeFullScreen(); var logicComp = fui.AddComponent(); self.AddWindow(uiResName, logicComp); logicComp.Window?.Show(); return logicComp; } } public static async ETTask ShowWindowAsync(this FUIComponent self, string packageBundle = null) where T : Entity, IAwake, new() where K : Entity, IAwake, IFUIWindow, new() { var zoneScene = self.ZoneScene(); var type = typeof (T); // 对应fairgui中的包内部的资源名 string uiResName = (string) type.GetField("UIResName").GetValue(null); var win = self.GetWindow(uiResName); if (win != null) { win.Window?.Show(); return win as K; } else { // 对应fairgui中的包名 string uiPackageName = (string) type.GetField("UIPackageName").GetValue(null); if (packageBundle == null) { // 默认使用fui提供的包名 packageBundle = uiPackageName; } await zoneScene.GetComponent().AddPackageAsync(packageBundle); T dataComp = FUIHelper.CreateInstance(zoneScene, uiPackageName, uiResName); FUI fui = dataComp.GetParent(); fui.Name = uiResName; fui.MakeFullScreen(); var logicComp = fui.AddComponent(); self.AddWindow(uiResName, logicComp); logicComp.Window?.Show(); return logicComp; } } } }