UIVerticalLayoutGroup.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace AdaptUI
  4. {
  5. [ExecuteAlways]
  6. [RequireComponent(typeof(RectTransform))]
  7. public class UIVerticalLayoutGroup : MonoBehaviour
  8. {
  9. [Header("Default Layout")]
  10. public UILayoutData defaultLayout;
  11. [Header("iPhone Layout")]
  12. public UILayoutData iPhoneLayout;
  13. [Header("iPad Layout")]
  14. public UILayoutData iPadLayout;
  15. private VerticalLayoutGroup layoutGroup;
  16. private bool isInitialized = false; // ✅ 防止重复初始化
  17. private void Reset()
  18. {
  19. layoutGroup = GetComponent<VerticalLayoutGroup>();
  20. if (!isInitialized)
  21. {
  22. // Debug.Log("UIVerticalLayoutGroup: 首次挂载,自动记录当前 UI 作为默认布局");
  23. // ✅ 记录当前 VerticalLayoutGroup 的参数
  24. defaultLayout = new UILayoutData
  25. {
  26. paddingLeft = Mathf.RoundToInt(layoutGroup.padding.left),
  27. paddingRight = Mathf.RoundToInt(layoutGroup.padding.right),
  28. paddingTop = Mathf.RoundToInt(layoutGroup.padding.top),
  29. paddingBottom = Mathf.RoundToInt(layoutGroup.padding.bottom),
  30. spacing = layoutGroup.spacing,
  31. childAlignment = layoutGroup.childAlignment,
  32. reverseArrangement = layoutGroup.reverseArrangement,
  33. controlChildWidth = layoutGroup.childControlWidth,
  34. controlChildHeight = layoutGroup.childControlHeight,
  35. useChildScaleWidth = layoutGroup.childScaleWidth,
  36. useChildScaleHeight = layoutGroup.childScaleHeight,
  37. childForceExpandWidth = layoutGroup.childForceExpandWidth,
  38. childForceExpandHeight = layoutGroup.childForceExpandHeight
  39. };
  40. iPhoneLayout = new UILayoutData(defaultLayout);
  41. iPadLayout = new UILayoutData(defaultLayout);
  42. isInitialized = true; // ✅ 避免 Awake() 误触
  43. }
  44. }
  45. private void Awake()
  46. {
  47. layoutGroup = GetComponent<VerticalLayoutGroup>();
  48. #if UNITY_EDITOR
  49. if (!isInitialized)
  50. {
  51. // Debug.Log("[Awake] ❌ 未初始化,跳过 ApplyLayout()");
  52. if (Application.isPlaying)
  53. ApplyLayout();
  54. return;
  55. }
  56. #endif
  57. ApplyLayout();
  58. }
  59. public void ApplyLayout()
  60. {
  61. if (layoutGroup == null)
  62. {
  63. // Debug.LogError("[ApplyLayout] ❌ layoutGroup 为空,无法应用布局!");
  64. return;
  65. }
  66. UILayoutData layout = GetCurrentLayout();
  67. // Debug.Log($"[ApplyLayout] 📌 选中的布局数据: {layout}");
  68. // ✅ 设置 VerticalLayoutGroup 的布局参数
  69. layoutGroup.padding.left = layout.paddingLeft;
  70. layoutGroup.padding.right = layout.paddingRight;
  71. layoutGroup.padding.top = layout.paddingTop;
  72. layoutGroup.padding.bottom = layout.paddingBottom;
  73. layoutGroup.spacing = layout.spacing;
  74. layoutGroup.childAlignment = layout.childAlignment;
  75. layoutGroup.childControlWidth = layout.controlChildWidth;
  76. layoutGroup.childControlHeight = layout.controlChildHeight;
  77. layoutGroup.childScaleWidth = layout.useChildScaleWidth;
  78. layoutGroup.childScaleHeight = layout.useChildScaleHeight;
  79. layoutGroup.childForceExpandWidth = layout.childForceExpandWidth;
  80. layoutGroup.childForceExpandHeight = layout.childForceExpandHeight;
  81. // Debug.Log($"[ApplyLayout] 🔄 更新 layoutGroup: " +
  82. // $"Padding=({layoutGroup.padding.left}, {layoutGroup.padding.right}, {layoutGroup.padding.top}, {layoutGroup.padding.bottom}), " +
  83. // $"Spacing={layoutGroup.spacing}, Alignment={layoutGroup.childAlignment}, " +
  84. // $"Control(W,H)=({layoutGroup.childControlWidth}, {layoutGroup.childControlHeight}), " +
  85. // $"Scale(W,H)=({layoutGroup.childScaleWidth}, {layoutGroup.childScaleHeight}), " +
  86. // $"Expand(W,H)=({layoutGroup.childForceExpandWidth}, {layoutGroup.childForceExpandHeight})");
  87. // ✅ 强制更新布局
  88. LayoutRebuilder.ForceRebuildLayoutImmediate(layoutGroup.GetComponent<RectTransform>());
  89. Canvas.ForceUpdateCanvases();
  90. // Debug.Log("[ApplyLayout] ✅ 布局已更新!");
  91. }
  92. private UILayoutData GetCurrentLayout()
  93. {
  94. DeviceTypeHelper.DeviceType deviceType = DeviceTypeHelper.DetectDeviceType();
  95. switch (deviceType)
  96. {
  97. case DeviceTypeHelper.DeviceType.iPhone:
  98. // Debug.Log("[GetCurrentLayout] 📱 当前设备: iPhone,使用 iPhone 布局");
  99. return iPhoneLayout;
  100. case DeviceTypeHelper.DeviceType.iPad:
  101. // Debug.Log("[GetCurrentLayout] 📲 当前设备: iPad,使用 iPad 布局");
  102. return iPadLayout;
  103. default:
  104. // Debug.Log("[GetCurrentLayout] 💻 未知设备,使用 Default 布局");
  105. return defaultLayout;
  106. }
  107. }
  108. // [ContextMenu("手动应用布局")]
  109. // public void DebugApplyLayout()
  110. // {
  111. // ApplyLayout();
  112. // }
  113. }
  114. }