You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.4 KiB
C#

using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using FairyGUI;
using Script.FrameWork.EventSystem;
using UnityEngine;
using YooAsset;
namespace Script.FrameWork.UI
{
public class UIPage
{
private GComponent _panel;
private Window _window;
private bool _isShow;
public bool IsLoaded;
public bool IsLoading;
public UIParam Param;
private Dictionary<int, List<Dispatcher>> _eventDic = new();
// ui cache
private Dictionary<string, GObject> _cacheComponents = new();
private Dictionary<string, Controller> _cacheControllers = new();
private Dictionary<string, Transition> _cacheTransitions = new();
public UIPage()
{
IsLoading = false;
IsLoaded = false;
}
public void Create(Window window, GComponent panel)
{
_window = window;
_panel = panel;
IsLoading = false;
IsLoaded = true;
OnCreate();
}
public void Init(UIParam param)
{
Param = param;
OnInit(param);
}
public void InitEvents()
{
}
public void Show()
{
if (IsLoading || !IsLoaded)
return;
if (!_isShow)
{
_window.Show();
_isShow = true;
OnShow();
}
}
public void Hide()
{
if (IsLoading || !IsLoaded)
return;
if (_isShow)
{
_window.Hide();
_isShow = false;
OnHide();
}
}
public async UniTask Close()
{
if (IsLoaded)
{
// Fade Out
var outTransition = _panel.GetTransition("Out");
if (outTransition != null)
{
outTransition.Play();
await UniTask.WaitUntil(() => !outTransition.playing);
}
_window.Dispose();
_panel.Dispose();
_window = null;
_panel = null;
IsLoaded = false;
}
_isShow = false;
_cacheComponents.Clear();
_cacheControllers.Clear();
}
public void Update(float deltaTime)
{
OnUpdate(deltaTime);
}
public void Destroy()
{
OnDestroy();
}
public void AddEventListener(int eventKey, Dispatcher handler)
{
if (!_eventDic.TryGetValue(eventKey, out var dispatchers))
{
dispatchers = new List<Dispatcher>();
_eventDic.Add(eventKey, dispatchers);
}
}
protected void SetSortingOrder(int sortingOrder)
{
_window.sortingOrder = sortingOrder;
}
protected T GetChild<T>(string childName) where T : GObject
{
if (_cacheComponents.TryGetValue(childName, out var com))
return (T)com;
com = _panel.GetChild(childName);
_cacheComponents.Add(childName, com);
return (T)com;
}
protected T GetChildByPath<T>(string childPath) where T : GComponent
{
if (_cacheComponents.TryGetValue(childPath, out var com))
return (T)com;
com = _panel.GetChildByPath(childPath).asCom;
_cacheComponents.Add(childPath, com);
return (T)com;
}
protected Controller GetController(string controllerName)
{
if (_cacheControllers.TryGetValue(controllerName, out var controller))
return controller;
return _panel.GetController(controllerName);
}
protected Transition GetTransition(string transitionName)
{
if (_cacheTransitions.TryGetValue(transitionName, out var transition))
return transition;
return _panel.GetTransition(transitionName);
}
protected virtual void OnCreate()
{
}
protected virtual void OnInit(UIParam param = null)
{
}
protected virtual void OnInitEvents()
{
}
protected virtual void OnShow()
{
}
protected virtual void OnHide()
{
}
protected virtual void OnClose()
{
}
protected virtual void OnUpdate(float deltaTime)
{
}
protected virtual void OnDestroy()
{
}
protected T CreateUGUIGraph<T>(GGraph holder, string uguiName)
{
var canvasGo = new GameObject("Canvas");
var canvas = canvasGo.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
canvas.worldCamera = StageCamera.main;
var prefab = YooAssets.LoadAssetSync(uguiName).AssetObject as GameObject;
var go = GameObject.Instantiate(prefab, canvasGo.transform, true);
var size = holder.size;
canvas.GetComponent<RectTransform>().sizeDelta = size;
go.GetComponent<RectTransform>().sizeDelta = size;
var gw = new GoWrapper(canvasGo);
holder.displayObject.Dispose();
holder.SetNativeObject(gw);
return go.GetComponent<T>();
}
}
}