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