Tanoda
CurvedLayout.cs
Go to the documentation of this file.
1
9
11{
17 [AddComponentMenu("Layout/Extensions/Curved Layout")]
18 public class CurvedLayout : LayoutGroup {
19 public Vector3 CurveOffset;
20
21 // Yes these two could be combined into a single vector
22 // but this makes it easier to use?
23 [Tooltip("axis along which to place the items, Normalized before use")]
24 public Vector3 itemAxis;
25 [Tooltip("size of each item along the Normalized axis")]
26 public float itemSize;
27
28 // the slope can be moved by altering this setting, it could be constrained to the 0-1 range, but other values are useful for animations
29 public float centerpoint = 0.5f;
30
31 protected override void OnEnable() { base.OnEnable(); CalculateRadial(); }
32 public override void SetLayoutHorizontal() {
33 }
34 public override void SetLayoutVertical() {
35 }
36 public override void CalculateLayoutInputVertical() {
37 CalculateRadial();
38 }
39 public override void CalculateLayoutInputHorizontal() {
40 CalculateRadial();
41 }
42#if UNITY_EDITOR
43 protected override void OnValidate() {
44 base.OnValidate();
45 CalculateRadial();
46 }
47#endif
48
49 void CalculateRadial() {
50 m_Tracker.Clear();
51 if (transform.childCount == 0)
52 return;
53
54 //one liner for figuring out the desired pivot (should be moved into a utility function)
55 Vector2 pivot = new Vector2(((int)childAlignment % 3) * 0.5f, ((int)childAlignment / 3) * 0.5f);
56
57 //this seems to work ok-ish
58 Vector3 lastPos = new Vector3(
59 GetStartOffset(0, GetTotalPreferredSize(0)),
60 GetStartOffset(1, GetTotalPreferredSize(1)),
61 0f
62 );
63
64 // 0 = first, 1 = last child
65 float lerp = 0;
66 //no need to catch divide by 0 as childCount > 0
67 float step = 1f / transform.childCount;
68
69 //normalize and create a distance between items
70 var dist = itemAxis.normalized * itemSize;
71
72 for (int i = 0; i < transform.childCount; i++) {
73 RectTransform child = (RectTransform)transform.GetChild(i);
74 if (child != null) {
75 //stop the user from altering certain values in the editor
76 m_Tracker.Add(this, child,
77 DrivenTransformProperties.Anchors |
78 DrivenTransformProperties.AnchoredPosition |
79 DrivenTransformProperties.Pivot);
80 Vector3 vPos = lastPos + dist;
81
82 child.localPosition = lastPos = vPos + (lerp - centerpoint) * CurveOffset;
83
84 child.pivot = pivot;
85 //child anchors are not yet calculated, each child should set it's own size for now
86 child.anchorMin = child.anchorMax = new Vector2(0.5f, 0.5f);
87 lerp += step;
88 }
89 }
90
91 }
92 }
93}
override void CalculateLayoutInputHorizontal()
Definition: CurvedLayout.cs:39
Credit Erdener Gonenc - @PixelEnvision.