CurvedLayout.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /// <summary>
  2. /// Curved Layout Group Created by Freezy - http://www.ElicitIce.com
  3. /// Posted on Unity Forums http://forum.unity3d.com/threads/curved-layout.403985/
  4. ///
  5. /// Free for any use and alteration, source code may not be sold without my permission.
  6. /// If you make improvements on this script please share them with the community.
  7. ///
  8. /// </summary>
  9. namespace UnityEngine.UI.Extensions
  10. {
  11. [AddComponentMenu("Layout/Extensions/Curved Layout")]
  12. public class CurvedLayout : LayoutGroup {
  13. public Vector3 CurveOffset;
  14. // Yes these two could be combined into a single vector
  15. // but this makes it easier to use?
  16. [Tooltip("axis along which to place the items, Normalized before use")]
  17. public Vector3 itemAxis;
  18. [Tooltip("size of each item along the Normalized axis")]
  19. public float itemSize;
  20. // 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
  21. public float centerpoint = 0.5f;
  22. protected override void OnEnable() { base.OnEnable(); CalculateRadial(); }
  23. public override void SetLayoutHorizontal() {
  24. }
  25. public override void SetLayoutVertical() {
  26. }
  27. public override void CalculateLayoutInputVertical() {
  28. CalculateRadial();
  29. }
  30. public override void CalculateLayoutInputHorizontal() {
  31. CalculateRadial();
  32. }
  33. #if UNITY_EDITOR
  34. protected override void OnValidate() {
  35. base.OnValidate();
  36. CalculateRadial();
  37. }
  38. #endif
  39. void CalculateRadial() {
  40. m_Tracker.Clear();
  41. if (transform.childCount == 0)
  42. return;
  43. //one liner for figuring out the desired pivot (should be moved into a utility function)
  44. Vector2 pivot = new Vector2(((int)childAlignment % 3) * 0.5f, ((int)childAlignment / 3) * 0.5f);
  45. //this seems to work ok-ish
  46. Vector3 lastPos = new Vector3(
  47. GetStartOffset(0, GetTotalPreferredSize(0)),
  48. GetStartOffset(1, GetTotalPreferredSize(1)),
  49. 0f
  50. );
  51. // 0 = first, 1 = last child
  52. float lerp = 0;
  53. //no need to catch divide by 0 as childCount > 0
  54. float step = 1f / transform.childCount;
  55. //normalize and create a distance between items
  56. var dist = itemAxis.normalized * itemSize;
  57. for (int i = 0; i < transform.childCount; i++) {
  58. RectTransform child = (RectTransform)transform.GetChild(i);
  59. if (child != null) {
  60. //stop the user from altering certain values in the editor
  61. m_Tracker.Add(this, child,
  62. DrivenTransformProperties.Anchors |
  63. DrivenTransformProperties.AnchoredPosition |
  64. DrivenTransformProperties.Pivot);
  65. Vector3 vPos = lastPos + dist;
  66. child.localPosition = lastPos = vPos + (lerp - centerpoint) * CurveOffset;
  67. child.pivot = pivot;
  68. //child anchors are not yet calculated, each child should set it's own size for now
  69. child.anchorMin = child.anchorMax = new Vector2(0.5f, 0.5f);
  70. lerp += step;
  71. }
  72. }
  73. }
  74. }
  75. }