初始化

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,46 @@
//------------------------------------------------------------
// Author: 亦亦
// Mail: 379338943@qq.com
// Data: 2023年2月12日
//------------------------------------------------------------
using UnityEngine;
using Cysharp.Threading.Tasks;
namespace YIUIFramework
{
public static partial class YIUIFactory
{
//普通的UI预制体 创建与摧毁 一定要成对
//为了防止忘记 所以默认自动回收
public static GameObject InstantiateGameObject(string pkgName, string resName)
{
var obj = YIUILoadHelper.LoadAssetInstantiate(pkgName, resName);
if (obj == null)
{
Debug.LogError($"没有加载到这个资源 {pkgName}/{resName}");
return null;
}
//强制添加 既然你要使用这个方法那就必须接受 否则请使用其他方式
//被摧毁时 自动回收 无需调用 UIFactory.Destroy
obj.AddComponent<YIUIReleaseInstantiate>();
return obj;
}
public static async UniTask<GameObject> InstantiateGameObjectAsync(string pkgName, string resName)
{
var obj = await YIUILoadHelper.LoadAssetAsyncInstantiate(pkgName, resName);
if (obj == null)
{
Debug.LogError($"没有加载到这个资源 {pkgName}/{resName}");
return null;
}
obj.AddComponent<YIUIReleaseInstantiate>();
return obj;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 611e6023ac2e4dcb86fc889f289678bb
timeCreated: 1682498501

View File

@@ -0,0 +1,31 @@
//------------------------------------------------------------
// Author: 亦亦
// Mail: 379338943@qq.com
// Data: 2023年2月12日
//------------------------------------------------------------
using UnityEngine;
namespace YIUIFramework
{
public static partial class YIUIFactory
{
internal static void Destroy(GameObject obj)
{
YIUILoadHelper.ReleaseInstantiate(obj);
}
//内部会自动调用
//一定要使用本类中的创建 否则会有报错提示
internal static void Destroy(UIBase uiBase)
{
if (uiBase.OwnerGameObject == null)
{
Debug.LogError($"此UI 是空对象 请检查{uiBase.UIBindVo.PkgName} {uiBase.UIBindVo.ResName}");
return;
}
Destroy(uiBase.OwnerGameObject);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 01802f5fb7dc49d383cbfa99c13687ed
timeCreated: 1688113765

View File

@@ -0,0 +1,125 @@
//------------------------------------------------------------
// Author: 亦亦
// Mail: 379338943@qq.com
// Data: 2023年2月12日
//------------------------------------------------------------
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
namespace YIUIFramework
{
public static partial class YIUIFactory
{
private static void SetParent(RectTransform self, RectTransform parent)
{
self.SetParent(parent, false);
self.AutoReset();
}
internal static UIBase CreateCommon(string pkgName, string resName, GameObject obj)
{
var bingVo = UIBindHelper.GetBindVoByPath(pkgName, resName);
if (bingVo == null) return null;
var vo = bingVo.Value;
return CreateByObjVo(vo, obj);
}
internal static UIBase CreatePanel(PanelInfo panelInfo)
{
return Create(panelInfo.PkgName, panelInfo.ResName);
}
private static T Create<T>() where T : UIBase
{
var data = UIBindHelper.GetBindVoByType<T>();
if (data == null) return null;
var vo = data.Value;
return (T)Create(vo);
}
private static UIBase Create(string pkgName, string resName)
{
var bingVo = UIBindHelper.GetBindVoByPath(pkgName, resName);
return bingVo == null ? null : Create(bingVo.Value);
}
private static UIBase Create(UIBindVo vo)
{
var obj = YIUILoadHelper.LoadAssetInstantiate(vo.PkgName, vo.ResName);
if (obj == null)
{
Debug.LogError($"没有加载到这个资源 {vo.PkgName}/{vo.ResName}");
return null;
}
return CreateByObjVo(vo, obj);
}
private static UIBase CreateByObjVo(UIBindVo vo, GameObject obj)
{
var cdeTable = obj.GetComponent<UIBindCDETable>();
if (cdeTable == null)
{
Debug.LogError($"{obj.name} 没有 UIBindCDETable 组件 无法创建 请检查");
return null;
}
var lastActive = cdeTable.gameObject.activeSelf;
if (!lastActive)
{
//加载时必须保证处于激活状态 否则一些Mono相关未初始化导致不可预知的错误
cdeTable.gameObject.SetActive(true);
}
cdeTable.CreateComponent();
var uiBase = (UIBase)Activator.CreateInstance(vo.CreatorType);
uiBase.InitUIBase(vo, obj);
if (!lastActive)
{
//如果之前是隐藏的状态 则还原
//此时已经初始化完毕 所以可能会收到被隐藏的消息 请自行酌情处理
cdeTable.gameObject.SetActive(false);
}
return uiBase;
}
private static void CreateComponent(this UIBindCDETable cdeTable)
{
foreach (var childCde in cdeTable.AllChildCdeTable)
{
if (childCde == null)
{
Debug.LogError($"{cdeTable.name} 存在null对象的childCde 检查是否因为删除或丢失或未重新生成");
continue;
}
var lastActive = childCde.gameObject.activeSelf;
if (!lastActive)
{
//加载时必须保证处于激活状态 否则一些Mono相关未初始化导致不可预知的错误
childCde.gameObject.SetActive(true);
}
var bingVo = UIBindHelper.GetBindVoByPath(childCde.PkgName, childCde.ResName);
if (bingVo == null) continue;
var childBase = (UIBase)Activator.CreateInstance(bingVo.Value.CreatorType);
childCde.CreateComponent();
childBase.InitUIBase(bingVo.Value, childCde.gameObject);
cdeTable.AddUIBase(childCde.gameObject.name, childBase);
if (!lastActive)
{
//如果之前是隐藏的状态 则还原
//此时已经初始化完毕 所以可能会收到被隐藏的消息 请自行酌情处理
childCde.gameObject.SetActive(false);
}
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 22cb104eb4d94e60bdbc322695d370f8
timeCreated: 1688113831

View File

@@ -0,0 +1,77 @@
//------------------------------------------------------------
// Author: 亦亦
// Mail: 379338943@qq.com
// Data: 2023年2月12日
//------------------------------------------------------------
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
namespace YIUIFramework
{
public static partial class YIUIFactory
{
public static async UniTask<T> InstantiateAsync<T>(RectTransform parent = null) where T : UIBase
{
var data = UIBindHelper.GetBindVoByType<T>();
if (data == null) return null;
var vo = data.Value;
return await InstantiateAsync<T>(vo, parent);
}
public static async UniTask<T> InstantiateAsync<T>(UIBindVo vo, RectTransform parent = null) where T : UIBase
{
var uiBase = await CreateAsync(vo);
SetParent(uiBase.OwnerRectTransform, parent ? parent : PanelMgr.Inst.UICache);
return (T)uiBase;
}
public static async UniTask<UIBase> InstantiateAsync(UIBindVo vo, RectTransform parent = null)
{
var uiBase = await CreateAsync(vo);
SetParent(uiBase.OwnerRectTransform, parent ? parent : PanelMgr.Inst.UICache);
return uiBase;
}
public static async UniTask<UIBase> InstantiateAsync(Type uiType, RectTransform parent = null)
{
var data = UIBindHelper.GetBindVoByType(uiType);
if (data == null) return null;
var vo = data.Value;
return await InstantiateAsync(vo, parent);
}
public static async UniTask<UIBase> InstantiateAsync(string pkgName, string resName,
RectTransform parent = null)
{
var data = UIBindHelper.GetBindVoByPath(pkgName, resName);
if (data == null) return null;
var vo = data.Value;
return await InstantiateAsync(vo, parent);
}
internal static async UniTask<UIBase> CreatePanelAsync(PanelInfo panelInfo)
{
var bingVo = UIBindHelper.GetBindVoByPath(panelInfo.PkgName, panelInfo.ResName);
if (bingVo == null) return null;
var uiBase = await CreateAsync(bingVo.Value);
return uiBase;
}
private static async UniTask<UIBase> CreateAsync(UIBindVo vo)
{
var obj = await YIUILoadHelper.LoadAssetAsyncInstantiate(vo.PkgName, vo.ResName);
if (obj == null)
{
Debug.LogError($"没有加载到这个资源 {vo.PkgName}/{vo.ResName}");
return null;
}
return CreateByObjVo(vo, obj);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 281fac6207be48379ad9008ad4837b79
timeCreated: 1688113666

View File

@@ -0,0 +1,62 @@
//------------------------------------------------------------
// Author: 亦亦
// Mail: 379338943@qq.com
// Data: 2023年2月12日
//------------------------------------------------------------
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
namespace YIUIFramework
{
public static partial class YIUIFactory
{
public static T Instantiate<T>(RectTransform parent = null) where T : UIBase
{
var data = UIBindHelper.GetBindVoByType<T>();
if (data == null) return null;
var vo = data.Value;
return Instantiate<T>(vo, parent);
}
public static T Instantiate<T>(UIBindVo vo, RectTransform parent = null) where T : UIBase
{
var instance = (T)Create(vo);
if (instance == null) return null;
SetParent(instance.OwnerRectTransform, parent ? parent : PanelMgr.Inst.UICache);
return instance;
}
public static UIBase Instantiate(Type uiType, RectTransform parent = null)
{
var data = UIBindHelper.GetBindVoByType(uiType);
if (data == null) return null;
var vo = data.Value;
return Instantiate(vo, parent);
}
public static UIBase Instantiate(UIBindVo vo, RectTransform parent = null)
{
var instance = Create(vo);
if (instance == null) return null;
SetParent(instance.OwnerRectTransform, parent ? parent : PanelMgr.Inst.UICache);
return instance;
}
public static UIBase Instantiate(string pkgName, string resName, RectTransform parent = null)
{
var data = UIBindHelper.GetBindVoByPath(pkgName, resName);
if (data == null) return null;
var vo = data.Value;
return Instantiate(vo, parent);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f0e66f740bcd4b918e3ef478577e28f4
timeCreated: 1692273960

View File

@@ -0,0 +1,59 @@
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// 创建一个UI空对象时
/// </summary>
public static class YIUIRectFactory
{
public static RectTransform CreateUIRect(RectTransform parent)
{
var obj = new GameObject();
var rect = obj.AddComponent<RectTransform>();
if (parent != null)
rect.SetParent(parent);
return rect;
}
//重置为全屏自适应UI
public static void ResetToFullScreen(this RectTransform self)
{
self.anchorMin = Vector2.zero;
self.anchorMax = Vector2.one;
self.anchoredPosition3D = Vector3.zero;
self.pivot = new Vector2(0.5f, 0.5f);
self.offsetMax = Vector2.zero;
self.offsetMin = Vector2.zero;
self.sizeDelta = Vector2.zero;
self.localEulerAngles = Vector3.zero;
self.localScale = Vector3.one;
}
//重置位置与旋转
public static void ResetLocalPosAndRot(this RectTransform self)
{
self.localPosition = Vector3.zero;
self.localRotation = Quaternion.identity;
}
/// <summary>
/// 自动重置
/// 一般情况下就2种 全屏的 那就全部归一
/// 其他的 那就什么都不改 只修改大小就可以了
/// </summary>
public static void AutoReset(this RectTransform self)
{
if (self.anchorMax == Vector2.one && self.anchorMin == Vector2.zero)
{
self.ResetToFullScreen();
}
else
{
self.localScale = Vector3.one;
}
self.ResetLocalPosAndRot();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1a3f42862c8a4af8b51a0bd94c3b21eb
timeCreated: 1682505904

View File

@@ -0,0 +1,21 @@
using System;
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// 一般的普通UI 被创建必须调用 UIFactory.Destroy 同步释放资源
/// 但是有些人真的会忘记 这里额外增加一个mono脚本 摧毁时自动调用
/// 缺点就是多了一个mono脚本 肯定消耗会变高的
/// 如果你创建的对象 你知道什么时候摧毁 就不要使用这个了
/// 如果你真的不知道什么时候移除 或者 不想管理 也接受多余消耗 可以挂载他
/// UIBase 类无需
/// </summary>
public class YIUIReleaseInstantiate : MonoBehaviour
{
private void OnDestroy()
{
YIUIFactory.Destroy(gameObject);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ae8f32640581423fa975430ffcc991f9
timeCreated: 1683627175