207 lines
6.4 KiB
C#
207 lines
6.4 KiB
C#
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)
|
||
{
|
||
FUI fui = await Create(scene, uiType);
|
||
FUIWindowComponent window = fui.GetOrAddComponent<FUIWindowComponent>();
|
||
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
|
||
foreach (FUI fui in FUIComponent.Instance.GetAll())
|
||
{
|
||
try
|
||
{
|
||
FUIWindowComponent window = fui.GetComponent<FUIWindowComponent>();
|
||
if (window != null)
|
||
{
|
||
window.Hide();
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Log.Error(e);
|
||
}
|
||
}
|
||
}
|
||
|
||
public static void HideWindow(this FUI self)
|
||
{
|
||
self.GetComponent<FUIWindowComponent>()?.Hide();
|
||
}
|
||
public static async ETTask<T> Open<T>(Scene scene, string uiType, WindowPos windowPos, Action<T> effcetAction = null) where T : FUI
|
||
{
|
||
FUI fui = await Create(scene, uiType);
|
||
T t = fui.As<T>();
|
||
FUIWindowComponent window = fui.GetOrAddComponent<FUIWindowComponent>();
|
||
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()
|
||
{
|
||
FUI fui = await Create(scene, uiType);
|
||
T t = fui.As<T>();
|
||
FUIWindowComponent window = fui.GetOrAddComponent<FUIWindowComponent>();
|
||
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);
|
||
}
|
||
}
|
||
}
|