GridLayoutGroup3D.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace AutoLayout3D
  5. {
  6. public class GridLayoutGroup3D : LayoutGroup3D
  7. {
  8. public Vector3 cellSize = Vector3.one;
  9. public Vector3 spacing = Vector3.zero;
  10. public AxisOrder axisOrder = AxisOrder.XZY;
  11. public AxisAlignment childAlignmentX = AxisAlignment.Middle;
  12. public AxisAlignment childAlignmentY = AxisAlignment.Middle;
  13. public AxisAlignment childAlignmentZ = AxisAlignment.Middle;
  14. public Direction childDirectionX = Direction.LowerToUpper;
  15. public Direction childDirectionY = Direction.LowerToUpper;
  16. public Direction childDirectionZ = Direction.LowerToUpper;
  17. public Bool3 childControlSize = new Bool3();
  18. public Bool3 childForceExpand = new Bool3();
  19. public Constraint constraintX = new Constraint();
  20. public Constraint constraintY = new Constraint();
  21. public Constraint constraintZ = new Constraint();
  22. private void Check()
  23. {
  24. if (cellSize.x < 0.0f) cellSize.x = 0.0f;
  25. if (cellSize.y < 0.0f) cellSize.y = 0.0f;
  26. if (cellSize.z < 0.0f) cellSize.z = 0.0f;
  27. if (constraintX.constraintCount < 1) constraintX.constraintCount = 1;
  28. if (constraintY.constraintCount < 1) constraintY.constraintCount = 1;
  29. if (constraintZ.constraintCount < 1) constraintZ.constraintCount = 1;
  30. }
  31. public override void UpdateLayout()
  32. {
  33. Check();
  34. elementList.Clear();
  35. foreach (Transform child in transform)
  36. {
  37. LayoutElement3D element = child.GetComponent<LayoutElement3D>();
  38. if (!child.gameObject.activeSelf || element == null) continue;
  39. elementList.Add(element);
  40. }
  41. Vector3 c_size = new Vector3();
  42. c_size.x = size.x - padding.lower.x - padding.upper.x;
  43. c_size.y = size.y - padding.lower.y - padding.upper.y;
  44. c_size.z = size.z - padding.lower.z - padding.upper.z;
  45. Vector3 c_center = center;
  46. c_center.x += (padding.lower.x - padding.upper.x) * 0.5f;
  47. c_center.y += (padding.lower.y - padding.upper.y) * 0.5f;
  48. c_center.z += (padding.lower.z - padding.upper.z) * 0.5f;
  49. Int3 c_count_max = new Int3();
  50. c_count_max.x = constraintX.constraintType == ConstraintType.Flexible ?
  51. Mathf.FloorToInt((c_size.x + spacing.x) / (cellSize.x + spacing.x)) : constraintX.constraintCount;
  52. c_count_max.y = constraintY.constraintType == ConstraintType.Flexible ?
  53. Mathf.FloorToInt((c_size.y + spacing.y) / (cellSize.y + spacing.y)) : constraintY.constraintCount;
  54. c_count_max.z = constraintZ.constraintType == ConstraintType.Flexible ?
  55. Mathf.FloorToInt((c_size.z + spacing.z) / (cellSize.z + spacing.z)) : constraintZ.constraintCount;
  56. Int3 c_count = new Int3();
  57. int countUnit, countRow, countPlane;
  58. countUnit = elementList.Count;
  59. switch (axisOrder)
  60. {
  61. case AxisOrder.XYZ:
  62. countRow = Mathf.CeilToInt((float)elementList.Count / c_count_max.x);
  63. countPlane = Mathf.CeilToInt((float)elementList.Count / c_count_max.x / c_count_max.y);
  64. c_count.x = countUnit < c_count_max.x ? countUnit : c_count_max.x;
  65. c_count.y = countRow < c_count_max.y ? countRow : c_count_max.y;
  66. c_count.z = countPlane < c_count_max.z ? countPlane : c_count_max.z;
  67. constraintZ.constraintType = ConstraintType.Flexible;
  68. break;
  69. case AxisOrder.XZY:
  70. countRow = Mathf.CeilToInt((float)elementList.Count / c_count_max.x);
  71. countPlane = Mathf.CeilToInt((float)elementList.Count / c_count_max.x / c_count_max.z);
  72. c_count.x = countUnit < c_count_max.x ? countUnit : c_count_max.x;
  73. c_count.z = countRow < c_count_max.z ? countRow : c_count_max.z;
  74. c_count.y = countPlane < c_count_max.y ? countPlane : c_count_max.y;
  75. constraintY.constraintType = ConstraintType.Flexible;
  76. break;
  77. case AxisOrder.YXZ:
  78. countRow = Mathf.CeilToInt((float)elementList.Count / c_count_max.y);
  79. countPlane = Mathf.CeilToInt((float)elementList.Count / c_count_max.y / c_count_max.x);
  80. c_count.y = countUnit < c_count_max.y ? countUnit : c_count_max.y;
  81. c_count.x = countRow < c_count_max.x ? countRow : c_count_max.x;
  82. c_count.z = countPlane < c_count_max.z ? countPlane : c_count_max.z;
  83. constraintZ.constraintType = ConstraintType.Flexible;
  84. break;
  85. case AxisOrder.YZX:
  86. countRow = Mathf.CeilToInt((float)elementList.Count / c_count_max.y);
  87. countPlane = Mathf.CeilToInt((float)elementList.Count / c_count_max.y / c_count_max.z);
  88. c_count.y = countUnit < c_count_max.y ? countUnit : c_count_max.y;
  89. c_count.z = countRow < c_count_max.z ? countRow : c_count_max.z;
  90. c_count.x = countPlane < c_count_max.x ? countPlane : c_count_max.x;
  91. constraintX.constraintType = ConstraintType.Flexible;
  92. break;
  93. case AxisOrder.ZXY:
  94. countRow = Mathf.CeilToInt((float)elementList.Count / c_count_max.z);
  95. countPlane = Mathf.CeilToInt((float)elementList.Count / c_count_max.z / c_count_max.x);
  96. c_count.z = countUnit < c_count_max.z ? countUnit : c_count_max.z;
  97. c_count.x = countRow < c_count_max.x ? countRow : c_count_max.x;
  98. c_count.y = countPlane < c_count_max.y ? countPlane : c_count_max.y;
  99. constraintY.constraintType = ConstraintType.Flexible;
  100. break;
  101. case AxisOrder.ZYX:
  102. countRow = Mathf.CeilToInt((float)elementList.Count / c_count_max.z);
  103. countPlane = Mathf.CeilToInt((float)elementList.Count / c_count_max.z / c_count_max.y);
  104. c_count.z = countUnit < c_count_max.z ? countUnit : c_count_max.z;
  105. c_count.y = countRow < c_count_max.y ? countRow : c_count_max.y;
  106. c_count.x = countPlane < c_count_max.x ? countPlane : c_count_max.x;
  107. constraintX.constraintType = ConstraintType.Flexible;
  108. break;
  109. }
  110. Vector3 sum_withSpace = new Vector3();
  111. sum_withSpace.x = (cellSize.x + spacing.x) * c_count.x - spacing.x;
  112. sum_withSpace.y = (cellSize.y + spacing.y) * c_count.y - spacing.y;
  113. sum_withSpace.z = (cellSize.z + spacing.z) * c_count.z - spacing.z;
  114. Vector3 offset = Vector3.zero;
  115. Vector3 direction = Vector3.zero;
  116. direction.x = childDirectionX == Direction.LowerToUpper ? 1.0f : -1.0f;
  117. direction.y = childDirectionY == Direction.LowerToUpper ? 1.0f : -1.0f;
  118. direction.z = childDirectionZ == Direction.LowerToUpper ? 1.0f : -1.0f;
  119. switch (childAlignmentX)
  120. {
  121. case AxisAlignment.Lower:
  122. offset.x = -c_size.x * 0.5f;
  123. if (childDirectionX == Direction.UpperToLower) offset.x += sum_withSpace.x;
  124. break;
  125. case AxisAlignment.Middle:
  126. offset.x = -direction.x * sum_withSpace.x * 0.5f;
  127. break;
  128. case AxisAlignment.Upper:
  129. offset.x = c_size.x * 0.5f;
  130. if (childDirectionX == Direction.LowerToUpper) offset.x -= sum_withSpace.x;
  131. break;
  132. }
  133. switch (childAlignmentY)
  134. {
  135. case AxisAlignment.Lower:
  136. offset.y = -c_size.y * 0.5f;
  137. if (childDirectionY == Direction.UpperToLower) offset.y += sum_withSpace.y;
  138. break;
  139. case AxisAlignment.Middle:
  140. offset.y = -direction.y * sum_withSpace.y * 0.5f;
  141. break;
  142. case AxisAlignment.Upper:
  143. offset.y = c_size.y * 0.5f;
  144. if (childDirectionY == Direction.LowerToUpper) offset.y -= sum_withSpace.y;
  145. break;
  146. }
  147. switch (childAlignmentZ)
  148. {
  149. case AxisAlignment.Lower:
  150. offset.z = -c_size.z * 0.5f;
  151. if (childDirectionZ == Direction.UpperToLower) offset.z += sum_withSpace.z;
  152. break;
  153. case AxisAlignment.Middle:
  154. offset.z = -direction.z * sum_withSpace.z * 0.5f;
  155. break;
  156. case AxisAlignment.Upper:
  157. offset.z = c_size.z * 0.5f;
  158. if (childDirectionZ == Direction.LowerToUpper) offset.z -= sum_withSpace.z;
  159. break;
  160. }
  161. Vector3 scaleFactor = Vector3.zero;
  162. if (childForceExpand.x) scaleFactor.x = cellSize.x;
  163. if (childForceExpand.y) scaleFactor.y = cellSize.y;
  164. if (childForceExpand.z) scaleFactor.z = cellSize.z;
  165. Vector3 offset_curr = offset;
  166. Int3 counter = new Int3();
  167. foreach (LayoutElement3D element in elementList)
  168. {
  169. Vector3 scale = element.transform.localScale;
  170. if (childControlSize.x) scale.x = scaleFactor.x / element.size.x;
  171. if (childControlSize.y) scale.y = scaleFactor.y / element.size.y;
  172. if (childControlSize.z) scale.z = scaleFactor.z / element.size.z;
  173. element.transform.localScale = scale;
  174. Vector3 elementOffset = Vector3.one;
  175. elementOffset.x = cellSize.x * 0.5f * direction.x - element.center.x * scale.x;
  176. elementOffset.y = cellSize.y * 0.5f * direction.y - element.center.y * scale.y;
  177. elementOffset.z = cellSize.z * 0.5f * direction.z - element.center.z * scale.z;
  178. element.transform.localPosition = c_center + offset_curr + elementOffset;
  179. switch (axisOrder)
  180. {
  181. case AxisOrder.XYZ:
  182. counter.x++;
  183. if (counter.x >= c_count.x)
  184. {
  185. counter.x = 0;
  186. offset_curr.x = offset.x;
  187. counter.y++;
  188. if (counter.y >= c_count.y)
  189. {
  190. counter.y = 0;
  191. offset_curr.y = offset.y;
  192. counter.z++;
  193. offset_curr.z += (cellSize.z + spacing.z) * direction.z;
  194. }
  195. else
  196. {
  197. offset_curr.y += (cellSize.y + spacing.y) * direction.y;
  198. }
  199. }
  200. else
  201. {
  202. offset_curr.x += (cellSize.x + spacing.x) * direction.x;
  203. }
  204. break;
  205. case AxisOrder.XZY:
  206. counter.x++;
  207. if (counter.x >= c_count.x)
  208. {
  209. counter.x = 0;
  210. offset_curr.x = offset.x;
  211. counter.z++;
  212. if (counter.z >= c_count.z)
  213. {
  214. counter.z = 0;
  215. offset_curr.z = offset.z;
  216. counter.y++;
  217. offset_curr.y += (cellSize.y + spacing.y) * direction.y;
  218. }
  219. else
  220. {
  221. offset_curr.z += (cellSize.z + spacing.z) * direction.z;
  222. }
  223. }
  224. else
  225. {
  226. offset_curr.x += (cellSize.x + spacing.x) * direction.x;
  227. }
  228. break;
  229. case AxisOrder.YXZ:
  230. counter.y++;
  231. if (counter.y >= c_count.y)
  232. {
  233. counter.y = 0;
  234. offset_curr.y = offset.y;
  235. counter.x++;
  236. if (counter.x >= c_count.x)
  237. {
  238. counter.x = 0;
  239. offset_curr.x = offset.x;
  240. counter.z++;
  241. offset_curr.z += (cellSize.z + spacing.z) * direction.z;
  242. }
  243. else
  244. {
  245. offset_curr.x += (cellSize.x + spacing.x) * direction.x;
  246. }
  247. }
  248. else
  249. {
  250. offset_curr.y += (cellSize.y + spacing.y) * direction.y;
  251. }
  252. break;
  253. case AxisOrder.YZX:
  254. counter.y++;
  255. if (counter.y >= c_count.y)
  256. {
  257. counter.y = 0;
  258. offset_curr.y = offset.y;
  259. counter.z++;
  260. if (counter.z >= c_count.z)
  261. {
  262. counter.z = 0;
  263. offset_curr.z = offset.z;
  264. counter.x++;
  265. offset_curr.x += (cellSize.x + spacing.x) * direction.x;
  266. }
  267. else
  268. {
  269. offset_curr.z += (cellSize.z + spacing.z) * direction.z;
  270. }
  271. }
  272. else
  273. {
  274. offset_curr.y += (cellSize.y + spacing.y) * direction.y;
  275. }
  276. break;
  277. case AxisOrder.ZXY:
  278. counter.z++;
  279. if (counter.z >= c_count.z)
  280. {
  281. counter.z = 0;
  282. offset_curr.z = offset.z;
  283. counter.x++;
  284. if (counter.x >= c_count.x)
  285. {
  286. counter.x = 0;
  287. offset_curr.x = offset.x;
  288. counter.y++;
  289. offset_curr.y += (cellSize.y + spacing.y) * direction.y;
  290. }
  291. else
  292. {
  293. offset_curr.x += (cellSize.x + spacing.x) * direction.x;
  294. }
  295. }
  296. else
  297. {
  298. offset_curr.z += (cellSize.z + spacing.z) * direction.z;
  299. }
  300. break;
  301. case AxisOrder.ZYX:
  302. counter.z++;
  303. if (counter.z >= c_count.z)
  304. {
  305. counter.z = 0;
  306. offset_curr.z = offset.z;
  307. counter.y++;
  308. if (counter.y >= c_count.y)
  309. {
  310. counter.y = 0;
  311. offset_curr.y = offset.y;
  312. counter.x++;
  313. offset_curr.x += (cellSize.x + spacing.x) * direction.x;
  314. }
  315. else
  316. {
  317. offset_curr.y += (cellSize.y + spacing.y) * direction.y;
  318. }
  319. }
  320. else
  321. {
  322. offset_curr.z += (cellSize.z + spacing.z) * direction.z;
  323. }
  324. break;
  325. }
  326. }
  327. elementList.Clear();
  328. }
  329. }
  330. }