CTT/Unity/Assets/HotfixView/Helper/FUIHelper.cs

207 lines
6.4 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using ET;
using FairyGUI;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ET
{
public enum WindowPos
{
None,
Center,
Left,
Right,
}
public static class FUIHelper
{
public static GWindow asGWindow(this GObject gObject)
{
return gObject as GWindow;
}
public static GComponent Add3DUI(this GameObject self, string pkgName, string cName, Camera camera, int sortingOrder)
{
//UIPanel的生命周期将和yourGameObject保持一致。再次提醒注意yourGameObject的layer。
UIPanel panel = self.AddComponent<UIPanel>();
panel.packageName = pkgName;
panel.componentName = cName;
//下面这是设置选项非必须注意很多属性都要在container上设置而不是UIPanel
//设置renderMode的方式
panel.container.renderMode = RenderMode.WorldSpace;
//设置renderCamera的方式
panel.container.renderCamera = camera;
//设置fairyBatching的方式
panel.container.fairyBatching = true;
//设置sortingOrder的方式
panel.SetSortingOrder(sortingOrder, true);
//设置hitTestMode的方式
panel.SetHitTestMode(HitTestMode.Default);
//最后创建出UI
panel.CreateUI();
return panel.ui;
}
public static void Left(this GObject self)
{
GComponent r;
if (self.parent != null)
r = self.parent;
else
r = self.root;
self.SetXY(0, (int)((r.height - self.height) / 2), true);
}
public static void LRCenter(this GObject self)
{
GComponent r;
if (self.parent != null)
r = self.parent;
else
r = self.root;
self.SetXY((int)((r.width - self.width) / 2), self.height, true);
}
public static void Right(this GObject self)
{
GComponent r;
if (self.parent != null)
r = self.parent;
else
r = self.root;
self.SetXY(r.width - self.width, (int)((r.height - self.height) / 2), true);
}
public static async ETTask<FUI> Create(Scene scene, string uiType)
{
return await scene.GetComponent<FUIComponent>().Create(uiType);
}
public static async ETTask<FUI> Open(Scene scene, string uiType, WindowPos windowPos, Action<FUI> effcetAction = null)
{
2021-04-11 19:50:39 +08:00
FUI fui = await Create(scene, uiType);
FUIWindowComponent window = fui.GetOrAddComponent<FUIWindowComponent>();
2021-04-08 20:09:59 +08:00
if (window.IsShowing)
{
window.Hide();
return fui;
}
window.Show();
effcetAction?.Invoke(fui);
switch (windowPos)
{
default:
case WindowPos.None:
break;
case WindowPos.Center:
window.Window.Center();
break;
case WindowPos.Left:
window.Window.Left();
break;
case WindowPos.Right:
window.Window.Right();
break;
}
return fui;
}
public static void CloseAllWindows()
{
//关闭所有UI
2021-04-11 19:50:39 +08:00
foreach (FUI fui in FUIComponent.Instance.GetAll())
2021-04-08 20:09:59 +08:00
{
try
{
2021-04-11 19:50:39 +08:00
FUIWindowComponent window = fui.GetComponent<FUIWindowComponent>();
2021-04-08 20:09:59 +08:00
if (window != null)
{
window.Hide();
}
}
catch (Exception e)
{
Log.Error(e);
}
}
}
2021-05-05 13:36:19 +08:00
public static void HideWindow(this FUI self)
{
self.GetComponent<FUIWindowComponent>()?.Hide();
}
2021-04-08 20:09:59 +08:00
public static async ETTask<T> Open<T>(Scene scene, string uiType, WindowPos windowPos, Action<T> effcetAction = null) where T : FUI
{
2021-04-11 19:50:39 +08:00
FUI fui = await Create(scene, uiType);
2021-04-08 20:09:59 +08:00
T t = fui.As<T>();
2021-04-11 19:50:39 +08:00
FUIWindowComponent window = fui.GetOrAddComponent<FUIWindowComponent>();
2021-04-08 20:09:59 +08:00
if (window.IsShowing)
{
window.Hide();
return t;
}
window.Show();
effcetAction?.Invoke(t);
switch (windowPos)
{
case WindowPos.Center:
window.Window.Center();
break;
case WindowPos.Left:
window.Window.Left();
break;
case WindowPos.Right:
window.Window.Right();
break;
default:
break;
}
return t;
}
public static async ETTask<TUI> Open<T, TUI>(Scene scene, string uiType, WindowPos windowPos, Action<T> effcetAction = null)
where T : FUI
where TUI : Entity, new()
{
2021-04-11 19:50:39 +08:00
FUI fui = await Create(scene, uiType);
2021-04-08 20:09:59 +08:00
T t = fui.As<T>();
2021-04-11 19:50:39 +08:00
FUIWindowComponent window = fui.GetOrAddComponent<FUIWindowComponent>();
2021-04-08 20:09:59 +08:00
if (window.IsShowing)
{
window.Hide();
return t.GetComponent<TUI>();
}
window.Show();
effcetAction?.Invoke(t);
switch (windowPos)
{
case WindowPos.Center:
window.Window.Center();
break;
case WindowPos.Left:
window.Window.Left();
break;
case WindowPos.Right:
window.Window.Right();
break;
default:
break;
}
TUI tui = t.AddComponent<TUI>();
window.Window.OnHideEvent += () =>
{
t.RemoveComponent<TUI>();
};
return tui;
}
public static void Remove(Scene scene, string uiType)
{
scene.GetComponent<FUIComponent>().Remove(uiType);
}
}
}