初始化

This commit is contained in:
come
2025-07-26 16:56:42 +08:00
parent 8291dbb91c
commit fa81439a8c
2574 changed files with 328492 additions and 2170 deletions

View File

@@ -0,0 +1,34 @@
#if UNITY_EDITOR
using System.Text;
namespace YIUIFramework
{
/// <summary>
/// 用于EDITOR时使用Get取得数据列表
/// 编译时使用Get and GenCode来生成同类型代码
/// 然后使用新生成代码里的Get来得到数据列表
/// </summary>
/// <typeparam name="T"></typeparam>
public interface ICodeGenerator<T>
{
T[] Get();
/// <summary>
/// 生成数据代码仅能在editor里执行
/// 在方法里,只要写单项的属性设置代码就行了。
/// 而要设置的代码名默认叫"v";
/// </summary>
/// <param name="info"></param>
/// <param name="sb">用于写代码的SB</param>
/// <returns></returns>
void WriteCode(T info, StringBuilder sb);
/// <summary>
/// 可能遇到他是个单例不是new的情况 所以这里可以重写
/// </summary>
/// <param name="info"></param>
/// <param name="sb"></param>
void NewCode(T info, StringBuilder sb);
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d6a0dff0c78b4939a63cd8e0ea5fd36a
timeCreated: 1682072802

View File

@@ -0,0 +1,216 @@
//#define YIUIMACRO_SIMULATE_NONEEDITOR //模拟非编辑器状态 在编辑器使用 非编辑器加载模式 用于在编辑器下测试
using System;
using System.Collections.Generic;
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// UI关联帮助类
/// </summary>
public static class UIBindHelper
{
/// <summary>
/// 根据创建时的类获取
/// </summary>
private static Dictionary<Type, UIBindVo> g_UITypeToPkgInfo = new Dictionary<Type, UIBindVo>();
/// <summary>
/// 根据 pkg + res 双字典获取
/// </summary>
private static Dictionary<string, Dictionary<string, UIBindVo>> g_UIPathToPkgInfo =
new Dictionary<string, Dictionary<string, UIBindVo>>();
/// <summary>
/// 只有panel 的信息
/// </summary>
private static Dictionary<string, UIBindVo> g_UIPanelNameToPkgInfo = new Dictionary<string, UIBindVo>();
//改为dll过后 提供给外部的方法
//1 从UI工具中自动生成绑定代码
//2 外部请直接调用此方法 UIBindHelper.InternalGameGetUIBindVoFunc = YIUICodeGenerated.UIBindProvider.Get;
public static Func<UIBindVo[]> InternalGameGetUIBindVoFunc { internal get; set; }
//初始化记录
public static bool IsInit { get; private set; }
public static Type BasePanelType = typeof(BasePanel);
/// <summary>
/// 初始化获取到所有UI相关的绑定关系
/// Editor下是反射
/// 其他 是序列化的文件 打包的时候一定要生成一次文件
/// </summary>
internal static bool InitAllBind()
{
if (IsInit)
{
Debug.LogError($"已经初始化过了 请检查");
return false;
}
#if !UNITY_EDITOR || YIUIMACRO_SIMULATE_NONEEDITOR
if (InternalGameGetUIBindVoFunc == null)
{
Debug.LogError($"使用非反射注册绑定 但是方法未实现 请检查");
return false;
}
var binds = InternalGameGetUIBindVoFunc?.Invoke();
#else
var binds = new UIBindProvider().Get();
#endif
if (binds == null || binds.Length <= 0)
{
Debug.LogError("没有找到绑定信息 或者 没有绑定信息 请检查");
return false;
}
g_UITypeToPkgInfo = new Dictionary<Type, UIBindVo>(binds.Length);
g_UIPathToPkgInfo = new Dictionary<string, Dictionary<string, UIBindVo>>();
g_UIPanelNameToPkgInfo = new Dictionary<string, UIBindVo>(binds.Length);
for (var i = 0; i < binds.Length; i++)
{
var vo = binds[i];
g_UITypeToPkgInfo[vo.CreatorType] = vo;
AddPkgInfoToPathDic(vo);
if (vo.CodeType == BasePanelType)
g_UIPanelNameToPkgInfo[vo.ResName] = vo;
}
IsInit = true;
return true;
}
private static void AddPkgInfoToPathDic(UIBindVo vo)
{
var pkgName = vo.PkgName;
var resName = vo.ResName;
if (!g_UIPathToPkgInfo.ContainsKey(pkgName))
{
g_UIPathToPkgInfo.Add(pkgName, new Dictionary<string, UIBindVo>());
}
var dic = g_UIPathToPkgInfo[pkgName];
if (dic.ContainsKey(resName))
{
Debug.LogError($"重复资源 请检查 {pkgName} {resName}");
return;
}
dic.Add(resName, vo);
}
/// <summary>
/// 得到UI包信息
/// </summary>
/// <param name="uiType"></param>
/// <returns></returns>
public static UIBindVo? GetBindVoByType(Type uiType)
{
if (uiType == null)
{
Debug.LogError($"空 无法取到这个包信息 请检查");
return null;
}
if (g_UITypeToPkgInfo.TryGetValue(uiType, out var vo))
{
return vo;
}
Debug.LogError($"未获取到这个UI包信息 请检查 {uiType.Name}");
return null;
}
/// <summary>
/// 得到UI包信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static UIBindVo? GetBindVoByType<T>()
{
return GetBindVoByType(typeof(T));
}
/// <summary>
/// 根据唯一ID获取
/// 由pkg+res 拼接的唯一ID
/// </summary>
public static UIBindVo? GetBindVoByPath(string pkgName, string resName)
{
if (string.IsNullOrEmpty(pkgName) || string.IsNullOrEmpty(resName))
{
Debug.LogError($"空名称 无法取到这个包信息 请检查");
return null;
}
if (!g_UIPathToPkgInfo.ContainsKey(pkgName))
{
Debug.LogError($"不存在这个包信息 请检查 {pkgName}");
return null;
}
if (g_UIPathToPkgInfo[pkgName].TryGetValue(resName, out var vo))
{
return vo;
}
Debug.LogError($"未获取到这个包信息 请检查 {pkgName} {resName}");
return null;
}
/// <summary>
/// 根据panelName获取
/// 只有是panel才会存在的信息
/// 非Panel请使用其他
/// </summary>
internal static UIBindVo? GetBindVoByPanelName(string panelName)
{
if (string.IsNullOrEmpty(panelName))
{
Debug.LogError($"空名称 无法取到这个包信息 请检查");
return null;
}
if (g_UIPanelNameToPkgInfo.TryGetValue(panelName, out var vo))
{
return vo;
}
Debug.LogError($"未获取到这个包信息 请检查 {panelName}");
return null;
}
/// <summary>
/// 重置 慎用
/// </summary>
public static void Reset()
{
if (g_UITypeToPkgInfo != null)
{
g_UITypeToPkgInfo.Clear();
g_UITypeToPkgInfo = null;
}
if (g_UIPathToPkgInfo != null)
{
g_UIPathToPkgInfo.Clear();
g_UIPathToPkgInfo = null;
}
if (g_UIPanelNameToPkgInfo != null)
{
g_UIPanelNameToPkgInfo.Clear();
g_UIPanelNameToPkgInfo = null;
}
IsInit = false;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d09de6223df6425081b47e22385f3747
timeCreated: 1682217977

View File

@@ -0,0 +1,151 @@
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace YIUIFramework
{
internal class UIBindProvider : ICodeGenerator<UIBindVo>
{
//业务代码相关程序集的名字
//默认有Unity默认程序集 可以根据需求修改
internal static string[] LogicAssemblyNames = { "Assembly-CSharp" };
private static Type[] GetLogicTypes()
{
return AppDomain.CurrentDomain.GetTypesByAssemblyName(LogicAssemblyNames);
}
private Type m_BasePanelType = typeof(BasePanel);
private Type m_BaseViewType = typeof(BaseView);
private Type m_BaseComponentType = typeof(BaseComponent);
public UIBindVo[] Get()
{
var gameTypes = GetLogicTypes();
if (gameTypes.Length < 1)
{
return Array.Empty<UIBindVo>();
}
var panelTypes = new List<Type>(); //继承panel的
var viewTypes = new List<Type>(); //继承View的
var componentTypes = new List<Type>(); //继承Component的
var binds = new List<UIBindVo>();
foreach (var gameType in gameTypes)
{
if (m_BasePanelType.IsAssignableFrom(gameType))
{
panelTypes.Add(gameType);
}
else if (m_BaseViewType.IsAssignableFrom(gameType))
{
viewTypes.Add(gameType);
}
else if (m_BaseComponentType.IsAssignableFrom(gameType))
{
componentTypes.Add(gameType);
}
}
//panel绑定
foreach (var panelType in panelTypes)
{
if (panelType.BaseType == null)
{
continue;
}
if (GetBindVo(panelType.BaseType, panelType, m_BasePanelType, out var bindVo))
{
binds.Add(bindVo);
}
}
//view绑定
foreach (var viewType in viewTypes)
{
if (viewType.BaseType == null)
{
continue;
}
if (GetBindVo(viewType.BaseType, viewType, m_BaseViewType, out var bindVo))
{
binds.Add(bindVo);
}
}
//component绑定
foreach (var componentType in componentTypes)
{
if (componentType.BaseType == null)
{
continue;
}
if (GetBindVo(componentType.BaseType, componentType, m_BaseComponentType, out var bindVo))
{
binds.Add(bindVo);
}
}
return binds.ToArray();
}
private static bool GetBindVo(Type uiBaseType, Type creatorType, Type groupType, out UIBindVo bindVo)
{
bindVo = new UIBindVo();
if (uiBaseType == null ||
!uiBaseType.GetFieldValue("PkgName", out bindVo.PkgName) ||
!uiBaseType.GetFieldValue("ResName", out bindVo.ResName))
{
return false;
}
bindVo.CodeType = uiBaseType.BaseType;
bindVo.BaseType = uiBaseType;
bindVo.CreatorType = creatorType;
return true;
}
public void WriteCode(UIBindVo info, StringBuilder sb)
{
sb.Append(" {\r\n");
sb.AppendFormat(" PkgName = {0}.PkgName,\r\n", info.BaseType.FullName);
sb.AppendFormat(" ResName = {0}.ResName,\r\n", info.BaseType.FullName);
sb.AppendFormat(" CodeType = {0},\r\n", GetCodeTypeName(info.CodeType));
sb.AppendFormat(" BaseType = typeof({0}),\r\n", info.BaseType.FullName);
sb.AppendFormat(" CreatorType = typeof({0}),\r\n", info.CreatorType.FullName);
sb.Append(" };\r\n");
}
private string GetCodeTypeName(Type uiBaseType)
{
if (uiBaseType == m_BasePanelType)
{
return UIStaticHelper.UIBasePanelName;
}
else if (uiBaseType == m_BaseViewType)
{
return UIStaticHelper.UIBaseViewName;
}
else if (uiBaseType == m_BaseComponentType)
{
return UIStaticHelper.UIBaseComponentName;
}
else
{
Debug.LogError($"当前类型错误 是否新增了类型 {uiBaseType}");
return UIStaticHelper.UIBaseName;
}
}
public void NewCode(UIBindVo info, StringBuilder sb)
{
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4e96ffe5c3d54fef915c3a7845173cb1
timeCreated: 1682218117

View File

@@ -0,0 +1,25 @@
using System;
namespace YIUIFramework
{
/// <summary>
/// UI资源绑定信息
/// </summary>
public struct UIBindVo
{
//基类
public Type CodeType; //Panel/View/Component
//当前继承类
public Type BaseType; //他继承的类 就是 当前 + Base
//当前类
public Type CreatorType;
//包名/模块名
public string PkgName;
//资源名
public string ResName;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4e6d04d42ac04fcd8433e8eeaeb75889
timeCreated: 1682218000