SimpleCircleLayoutGroup.cs 1012 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5. public class SimpleCircleLayoutGroup : UIBehaviour, ILayoutGroup
  6. {
  7. public float radius = 100;
  8. public float offsetAngle;
  9. #if UNITY_EDITOR
  10. protected override void OnValidate()
  11. {
  12. base.OnValidate();
  13. Arrange();
  14. }
  15. #endif
  16. // Callbacks called automatically when the number of elements changes
  17. #region ILayoutController implementation
  18. public void SetLayoutHorizontal() { }
  19. public void SetLayoutVertical()
  20. {
  21. Arrange();
  22. }
  23. #endregion
  24. void Arrange()
  25. {
  26. float splitAngle = 360 / transform.childCount;
  27. //var rect = transform as RectTransform;
  28. for (int elementId = 0; elementId < transform.childCount; elementId++)
  29. {
  30. var child = transform.GetChild(elementId) as RectTransform;
  31. float currentAngle = splitAngle * elementId + offsetAngle;
  32. child.anchoredPosition = new Vector2(
  33. Mathf.Cos(currentAngle * Mathf.Deg2Rad),
  34. Mathf.Sin(currentAngle * Mathf.Deg2Rad)) * radius;
  35. }
  36. }
  37. }