using System; using FairyGUI; using UnityEngine; namespace ET { [FriendClass(typeof(GlobalComponent))] [FriendClass(typeof(FUI))] public static class FUIHelper { private static GObject CreateGObject(string packageName, string resName) { return UIPackage.CreateObject(packageName, resName); } private static void CreateGObjectAsync(string packageName, string resName, UIPackage.CreateObjectCallback result) { UIPackage.CreateObjectAsync(packageName, resName, result); } public static T CreateInstance(Entity parent, string packageName = null, string resName = null) where T: Entity, IAwake, new() { if (string.IsNullOrEmpty(packageName)) { packageName = (string) typeof(T).GetField("UIPackageName").GetValue(null); } if (string.IsNullOrEmpty(resName)) { resName = (string) typeof(T).GetField("UIResName").GetValue(null); } Game.EventSystem.Publish(new EventType.BeforeGObjectCreate(){Type = typeof(T)}); FUI fui = parent.AddChild(CreateGObject(packageName, resName)); return fui.AddComponent(); } public static ETTask CreateInstanceAsync(Entity parent, string packageName = null, string resName = null) where T: Entity, IAwake, new() { if (string.IsNullOrEmpty(packageName)) { packageName = (string) typeof(T).GetField("UIPackageName").GetValue(null); } if (string.IsNullOrEmpty(resName)) { resName = (string) typeof(T).GetField("UIResName").GetValue(null); } ETTask tcs = ETTask.Create(true); Game.EventSystem.Publish(new EventType.BeforeGObjectCreate(){Type = typeof(T)}); CreateGObjectAsync(packageName, resName, (go) => { FUI fui = parent.AddChild(go); tcs.SetResult(fui.AddComponent()); }); return tcs; } /// /// 仅用于go已经实例化情况下的创建(例如另一个组件引用了此组件) /// /// /// /// public static T Create(Entity parent, GObject go) where T: Entity, IAwake, new() { var fui = parent.AddChild(go); return fui.AddComponent(); } /// /// 通过此方法获取的FUI,在Dispose时不会释放GObject,需要自行管理(一般在配合FGUI的Pool机制时使用)。 /// public static T GetFromPool(Entity domain, GObject go) where T: Entity, IAwake, new() { var fui = go.Get(); var comp = fui.GetComponent(); if(comp == null) { comp = Create(domain, go); fui = comp.GetParent(); } fui.isFromFGUIPool = true; return comp; } public static void AddClickListenerAsync(this GButton button, Func action) { button.onClick.Clear(); async ETTask ClickActionAsync() { FUIComponent.Instance.IsButtonClicked = true; FUIComponent.Instance.StartButtonEventAsyncTimer(); await action(); FUIComponent.Instance.IsButtonClicked = false; FUIComponent.Instance.StopButtonEventAsyncTimer(); } button.onClick.Add(() => { if (FUIComponent.Instance.IsButtonClicked) { return; } ClickActionAsync().Coroutine(); }); } public static void AddClickListenerAsync(this GButton button, Func action) { button.onClick.Clear(); async ETTask ClickActionAsync(EventContext eventContext) { FUIComponent.Instance.IsButtonClicked = true; FUIComponent.Instance.StartButtonEventAsyncTimer(); await action(eventContext); FUIComponent.Instance.IsButtonClicked = false; FUIComponent.Instance.StopButtonEventAsyncTimer(); } button.onClick.Add((EventContext e) => { if (FUIComponent.Instance.IsButtonClicked) { return; } ClickActionAsync(e).Coroutine(); }); } public static Vector2 ConvertToUIPos(Vector2 worldPos) { var mainCamera = GlobalComponent.Instance.MainCamera.GetComponent(); var screenPos = mainCamera.WorldToScreenPoint(worldPos); screenPos.y = Screen.height - screenPos.y; screenPos = GRoot.inst.GlobalToLocal(screenPos); return screenPos; } } }