Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[Script] Curved Layout

Discussion in 'UGUI & TextMesh Pro' started by Freezy, May 12, 2016.

  1. Freezy

    Freezy

    Joined:
    Jul 15, 2012
    Posts:
    234
    Here is another quicky, to create curved menus.
    Add it like you would any other layout group.

    Then set the item size, curve offset and center point.



    Code (CSharp):
    1.  
    2. /// <summary>
    3. /// Curved Layout Group Created by Freezy - http://www.ElicitIce.com
    4. /// Posted on Unity Forums http://forum.unity3d.com/threads/script-curved-layout.403985/
    5. ///
    6. /// Free for any use and alteration, source code may not be sold without my permission.
    7. /// If you make improvements on this script please share them with the community.
    8. ///
    9. /// </summary>
    10.  
    11. namespace UnityEngine.UI.Extensions {
    12.     /// <summary>
    13.     /// TODO:
    14.     /// - add automatic child sizing, like in the HorizontalOrVerticalLayoutGroup.cs
    15.     /// - nicer anchor handling for initial child positions
    16.     /// </summary>
    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 usefull 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 pivot (why not a utility function switch statement?)
    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. }
    94.  
     
    Last edited: May 12, 2016
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Thanks for the great addition, will get that merged with the next update
     
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    A bit of help @Freezy
    Trying out the component and hitting a snag. If I use regular UI components they seem to draw fine. However if I use any primitive or custom controls, the control doesn't get drawn.

    Not a big issue for now, but it limits what child components can be used with it.
     
  4. Freezy

    Freezy

    Joined:
    Jul 15, 2012
    Posts:
    234
    It works fine with composite buttons, which was my intended use case.
    There is still a large issue with figuring out the proper sizing information, but I didn't need it so I skipped that part for now.

    I bet that the sizing information is what is causing other elements to be rendered incorrectly.
    It is open source, so maybe somewhere along the timeline I or somebody else will fix it :p
     
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
  6. happyJessica

    happyJessica

    Joined:
    Jan 4, 2022
    Posts:
    6
    A bit of help @Freezy

    I have a question about these two files..

    https://bitbucket.org/UnityUIExtens...elease/Runtime/Scripts/Layout/CurvedLayout.cs
    https://bitbucket.org/UnityUIExtens...ase/Runtime/Scripts/Primitives/UICornerCut.cs

    These two files have comments like:
    /// Free for any use and alteration, source code may not be sold without my permission.
    /// If you make improvements on this script please share them with the community.

    "unity-ui-extensions" is under BSD License, and "unity-ui-extensions" uses these two source code.
    So, is it possible to use "unity-ui-extensions" in commercial SW?
    you said, "source code may not be sold without my permission."
    In this case, when commercial SW wants to use "unity-ui-extensions", is any issue to use "unity-ui-extensions" which contains "CurvedLayout.cs", "UICornerCut.cs"