添加了更多的反射检查逻辑。

修复布局组件的缓存引用关系
This commit is contained in:
2026-04-28 17:13:23 +08:00
parent bcb13638c1
commit 5f93152c9b
20 changed files with 212 additions and 57 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ namespace XericUI.Helper
/// <param name="childAlignment">子项对齐</param> /// <param name="childAlignment">子项对齐</param>
/// <returns></returns> /// <returns></returns>
public static List<LayoutItem> CalculateGridLayout( public static List<LayoutItem> CalculateGridLayout(
List<LayoutItem> layoutItems, ref List<LayoutItem> layoutItems,
int childCount, int childCount,
Vector2 containerSize, Vector2 containerSize,
Vector2 cellSize, Vector2 cellSize,
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 29bdb71088ff44dd85e7485c8df8cd69
timeCreated: 1777358662
@@ -13,7 +13,6 @@ namespace XericUI.ReflectionUIGenerator
public readonly string LabelName; public readonly string LabelName;
public readonly string UIPropertyName; public readonly string UIPropertyName;
public readonly string UnitLabelName; public readonly string UnitLabelName;
public readonly bool MustFillIn;
public readonly int InspectorOrder; public readonly int InspectorOrder;
public readonly string GroupName; public readonly string GroupName;
public readonly string OnTriggerCall; public readonly string OnTriggerCall;
@@ -30,13 +29,12 @@ namespace XericUI.ReflectionUIGenerator
/// <param name="groupName">属性组名称(可以是以/&\分割的路径)</param> /// <param name="groupName">属性组名称(可以是以/&\分割的路径)</param>
/// <param name="onTriggerCall">属性附加的动作,调用此内联反射调用的函数</param> /// <param name="onTriggerCall">属性附加的动作,调用此内联反射调用的函数</param>
/// <param name="size">属性组名称(可以是以/&\分割的路径)</param> /// <param name="size">属性组名称(可以是以/&\分割的路径)</param>
public XuiavrgAttribute(string labelName, string uiPropertyName, string unitLabelName, bool mustFillin, public XuiavrgAttribute(string labelName, string uiPropertyName, string unitLabelName,
int inspectorOrder = 0, string groupName = null, string onTriggerCall = "", XuiavrgSize size = XuiavrgSize.Normal) int inspectorOrder = 0, string groupName = null, string onTriggerCall = "", XuiavrgSize size = XuiavrgSize.Normal)
{ {
LabelName = labelName; LabelName = labelName;
UIPropertyName = uiPropertyName; UIPropertyName = uiPropertyName;
UnitLabelName = unitLabelName; UnitLabelName = unitLabelName;
MustFillIn = mustFillin;
InspectorOrder = inspectorOrder; InspectorOrder = inspectorOrder;
GroupName = groupName; GroupName = groupName;
OnTriggerCall = onTriggerCall; OnTriggerCall = onTriggerCall;
@@ -52,7 +50,6 @@ namespace XericUI.ReflectionUIGenerator
property.labelName = LabelName; property.labelName = LabelName;
property.uiPropertyName = UIPropertyName; property.uiPropertyName = UIPropertyName;
property.unitLabelName = UnitLabelName; property.unitLabelName = UnitLabelName;
property.mustFillIn = MustFillIn;
property.inspectorOrder = InspectorOrder; property.inspectorOrder = InspectorOrder;
property.groupName = GroupName; property.groupName = GroupName;
property.onTriggerCall = OnTriggerCall; property.onTriggerCall = OnTriggerCall;
@@ -0,0 +1,28 @@
using System;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 集合检查
/// </summary>
public class XuiavrgCollectionCheckAttribute : Attribute, IXuiavrgAttribute
{
public readonly int minCount;
public readonly int maxCount;
public readonly bool notEmpty;
public XuiavrgCollectionCheckAttribute(int minCount, int maxCount, bool notEmpty)
{
this.minCount = minCount;
this.maxCount = maxCount;
this.notEmpty = notEmpty;
}
public void BindProperty(XuiavrgInspectorProperty property)
{
property.collectionMinCount = minCount;
property.collectionMaxCount = maxCount;
property.collectionNotEmpty = notEmpty;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4ec4224cb3844c439dd5fdb6b31c9730
timeCreated: 1777363387
@@ -0,0 +1,15 @@
using System;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 不可为空
/// </summary>
public class XuiavrgNotEmptyAttribute : Attribute, IXuiavrgAttribute
{
public void BindProperty(XuiavrgInspectorProperty property)
{
property.notEmpty = true;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 89e35e544d66421c8cc524d3b4d7b943
timeCreated: 1777360693
@@ -0,0 +1,25 @@
using System;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 数值检查
/// </summary>
public class XuiavrgNumberCheckAttribute : Attribute, IXuiavrgAttribute
{
public readonly float minValue;
public readonly float maxValue;
public XuiavrgNumberCheckAttribute(float minValue, float maxValue)
{
this.minValue = minValue;
this.maxValue = maxValue;
}
public void BindProperty(XuiavrgInspectorProperty property)
{
property.numberMinValue = minValue;
property.numberMaxValue = maxValue;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d5b533c6d6ca4de8811adc3913d1fd9b
timeCreated: 1777362005
@@ -0,0 +1,16 @@
using System;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 必填
/// </summary>
public class XuiavrgRequiredAttribute : Attribute, IXuiavrgAttribute
{
public void BindProperty(XuiavrgInspectorProperty property)
{
property.required = true;
property.notEmpty = true;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bcc4b8b735e84251b11ef87e9fe5b2cb
timeCreated: 1777358718
@@ -0,0 +1,48 @@
using System;
using UnityEngine;
namespace XericUI.ReflectionUIGenerator
{
/// <summary>
/// 字符串检查
/// </summary>
public class XuiavrgStringCheckAttribute : Attribute, IXuiavrgAttribute
{
public readonly int minLength;
public readonly int maxLength;
public readonly string regexPattern;
public readonly bool onlyLetters;
public readonly bool onlyNumber;
public readonly bool onlyEmail;
public readonly bool onlyPhone;
public readonly bool onlyUrl;
public XuiavrgStringCheckAttribute(
int minLength = 0, int maxLength = int.MaxValue,
string regexPattern = "",
bool onlyLetters = false, bool onlyNumber = false, bool onlyEmail = true, bool onlyPhone = true,
bool onlyUrl = true)
{
this.minLength = minLength;
this.maxLength = maxLength;
this.regexPattern = regexPattern;
this.onlyLetters = onlyLetters;
this.onlyNumber = onlyNumber;
this.onlyEmail = onlyEmail;
this.onlyPhone = onlyPhone;
this.onlyUrl = onlyUrl;
}
public void BindProperty(XuiavrgInspectorProperty property)
{
property.stringMinLength = minLength;
property.stringMaxLength = maxLength;
property.stringRegexPattern = regexPattern;
property.stringOnlyLetters = onlyLetters;
property.stringOnlyNumber = onlyNumber;
property.stringOnlyEmail = onlyEmail;
property.stringOnlyPhone = onlyPhone;
property.stringOnlyUrl = onlyUrl;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0a0e34665ed24a7c87e5eb84a45f6ef6
timeCreated: 1777360997
@@ -1,5 +1,6 @@
using System; using System;
using System.Reflection; using System.Reflection;
using UnityEngine;
using UnityEngine.Serialization; using UnityEngine.Serialization;
namespace XericUI.ReflectionUIGenerator namespace XericUI.ReflectionUIGenerator
@@ -10,21 +11,47 @@ namespace XericUI.ReflectionUIGenerator
[Serializable] [Serializable]
public class XuiavrgInspectorProperty public class XuiavrgInspectorProperty
{ {
#region 字段属性
public object valueTarget; public object valueTarget;
public bool isProperty; public bool isProperty;
public PropertyInfo propertyInfo; public PropertyInfo propertyInfo;
public FieldInfo fieldInfo; public FieldInfo fieldInfo;
public string labelName; // 基本信息
public string uiPropertyName; public string labelName; // 字段名称
public string unitLabelName; public string uiPropertyName; // ui属性名称
public bool mustFillIn; public string unitLabelName; // 单位名称
public int inspectorOrder; public bool required; // 项目必填
public string groupName; public bool notEmpty; // 不可为空
public string onTriggerCall; public int inspectorOrder; // 显示在检查器上的顺序
public XuiavrgSize argumentSize; public string groupName; // 编组名称
public bool hideInInspector; public string onTriggerCall; // 值变更时触发的反射
public XuiavrgSize argumentSize; // 属性尺寸
public bool hideInInspector; // 在检查器上隐藏显示
// 字符串检查
public int stringMinLength; // 最小长度
public int stringMaxLength = int.MaxValue; // 最大长度
public string stringRegexPattern = ""; // 正则检查
public bool stringOnlyLetters; // 允许字符
public bool stringOnlyNumber; // 允许数字
public bool stringOnlyEmail = true; // 允许邮箱
public bool stringOnlyPhone = true; // 允许电话号码
public bool stringOnlyUrl = true; // 允许网址
// 数值检查
public float numberMinValue; // 最小值
public float numberMaxValue; // 最大值
// 集合检查
public int collectionMinCount; // 集合的最小尺寸
public int collectionMaxCount = int.MaxValue; // 集合的最大尺寸
public bool collectionNotEmpty; // 集合不可为空
#endregion
#region 检查方法
/// <summary> /// <summary>
/// 绑定到数据端 /// 绑定到数据端
@@ -138,5 +165,7 @@ namespace XericUI.ReflectionUIGenerator
public void Clear() public void Clear()
{ {
} }
#endregion
} }
} }
+22 -43
View File
@@ -136,13 +136,15 @@ namespace XericUI.OverrideLayout
protected void RefreshPivot() protected void RefreshPivot()
{ {
if (!item) var itemRect = itemRectTransform;
if (!itemRect)
{ {
Debug.LogError($"滚动视图的实体对象在刷新前并未指定,请先指定{nameof(item)}后再调用{nameof(RefreshPivot)}"); Debug.LogError($"滚动视图的实体对象在刷新前并未指定,请先指定{nameof(item)}后再调用{nameof(RefreshPivot)}");
} }
parentScrollRect.UpdateChildPivot(layout, item.RectTransform()); parentScrollRect.UpdateChildPivot(layout, itemRect);
itemRectTransform.anchoredPosition = layout.position; itemRect.anchoredPosition = layout.position;
itemRect.SetDeltaSize(layout.size);
} }
public void UpdateLayout(GridLayoutCalculator.LayoutItem layoutData) public void UpdateLayout(GridLayoutCalculator.LayoutItem layoutData)
@@ -311,19 +313,19 @@ namespace XericUI.OverrideLayout
// 将视口尺寸转换到content本地空间 // 将视口尺寸转换到content本地空间
// 手动计算视口在content空间的矩形 // 手动计算视口在content空间的矩形
viewport.GetWorldCorners(viewportVertex); viewport.GetWorldCorners(viewportVertex);
var preloadMarginVec2 = new Vector3(m_preloadMargin, m_preloadMargin);
var viewportRect = new Rect( var viewportRect = new Rect(
viewportVertex[0], viewportVertex[0] - preloadMarginVec2,
viewportVertex[2] - viewportVertex[0]); viewportVertex[2] - viewportVertex[0] + preloadMarginVec2 + preloadMarginVec2);
// 扩展可见区域边界
visibleMin = viewportRect.min;
visibleMax = viewportRect.max;
var contentRect = content.rect; var contentRect = content.rect;
// 计算预加载边距(考虑content缩放) // 计算预加载边距(考虑content缩放)
// var scaledMargin = preloadMargin / ((content.lossyScale.x + content.lossyScale.y) * 0.5f); // var scaledMargin = preloadMargin / ((content.lossyScale.x + content.lossyScale.y) * 0.5f);
// 扩展可见区域边界
var preloadMarginVec2 = new Vector2(m_preloadMargin, m_preloadMargin);
visibleMin = viewportRect.min - preloadMarginVec2;
visibleMax = viewportRect.max + preloadMarginVec2;
// 获取当前滚动归一化位置 // 获取当前滚动归一化位置
// var scrollPos = new Vector2( // var scrollPos = new Vector2(
// horizontal ? horizontalScrollbar?.value ?? 0 : 0, // horizontal ? horizontalScrollbar?.value ?? 0 : 0,
@@ -363,14 +365,7 @@ namespace XericUI.OverrideLayout
} }
} }
bool CheckIsVisibal(Vector2 pos) bool CheckIsVisibal(Vector2 pos) => viewportRect.Contains(pos);
{
return
pos.x >= visibleMin.x &&
pos.x <= visibleMax.x &&
pos.y >= visibleMin.y &&
pos.y <= visibleMax.y;
}
} }
#endregion #endregion
@@ -485,35 +480,19 @@ namespace XericUI.OverrideLayout
private void RefreshContentSizeByChildrenLayouts(List<GridLayoutCalculator.LayoutItem> layoutItems) private void RefreshContentSizeByChildrenLayouts(List<GridLayoutCalculator.LayoutItem> layoutItems)
{ {
if (layoutItems.Count == 0) return; if (layoutItems.Count == 0) return;
var size = Vector2.zero;
var first = layoutItems[0]; var spacing = new Vector2(
var last = layoutItems[^1]; GridLayoutCalculator.Grid.y * m_spacing.x,
GridLayoutCalculator.Grid.x * m_spacing.y);
var size = first.position - last.position;
size = size.Abs();
// 计算实际使用的行列数
int maxRow = (int)GridLayoutCalculator.Grid.x;
int maxColumn = (int)GridLayoutCalculator.Grid.y;
foreach (var item in layoutItems)
{
maxRow = Mathf.Max(maxRow, item.rowIndex);
maxColumn = Mathf.Max(maxColumn, item.columnIndex);
}
int actualCellCountX = maxColumn + 1;
int actualCellCountY = maxRow + 1;
var spacing = new Vector2((actualCellCountX - 1) * m_spacing.x, (actualCellCountY - 1) * m_spacing.y);
switch (m_startAxis) switch (m_startAxis)
{ {
case GridLayoutGroup.Axis.Horizontal: case GridLayoutGroup.Axis.Horizontal:
size.x = Mathf.Max(size.x, viewport.rect.width); size.x = Mathf.Max(spacing.x, viewport.rect.width);
size.y = Mathf.Max(size.y + m_spacing.y, m_cellSize.y); size.y = Mathf.Max(spacing.y, m_cellSize.y);
break; break;
case GridLayoutGroup.Axis.Vertical: case GridLayoutGroup.Axis.Vertical:
size.x = Mathf.Max(size.x + m_spacing.x, m_cellSize.x); size.x = Mathf.Max(spacing.x, m_cellSize.x);
size.y = Mathf.Max(size.y, viewport.rect.height); size.y = Mathf.Max(spacing.y, viewport.rect.height);
break; break;
} }
@@ -568,7 +547,7 @@ namespace XericUI.OverrideLayout
constraintCount = m_constraintCount; constraintCount = m_constraintCount;
return GridLayoutCalculator.CalculateGridLayout( return GridLayoutCalculator.CalculateGridLayout(
_layoutItems, ref _layoutItems,
childCount: childrenCount, childCount: childrenCount,
containerSize: ContextSize, containerSize: ContextSize,
cellSize: m_cellSize, cellSize: m_cellSize,