| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using UnityEngine;
- using UnityEngine.UI;
- namespace AdaptUI
- {
- [ExecuteAlways]
- [RequireComponent(typeof(RectTransform))]
- public class UIVerticalLayoutGroup : MonoBehaviour
- {
- [Header("Default Layout")]
- public UILayoutData defaultLayout;
- [Header("iPhone Layout")]
- public UILayoutData iPhoneLayout;
- [Header("iPad Layout")]
- public UILayoutData iPadLayout;
- private VerticalLayoutGroup layoutGroup;
- private bool isInitialized = false; // ✅ 防止重复初始化
- private void Reset()
- {
- layoutGroup = GetComponent<VerticalLayoutGroup>();
- if (!isInitialized)
- {
- // Debug.Log("UIVerticalLayoutGroup: 首次挂载,自动记录当前 UI 作为默认布局");
- // ✅ 记录当前 VerticalLayoutGroup 的参数
- defaultLayout = new UILayoutData
- {
- paddingLeft = Mathf.RoundToInt(layoutGroup.padding.left),
- paddingRight = Mathf.RoundToInt(layoutGroup.padding.right),
- paddingTop = Mathf.RoundToInt(layoutGroup.padding.top),
- paddingBottom = Mathf.RoundToInt(layoutGroup.padding.bottom),
- spacing = layoutGroup.spacing,
- childAlignment = layoutGroup.childAlignment,
- reverseArrangement = layoutGroup.reverseArrangement,
- controlChildWidth = layoutGroup.childControlWidth,
- controlChildHeight = layoutGroup.childControlHeight,
- useChildScaleWidth = layoutGroup.childScaleWidth,
- useChildScaleHeight = layoutGroup.childScaleHeight,
- childForceExpandWidth = layoutGroup.childForceExpandWidth,
- childForceExpandHeight = layoutGroup.childForceExpandHeight
- };
- iPhoneLayout = new UILayoutData(defaultLayout);
- iPadLayout = new UILayoutData(defaultLayout);
- isInitialized = true; // ✅ 避免 Awake() 误触
- }
- }
- private void Awake()
- {
- layoutGroup = GetComponent<VerticalLayoutGroup>();
- #if UNITY_EDITOR
- if (!isInitialized)
- {
- // Debug.Log("[Awake] ❌ 未初始化,跳过 ApplyLayout()");
- return;
- }
- #endif
- ApplyLayout();
- }
- public void ApplyLayout()
- {
- if (layoutGroup == null)
- {
- // Debug.LogError("[ApplyLayout] ❌ layoutGroup 为空,无法应用布局!");
- return;
- }
- UILayoutData layout = GetCurrentLayout();
- // Debug.Log($"[ApplyLayout] 📌 选中的布局数据: {layout}");
- // ✅ 设置 VerticalLayoutGroup 的布局参数
- layoutGroup.padding.left = layout.paddingLeft;
- layoutGroup.padding.right = layout.paddingRight;
- layoutGroup.padding.top = layout.paddingTop;
- layoutGroup.padding.bottom = layout.paddingBottom;
- layoutGroup.spacing = layout.spacing;
- layoutGroup.childAlignment = layout.childAlignment;
- layoutGroup.childControlWidth = layout.controlChildWidth;
- layoutGroup.childControlHeight = layout.controlChildHeight;
- layoutGroup.childScaleWidth = layout.useChildScaleWidth;
- layoutGroup.childScaleHeight = layout.useChildScaleHeight;
- layoutGroup.childForceExpandWidth = layout.childForceExpandWidth;
- layoutGroup.childForceExpandHeight = layout.childForceExpandHeight;
- // Debug.Log($"[ApplyLayout] 🔄 更新 layoutGroup: " +
- // $"Padding=({layoutGroup.padding.left}, {layoutGroup.padding.right}, {layoutGroup.padding.top}, {layoutGroup.padding.bottom}), " +
- // $"Spacing={layoutGroup.spacing}, Alignment={layoutGroup.childAlignment}, " +
- // $"Control(W,H)=({layoutGroup.childControlWidth}, {layoutGroup.childControlHeight}), " +
- // $"Scale(W,H)=({layoutGroup.childScaleWidth}, {layoutGroup.childScaleHeight}), " +
- // $"Expand(W,H)=({layoutGroup.childForceExpandWidth}, {layoutGroup.childForceExpandHeight})");
- // ✅ 强制更新布局
- LayoutRebuilder.ForceRebuildLayoutImmediate(layoutGroup.GetComponent<RectTransform>());
- Canvas.ForceUpdateCanvases();
- // Debug.Log("[ApplyLayout] ✅ 布局已更新!");
- }
- private UILayoutData GetCurrentLayout()
- {
- DeviceTypeHelper.DeviceType deviceType = DeviceTypeHelper.DetectDeviceType();
- switch (deviceType)
- {
- case DeviceTypeHelper.DeviceType.iPhone:
- // Debug.Log("[GetCurrentLayout] 📱 当前设备: iPhone,使用 iPhone 布局");
- return iPhoneLayout;
- case DeviceTypeHelper.DeviceType.iPad:
- // Debug.Log("[GetCurrentLayout] 📲 当前设备: iPad,使用 iPad 布局");
- return iPadLayout;
- default:
- // Debug.Log("[GetCurrentLayout] 💻 未知设备,使用 Default 布局");
- return defaultLayout;
- }
- }
- // [ContextMenu("手动应用布局")]
- // public void DebugApplyLayout()
- // {
- // ApplyLayout();
- // }
- }
- }
|