提交FairyGUI

This commit is contained in:
PC-20230316NUNE\Administrator
2024-10-15 20:37:54 +08:00
parent ef1d3dfb2f
commit 9cd469b811
409 changed files with 66228 additions and 4076 deletions

View File

@@ -43,6 +43,12 @@ namespace SHFrame
public static ProcedureModule Procedure => _procedure ??= Get<ProcedureModule>();
private static ProcedureModule _procedure;
/// <summary>
/// 获取UI模块。
/// </summary>
public static UIModule UI => _ui ??= Get<UIModule>();
private static UIModule _ui;
#endregion
/// <summary>

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 91ae8771f1ed4d36bdf225c58fafae69
timeCreated: 1728993220

View File

@@ -0,0 +1,139 @@
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using FairyGUI;
using UnityEngine;
using YooAsset;
namespace SHFrame
{
public class FairyGUILoader
{
/// <summary>
/// 资源句柄列表
/// </summary>
private Dictionary<string, AssetHandle> _handles = new Dictionary<string, AssetHandle>();
// /// <summary>
// /// 加载方法
// /// </summary>
// /// <param name="name"></param>
// /// <param name="extension"></param>
// /// <param name="type"></param>
// /// <param name="method"></param>
// /// <returns></returns>
// private object LoadFuncSync(string name, string extension, System.Type type, out DestroyMethod method)
// {
// method = DestroyMethod.None; //注意这里一定要设置为None
// var package = YooAssets.GetPackage("DefaultPackage");
// var handle = package.LoadAssetSync(name, type);
// _handles.Add(name, handle);
// return handle.AssetObject;
// }
// public bool AddPackageSync(string packageName)
// {
// string location = $"{packageName}_fui";
//
// bool isValid = YooAssets.CheckLocationValid(location);
// //Debug.Log($"检查资源定位地址是否有效:{location}{isValid}");
//
// if (isValid == false) return false;
//
// if (_handles.ContainsKey(packageName))
// {
// return true;
// }
//
// // 执行FairyGUI的添加包函数
// UIPackage.AddPackage(packageName, LoadFuncSync);
// return true;
// }
private void LoadFuncAsync(string location, string extension, System.Type type, PackageItem item)
{
bool isValid = YooAssets.CheckLocationValid(location);
if (isValid == false)
{
return;
}
if (_handles.ContainsKey(location))
{
Debug.LogWarning($"LoadAysnc--_handles已经添加过:{location}");
return;
}
var handle = YooAssets.LoadAssetAsync(location, type);
_handles.Add(location, handle);
handle.Completed += onCompleted;
void onCompleted(AssetHandle handle)
{
handle.Completed -= onCompleted;
//destroyMethod 指示返回的资源应该怎样释放。例如复制的资源实例可以用Destroy共享的资源实例需要用Unload不需要底层处理则传None。
item.owner.SetItemAsset(item, handle.AssetObject, DestroyMethod.None);
}
}
public async UniTask<bool> AddPackageAsync(string packageName)
{
string location = $"{packageName}_fui";
bool isValid = YooAssets.CheckLocationValid(location);
if (isValid == false)
{
return false;
}
if (_handles.ContainsKey(location))
{
return true;
}
AssetHandle handle = YooAssets.LoadAssetAsync<TextAsset>(location);
_handles.Add(location, handle);
await handle.ToUniTask();
//异步加载方式适用于Addressable。注意这种方式必须是异步不能在回调函数里直接设置结果。
//descData是包的描述数据需要自行加载出来传入
//assetNamePrefix参数底层不使用但在回调中name参数会带上assetPath作为前缀。
UIPackage.AddPackage((handle.AssetObject as TextAsset).bytes, packageName, LoadFuncAsync);
return true;
}
public GComponent CreateGComponent(string packageName, string componentName)
{
return (GComponent)UIPackage.CreateObject(packageName, componentName);
}
public UniTask<GComponent> CreateObjectAsync(string packageName, string componentName)
{
var utcs = new UniTaskCompletionSource<GComponent>();
UIPackage.CreateObjectAsync(packageName, componentName, MyCreateObjectCallback);
void MyCreateObjectCallback(GObject obj)
{
utcs.TrySetResult((GComponent)obj);
}
return utcs.Task; //本质上就是返回了一个UniTask<int>
}
/// <summary>
/// 释放资源句柄列表
/// </summary>
private void ReleaseHandles()
{
foreach (var handle in _handles)
{
handle.Value.Release();
}
_handles.Clear();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8f70f254db9c4496b328039cdbaf7848
timeCreated: 1728993226

View File

@@ -0,0 +1,56 @@
using FairyGUI;
namespace SHFrame
{
public abstract class UIBase
{
public string PanelName { get; set; }
/// <summary>
/// UI的object
/// </summary>
public GComponent View { get; set; }
/// <summary>
/// 实例化时调用
/// </summary>
public abstract void OnInit();
/// <summary>
/// 打开面板时调用
/// </summary>
public abstract void OnOpen(params object[] param);
/// <summary>
/// 添加监听事件在OnOpen后调用
/// </summary>
public abstract void AddListener();
/// <summary>
/// 关闭面板时调用
/// </summary>
public abstract void OnClose();
/// <summary>
/// 移除监听事件在OnClose后调用
/// </summary>
public abstract void RemoveListener();
/// <summary>
/// 在RemoveListener后调用,调用父类方法销魂会销毁显示对象VIew
/// </summary>
public virtual void OnDispose()
{
if (View != null)
{
if (View.parent != null)
{
View.parent.RemoveChild(View);
}
View.Dispose();
View = null;
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b35298b8478d4ae4810b7c27ebb75dee
timeCreated: 1728993226

View File

@@ -0,0 +1,192 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using FairyGUI;
namespace SHFrame
{
public enum UILayerEnum
{
Root, //添加到根节点
View,
Window,
Pop,
Guide
}
public class UIModule : Module
{
public readonly Dictionary<string, UIBase> UIMap = new();
private FairyGUILoader m_FguiLoader;
public string CurKey { private set; get; } = "";
private GComponent ViewRoot { get; set; }
private GComponent WindowRoot { get; set; }
private GComponent PopRoot { get; set; }
private GComponent GuideRoot { get; set; }
/// <summary>
/// 游戏框架模块初始化。
/// </summary>
protected override void Awake()
{
base.Awake();
m_FguiLoader = new FairyGUILoader();
}
// public float ScaleT { get; private set; }
public void InitScaleT()
{
// UIContentScaler scaler = Stage.inst.gameObject.GetComponent<UIContentScaler>();
// float scaleX = GRoot.inst.width / scaler.designResolutionX;
// float scaleY = GRoot.inst.height / scaler.designResolutionY;
// ScaleT = Mathf.Min(scaleX, scaleY);
}
public void InitUIModule(GComponent view, GComponent window, GComponent pop, GComponent guide)
{
ViewRoot = view;
WindowRoot = window;
PopRoot = pop;
GuideRoot = guide;
}
public void MakeFullScreen(GObject gObject)
{
// gObject.SetSize(GRoot.inst.width / ScaleT, GRoot.inst.height / ScaleT);
gObject.MakeFullScreen();
}
// public bool AddPackageSync(string packageName)
// {
// return m_FguiLoader.AddPackageSync(packageName);
// }
public UniTask<bool> AddPackageASync(string packageName)
{
return m_FguiLoader.AddPackageAsync(packageName);
}
/// <summary>
/// 通过UIManager打开UI
/// </summary>
/// <typeparam name="TClassType">ui的类名</typeparam>
/// <param name="pkgName">ui的pkgName</param>
/// <param name="resName">ui的resName</param>
/// <param name="layer"></param>
/// <param name="param"></param>
public async UniTask Open<TClassType>(string pkgName, string resName, UILayerEnum layer, params object[] param)
where TClassType : UIBase, new()
{
var key = pkgName + "/" + resName;
UIMap.TryGetValue(key, out var uiClass);
if (uiClass == null)
{
uiClass = new TClassType();
UIMap.Add(key, uiClass);
}
if (uiClass.View == null)
{
await SHFrameModule.UI.AddPackageASync(pkgName);
uiClass.View = await m_FguiLoader.CreateObjectAsync(pkgName, resName);
// uiClass.View.SetScale(ScaleT, ScaleT);
// uiClass.View.SetSize(GRoot.inst.width / ScaleT, GRoot.inst.height / ScaleT);
uiClass.View.MakeFullScreen();
UIMap[key].OnInit();
}
GComponent root;
switch (layer)
{
case UILayerEnum.Root:
root = GRoot.inst;
break;
case UILayerEnum.View:
root = ViewRoot;
break;
case UILayerEnum.Window:
root = WindowRoot;
break;
case UILayerEnum.Guide:
root = GuideRoot;
break;
default:
throw new ArgumentOutOfRangeException(nameof(layer), layer, null);
}
// root.RemoveChildren();
// root.visible = true;
// if (root.parent != null)
// {
// root.parent.visible = true;
// }
try
{
root.AddChild(uiClass.View);
uiClass.View.x = 0;
uiClass.View.y = 0;
uiClass.View.name = resName;
uiClass.OnOpen(param);
// uiClass.RemoveListener();
uiClass.AddListener();
CurKey = key;
}
catch (Exception e)
{
Log.Error(e);
throw;
}
}
/// <summary>
/// 关闭通过UIManager打开的UI
/// </summary>
public void Close(string pkgName, string resName)
{
var key = pkgName + "/" + resName;
if (key == "" || !UIMap.ContainsKey(key)) return;
Close(key);
}
public void Close()
{
Close(CurKey);
}
public void Close(string key)
{
if (key == "" || !UIMap.ContainsKey(key)) return;
if (UIMap[key].View == null || UIMap[key].View.parent == null) return;
UIMap[key].View.parent.RemoveChild(UIMap[key].View);
UIMap[key].OnClose();
UIMap[key].RemoveListener();
UIMap[key].OnDispose();
UIMap.Remove(key);
}
public void CloseAll()
{
// var list = ReferencePool.Acquire<VarStringList>();
// list.Value.AddRange(UIMap.Keys);
// foreach (var key in list.Value)
var list = new List<string>();
list.AddRange(UIMap.Keys);
foreach (var key in list){
if (key == "" || !UIMap.ContainsKey(key)) return;
Close(key);
}
// ReferencePool.Release(list);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a847b1151d044df183a7ab4fe052112f
timeCreated: 1728993226