You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
5.2 KiB
148 lines
5.2 KiB
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<T>(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<FUI, GObject>(CreateGObject(packageName, resName)); |
|
return fui.AddComponent<T>(); |
|
} |
|
|
|
public static ETTask<T> CreateInstanceAsync<T>(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<T> tcs = ETTask<T>.Create(true); |
|
Game.EventSystem.Publish(new EventType.BeforeGObjectCreate(){Type = typeof(T)}); |
|
CreateGObjectAsync(packageName, resName, (go) => |
|
{ |
|
FUI fui = parent.AddChild<FUI, GObject>(go); |
|
tcs.SetResult(fui.AddComponent<T>()); |
|
}); |
|
|
|
return tcs; |
|
} |
|
/// <summary> |
|
/// 仅用于go已经实例化情况下的创建(例如另一个组件引用了此组件) |
|
/// </summary> |
|
/// <param name="domain"></param> |
|
/// <param name="go"></param> |
|
/// <returns></returns> |
|
public static T Create<T>(Entity parent, GObject go) where T: Entity, IAwake, new() |
|
{ |
|
var fui = parent.AddChild<FUI, GObject>(go); |
|
return fui.AddComponent<T>(); |
|
} |
|
|
|
/// <summary> |
|
/// 通过此方法获取的FUI,在Dispose时不会释放GObject,需要自行管理(一般在配合FGUI的Pool机制时使用)。 |
|
/// </summary> |
|
public static T GetFromPool<T>(Entity domain, GObject go) where T: Entity, IAwake, new() |
|
{ |
|
var fui = go.Get<FUI>(); |
|
|
|
var comp = fui.GetComponent<T>(); |
|
if(comp == null) |
|
{ |
|
comp = Create<T>(domain, go); |
|
fui = comp.GetParent<FUI>(); |
|
} |
|
|
|
fui.isFromFGUIPool = true; |
|
|
|
return comp; |
|
} |
|
public static void AddClickListenerAsync(this GButton button, Func<ETTask> 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<EventContext, ETTask> 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<Camera>(); |
|
var screenPos = mainCamera.WorldToScreenPoint(worldPos); |
|
screenPos.y = Screen.height - screenPos.y; |
|
screenPos = GRoot.inst.GlobalToLocal(screenPos); |
|
|
|
return screenPos; |
|
} |
|
} |
|
} |