SimpleLayout.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace Fort23.Core
  4. {
  5. public enum LayoutType
  6. {
  7. Horizontal,
  8. Vertical
  9. }
  10. public class SimpleLayout
  11. {
  12. /// <summary>
  13. /// 根据传入的布局类型,排列 GameObject 列表,水平排列并居中
  14. /// </summary>
  15. /// <param name="list">需要排列的 GameObject 列表</param>
  16. /// <param name="type">布局类型,支持 Horizontal 或 Vertical</param>
  17. /// <param name="spacing">元素之间的间距</param>
  18. public static void Layout(List<object> list, LayoutType type, float spacing = 0f)
  19. {
  20. // 获取父节点的 RectTransform
  21. if (list.Count == 0)
  22. return;
  23. if (type == LayoutType.Horizontal)
  24. {
  25. List<GameObject> activeElements = new List<GameObject>();
  26. // 筛选 active 为 true 的元素
  27. foreach (object item in list)
  28. {
  29. GameObject g = item as GameObject;
  30. if (g != null && g.activeSelf)
  31. {
  32. activeElements.Add(g);
  33. }
  34. }
  35. int count = activeElements.Count;
  36. // 如果没有 active 的元素,直接返回
  37. if (count == 0) return;
  38. // 计算起始位置,使中心点的元素对齐父节点中心
  39. float totalWidth = 0f;
  40. for (int i = 0; i < count; i++)
  41. {
  42. RectTransform rt = activeElements[i].GetComponent<RectTransform>();
  43. totalWidth += rt.rect.width + (i < count - 1 ? spacing : 0);
  44. }
  45. // 起始位置的计算:最左侧元素的中心点相对于父节点
  46. float startX = -totalWidth / 2 + activeElements[0].GetComponent<RectTransform>().rect.width / 2;
  47. // 设置每个元素的位置
  48. for (int i = 0; i < count; i++)
  49. {
  50. RectTransform rt = activeElements[i].GetComponent<RectTransform>();
  51. // 更新位置
  52. rt.anchoredPosition = new Vector2(startX, 0);
  53. // 移动到下一个元素位置
  54. startX += rt.rect.width + spacing;
  55. }
  56. }
  57. else if (type == LayoutType.Vertical)
  58. {
  59. List<GameObject> activeElements = new List<GameObject>();
  60. // 筛选 active 为 true 的元素
  61. foreach (object item in list)
  62. {
  63. GameObject g = item as GameObject;
  64. if (g != null && g.activeSelf)
  65. {
  66. activeElements.Add(g);
  67. }
  68. }
  69. int count = activeElements.Count;
  70. // 如果没有 active 的元素,直接返回
  71. if (count == 0) return;
  72. // 计算起始位置,使中心点的元素对齐父节点中心
  73. float totalHeight = 0f;
  74. for (int i = 0; i < count; i++)
  75. {
  76. RectTransform rt = activeElements[i].GetComponent<RectTransform>();
  77. totalHeight += rt.rect.height + (i < count - 1 ? spacing : 0);
  78. }
  79. // 起始位置的计算:最顶部元素的中心点相对于父节点
  80. float startY = totalHeight / 2 - activeElements[0].GetComponent<RectTransform>().rect.height / 2;
  81. // 设置每个元素的位置
  82. for (int i = 0; i < count; i++)
  83. {
  84. RectTransform rt = activeElements[i].GetComponent<RectTransform>();
  85. // 更新位置
  86. rt.anchoredPosition = new Vector2(0, startY);
  87. // 移动到下一个元素位置
  88. startY -= rt.rect.height + spacing;
  89. }
  90. }
  91. }
  92. }
  93. }