2
0

RadialLayout.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /// Credit Danny Goodayle
  2. /// Sourced from - http://www.justapixel.co.uk/radial-layouts-nice-and-simple-in-unity3ds-ui-system/
  3. /// Updated by SimonDarksideJ - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect.
  4. /// Child Layout fix by John Hattan - enables an options
  5. /*
  6. Radial Layout Group by Just a Pixel (Danny Goodayle) - http://www.justapixel.co.uk
  7. Copyright (c) 2015
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. */
  24. namespace UnityEngine.UI.Extensions
  25. {
  26. [AddComponentMenu("Layout/Extensions/Radial Layout")]
  27. public class RadialLayout : LayoutGroup
  28. {
  29. public float fDistance;
  30. [Range(0f, 360f)]
  31. public float MinAngle, MaxAngle, StartAngle;
  32. public bool OnlyLayoutVisible = false;
  33. protected override void OnEnable() { base.OnEnable(); CalculateRadial(); }
  34. public override void SetLayoutHorizontal()
  35. {
  36. }
  37. public override void SetLayoutVertical()
  38. {
  39. }
  40. public override void CalculateLayoutInputVertical()
  41. {
  42. CalculateRadial();
  43. }
  44. public override void CalculateLayoutInputHorizontal()
  45. {
  46. CalculateRadial();
  47. }
  48. #if UNITY_EDITOR
  49. protected override void OnValidate()
  50. {
  51. base.OnValidate();
  52. CalculateRadial();
  53. }
  54. #endif
  55. void CalculateRadial()
  56. {
  57. m_Tracker.Clear();
  58. if (transform.childCount == 0)
  59. return;
  60. int ChildrenToFormat = 0;
  61. if (OnlyLayoutVisible)
  62. {
  63. for (int i = 0; i < transform.childCount; i++)
  64. {
  65. RectTransform child = (RectTransform)transform.GetChild(i);
  66. if ((child != null) && child.gameObject.activeSelf)
  67. ++ChildrenToFormat;
  68. }
  69. }
  70. else
  71. {
  72. ChildrenToFormat = transform.childCount;
  73. }
  74. float fOffsetAngle = (MaxAngle - MinAngle) / ChildrenToFormat;
  75. float fAngle = StartAngle;
  76. for (int i = 0; i < transform.childCount; i++)
  77. {
  78. RectTransform child = (RectTransform)transform.GetChild(i);
  79. if ((child != null) && (!OnlyLayoutVisible || child.gameObject.activeSelf))
  80. {
  81. //Adding the elements to the tracker stops the user from modifying their positions via the editor.
  82. m_Tracker.Add(this, child,
  83. DrivenTransformProperties.Anchors |
  84. DrivenTransformProperties.AnchoredPosition |
  85. DrivenTransformProperties.Pivot);
  86. Vector3 vPos = new Vector3(Mathf.Cos(fAngle * Mathf.Deg2Rad), Mathf.Sin(fAngle * Mathf.Deg2Rad), 0);
  87. child.localPosition = vPos * fDistance;
  88. //Force objects to be center aligned, this can be changed however I'd suggest you keep all of the objects with the same anchor points.
  89. child.anchorMin = child.anchorMax = child.pivot = new Vector2(0.5f, 0.5f);
  90. fAngle += fOffsetAngle;
  91. }
  92. }
  93. }
  94. }
  95. }