From 78aa102238c4c10b563aad30f2749e50cbed35dd Mon Sep 17 00:00:00 2001 From: shenjianxing <”315615051@qq.com“> Date: Tue, 15 Apr 2025 13:54:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=98=BE=E7=A4=BAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Art/UIPrefab/UIBody3DInfo.prefab | 149 ++++++++++++++------ Assets/Scripts/Tools/CustomWrapGrid.cs | 93 ++++++++++++ Assets/Scripts/Tools/CustomWrapGrid.cs.meta | 11 ++ 3 files changed, 207 insertions(+), 46 deletions(-) create mode 100644 Assets/Scripts/Tools/CustomWrapGrid.cs create mode 100644 Assets/Scripts/Tools/CustomWrapGrid.cs.meta diff --git a/Assets/Art/UIPrefab/UIBody3DInfo.prefab b/Assets/Art/UIPrefab/UIBody3DInfo.prefab index 9c8ce25f..8ce73078 100644 --- a/Assets/Art/UIPrefab/UIBody3DInfo.prefab +++ b/Assets/Art/UIPrefab/UIBody3DInfo.prefab @@ -945,9 +945,8 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 1076934826281074923} - - component: {fileID: 5301118011458687740} - component: {fileID: 8755480884770592513} - - component: {fileID: 2032849314590666560} + - component: {fileID: 841870042491869694} m_Layer: 0 m_Name: ListContent m_TagString: Untagged @@ -967,39 +966,13 @@ RectTransform: m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] - m_Father: {fileID: 683708360215293002} + m_Father: {fileID: 7980721957373656544} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0.5, y: 0.5} ---- !u!114 &5301118011458687740 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1591183619690481046} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 30649d3a9faa99c48a7b1166b86bf2a0, type: 3} - m_Name: - m_EditorClassIdentifier: - m_Padding: - m_Left: 0 - m_Right: 0 - m_Top: 0 - m_Bottom: 0 - m_ChildAlignment: 3 - m_Spacing: 0 - m_ChildForceExpandWidth: 0 - m_ChildForceExpandHeight: 0 - m_ChildControlWidth: 1 - m_ChildControlHeight: 1 - m_ChildScaleWidth: 0 - m_ChildScaleHeight: 0 - m_ReverseArrangement: 0 --- !u!114 &8755480884770592513 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1016,7 +989,7 @@ MonoBehaviour: CustomComponentName: ListContent CustomComment: mComponentName: RectTransform ---- !u!114 &2032849314590666560 +--- !u!114 &841870042491869694 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -1025,17 +998,17 @@ MonoBehaviour: m_GameObject: {fileID: 1591183619690481046} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 306cc8c2b49d7114eaa3623786fc2126, type: 3} + m_Script: {fileID: 11500000, guid: 04acbd8265ebc9e4b8824e08b084e953, type: 3} m_Name: m_EditorClassIdentifier: - m_IgnoreLayout: 0 - m_MinWidth: -1 - m_MinHeight: -1 - m_PreferredWidth: -1 - m_PreferredHeight: -1 - m_FlexibleWidth: -1 - m_FlexibleHeight: -1 - m_LayoutPriority: 1 + m_Padding: + m_Left: 0 + m_Right: 0 + m_Top: 0 + m_Bottom: 0 + m_ChildAlignment: 0 + spacingX: 0 + spacingY: 0 --- !u!1 &2074544796856548067 GameObject: m_ObjectHideFlags: 0 @@ -3369,9 +3342,9 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 1} m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 32.504997, y: 107.39996} - m_SizeDelta: {x: 35.01, y: 19.61} - m_Pivot: {x: 0.5, y: 1} + m_AnchoredPosition: {x: 14.999878, y: 88} + m_SizeDelta: {x: 80.03, y: 19.61} + m_Pivot: {x: -0.00000017136335, y: 0.00000078976154} --- !u!222 &8371822126990485976 CanvasRenderer: m_ObjectHideFlags: 0 @@ -4030,7 +4003,7 @@ RectTransform: m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: - - {fileID: 1076934826281074923} + - {fileID: 7980721957373656544} - {fileID: 8633842398434044872} m_Father: {fileID: 7693151069151688728} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -4266,8 +4239,8 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 1} m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 17.505, y: -9.805} - m_SizeDelta: {x: 35.01, y: 19.61} + m_AnchoredPosition: {x: 40.015, y: -9.805} + m_SizeDelta: {x: 80.03, y: 19.61} m_Pivot: {x: 0.5, y: 0.5} --- !u!222 &1941232802810836953 CanvasRenderer: @@ -4297,7 +4270,7 @@ MonoBehaviour: m_OnCullStateChanged: m_PersistentCalls: m_Calls: [] - m_text: "\u808C\u8089/" + m_text: "\u808C\u8089/123123" m_isRightToLeft: 0 m_fontAsset: {fileID: 11400000, guid: 97936a7b3a86809479ac89371e95b079, type: 2} m_sharedMaterial: {fileID: 168401102475399263, guid: 97936a7b3a86809479ac89371e95b079, type: 2} @@ -4697,6 +4670,90 @@ MonoBehaviour: m_FillOrigin: 0 m_UseSpriteMesh: 0 m_PixelsPerUnitMultiplier: 1 +--- !u!1 &8460087031516395123 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 7980721957373656544} + - component: {fileID: 5416135810207341871} + - component: {fileID: 7839656811540880901} + m_Layer: 0 + m_Name: GameObject + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &7980721957373656544 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8460087031516395123} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: + - {fileID: 1076934826281074923} + m_Father: {fileID: 683708360215293002} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 0, y: 0} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &5416135810207341871 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8460087031516395123} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 59f8146938fff824cb5fd77236b75775, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Padding: + m_Left: 0 + m_Right: 0 + m_Top: 0 + m_Bottom: 0 + m_ChildAlignment: 0 + m_Spacing: 0 + m_ChildForceExpandWidth: 1 + m_ChildForceExpandHeight: 0 + m_ChildControlWidth: 1 + m_ChildControlHeight: 1 + m_ChildScaleWidth: 0 + m_ChildScaleHeight: 0 + m_ReverseArrangement: 0 +--- !u!114 &7839656811540880901 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8460087031516395123} + m_Enabled: 0 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 306cc8c2b49d7114eaa3623786fc2126, type: 3} + m_Name: + m_EditorClassIdentifier: + m_IgnoreLayout: 0 + m_MinWidth: -1 + m_MinHeight: 25 + m_PreferredWidth: -1 + m_PreferredHeight: -1 + m_FlexibleWidth: -1 + m_FlexibleHeight: -1 + m_LayoutPriority: 1 --- !u!1 &8477438582353967822 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Tools/CustomWrapGrid.cs b/Assets/Scripts/Tools/CustomWrapGrid.cs new file mode 100644 index 00000000..98c5fc63 --- /dev/null +++ b/Assets/Scripts/Tools/CustomWrapGrid.cs @@ -0,0 +1,93 @@ +using UnityEngine; +using UnityEngine.UI; + +[ExecuteAlways] +public class CustomWrapGrid : LayoutGroup +{ + public float spacingX = 0f; + public float spacingY = 0f; + + private float _containerWidth; + private Vector2 _currentPos; + // ¼ܸ߶ + private float _totalHeight; + protected override void OnEnable() + { + base.OnEnable(); + CalculateLayout(); + } + + public override void SetLayoutHorizontal() + { + CalculateLayout(); + } + + public override void SetLayoutVertical() + { + // Ҫʵ֣Ϊ߼SetLayoutHorizontal + } + + public override void CalculateLayoutInputVertical() + { + CalculateLayout(); + float minHeight = _totalHeight; + SetLayoutInputForAxis(minHeight, minHeight, 0, 1); // + } + + void CalculateLayout() + { + _totalHeight = 0f; // ܸ߶ + if (rectChildren.Count == 0) return; + + _containerWidth = rectTransform.rect.width - padding.horizontal; + _currentPos = new Vector2(padding.left, -padding.top); + float rowHeight = 0f; + foreach (RectTransform child in rectChildren) + { + // صĶ + if (!child.gameObject.activeSelf) continue; + + // ȡߴ + float childWidth = LayoutUtility.GetPreferredWidth(child); + float childHeight = LayoutUtility.GetPreferredHeight(child); + + // ж + if (_currentPos.x + childWidth > _containerWidth - padding.right) + { + StartNewRow(ref _currentPos, ref rowHeight); + } + + // λ + SetChildAlongAxis(child, 0, _currentPos.x, childWidth); + SetChildAlongAxis(child, 1, _currentPos.y, childHeight); + + // µǰ״̬ + _currentPos.x += childWidth + spacingX; + rowHeight = Mathf.Max(rowHeight, childHeight); + } + // ռһи߶ + _totalHeight = Mathf.Abs(_currentPos.y) + rowHeight - padding.top; + } + + void StartNewRow(ref Vector2 pos, ref float rowHeight) + { + pos.x = padding.left; + pos.y += rowHeight + spacingY; + rowHeight = 0f; + } + + // 仯ʱԶˢ + protected override void OnRectTransformDimensionsChange() + { + base.OnRectTransformDimensionsChange(); + CalculateLayout(); + } + +#if UNITY_EDITOR + protected override void OnValidate() + { + base.OnValidate(); + CalculateLayout(); + } +#endif +} \ No newline at end of file diff --git a/Assets/Scripts/Tools/CustomWrapGrid.cs.meta b/Assets/Scripts/Tools/CustomWrapGrid.cs.meta new file mode 100644 index 00000000..43ad1d08 --- /dev/null +++ b/Assets/Scripts/Tools/CustomWrapGrid.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 04acbd8265ebc9e4b8824e08b084e953 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: