CanvasPositionManager.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CanvasPositionManager : MonoBehaviour
  5. {
  6. private float radius = 1f;//圆的半径
  7. private int numberOfObjects;//每行排列多少个物体
  8. private int theChildCount;//需要排列的物体的总个数
  9. private void Awake()
  10. {
  11. if (this.transform.name == "GGKFTherUIP")//这里可以忽略,是我自己的需求,根据不同场景中的物体名字决定一行排列多少个
  12. {
  13. numberOfObjects = 5;
  14. }
  15. else
  16. {
  17. numberOfObjects = 10;
  18. }
  19. theChildCount = this.transform.childCount;//物体总个数就是当前物体下的子物体的个数
  20. GerCurP(this.transform);//排列
  21. }
  22. private void Start()
  23. {
  24. }
  25. /// <summary>
  26. /// 半圆排列
  27. /// </summary>
  28. /// <param name="trans"></param>
  29. public void GerCurP(Transform trans)
  30. {
  31. if (theChildCount <= numberOfObjects)//如果总个数小于等于一行的个数,那只需要排列一行
  32. {
  33. print("个数不超过十个");
  34. for (int i = 0; i < trans.childCount; i++)
  35. {
  36. float angle = i * Mathf.PI / numberOfObjects;//根据每个物体(i)乘圆周率(Π)
  37. Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
  38. this.transform.GetChild(i).position = pos;
  39. }
  40. }
  41. else
  42. {
  43. print("个数!!!超过十个");
  44. int temp = trans.childCount / numberOfObjects;//行数(伪行数)
  45. int tempNumber;//记过下边的if else计算,得出真正所需的行数(真行数)
  46. float highUp = 0;
  47. if (temp % numberOfObjects == 0)
  48. {
  49. tempNumber = temp;
  50. }
  51. else//对10取余不为零,补一行
  52. {
  53. tempNumber = temp + 1;
  54. }
  55. Debug.Log("总共有几行" + tempNumber);
  56. //排列思路:(我的每个物体高度是200)第一行排在-200,然后每行依次+200,最后一行排在第一行下边也就是-400,这样开起来比较居中。因为排列太多行会看不清楚内容,所以一般五六行就够了,所以采用比较固(僵)定(硬)的排列方式,可以根据自己需求更改。
  57. for (int i = 0; i < tempNumber; i++)//循环几列
  58. {
  59. if (i == tempNumber - 1)//最后一行Y坐标需要排在第一行的下边(固定值,-400位置)
  60. {
  61. for (int j = (numberOfObjects * i); j < trans.childCount; j++)//最后一行的头到最终末尾
  62. {
  63. if (j >= (numberOfObjects * i) && j < trans.childCount)
  64. {
  65. float angle = (j - (numberOfObjects * i)) * Mathf.PI / numberOfObjects;//每行的每个点占圆周率的比例
  66. print(angle);
  67. Vector3 pos = new Vector3(-Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;//对angle取余弦和正弦值再乘以半径获得当前物体在的坐标
  68. this.transform.GetChild(j).position = new Vector3(pos.x, pos.y - 400, pos.z);//坐标赋值
  69. }
  70. }
  71. }
  72. else
  73. {
  74. for (int j = (numberOfObjects * i); j < numberOfObjects * (i + 1); j++)//每行的开头到当前行的末尾
  75. {
  76. if (j >= (numberOfObjects * i) && j < numberOfObjects * (i + 1))
  77. {
  78. float angle = (j - (numberOfObjects * i)) * Mathf.PI / numberOfObjects;//
  79. print(angle);
  80. Vector3 pos = new Vector3(-Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
  81. this.transform.GetChild(j).position = new Vector3(pos.x, pos.y + highUp - 200, pos.z);
  82. }
  83. }
  84. }
  85. highUp += 200;
  86. }
  87. }
  88. }
  89. }