ObjectGrid.cs 962 B

12345678910111213141516171819202122232425262728293031323334
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ObjectGrid : MonoBehaviour
  5. {
  6. public Vector2 Spacing;
  7. public int row;
  8. public int col;
  9. [ContextMenu("Range")]
  10. public void Range()
  11. {
  12. if (col == 0 || row == 0)
  13. {
  14. return;
  15. }
  16. Vector3[] poseList = new Vector3[row * col];
  17. Vector2 offset_origin = new Vector2(-0.5f * (col - 1) * Spacing.x, 0.5f * (row - 1) * Spacing.y);
  18. for (int i = 0; i < row; i++)
  19. {
  20. for (int j = 0; j < col; j++)
  21. {
  22. Vector2 offset = new Vector2(j * Spacing.x, -i * Spacing.y) + offset_origin;
  23. poseList[i * col + j] = Vector3.up * offset.y + Vector3.right * offset.x;
  24. }
  25. }
  26. for (int index = 0; index < transform.childCount; index++)
  27. {
  28. transform.GetChild(index).localPosition = poseList[index];
  29. }
  30. }
  31. }