Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Cut corners primative

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by Freezy, Oct 6, 2015.

?

Quick feedback

  1. Thanks

    0 vote(s)
    0.0%
  2. The point?

    0 vote(s)
    0.0%
  3. I used bits and pieces

    0 vote(s)
    0.0%
  1. Freezy

    Freezy

    Joined:
    Jul 15, 2012
    Posts:
    232
    Here is a script that will take a rectangular TransformRect and cut off some corners based on the corner size.
    This is great for when you need a quick and easy non-square panel/image.

    You can use these as panels, elements, toggles, buttons etc.
    Optionally you can set the top and bottom bar colors.

    Enjoy!
    It adds an additional square if the relevant side has a corner cut, it then offsets the ends to simulate a cut corner.
    The parameters can be set to whatever you like, ending up with ''Not a bug, but a feature" style meshes.

    UVs are being set, but might be skewed when a texture is applied.



    Code (CSharp):
    1.  
    2. /// <summary>
    3. /// Created by Freezy - ElicitIce.nl
    4. /// Posted on Unity Forums http://forum.unity3d.com/threads/cut-corners-primative.359494/
    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. ///
    10. /// Here is a script that will take a rectangular TransformRect and cut off some corners based on the corner size.
    11. /// This is great for when you need a quick and easy non-square panel/image.
    12. /// Enjoy!
    13. /// It adds an additional square if the relevant side has a corner cut, it then offsets the ends to simulate a cut corner.
    14. /// UVs are being set, but might be skewed when a texture is applied.
    15. /// You could hide the additional colors by using the following:
    16. /// http://rumorgames.com/hide-in-inspector/
    17. ///
    18. /// </summary>
    19.  
    20. namespace UnityEngine.UI.Extensions {
    21.     [ExecuteInEditMode]
    22.     [AddComponentMenu("UI/Extensions/Primitives/Cut Corners")]
    23.     public class UICornerCut : MaskableGraphic {
    24.  
    25.         public Vector2 cornerSize = new Vector2(16, 16);
    26.  
    27.         [Header("Corners to cut")]
    28.         public bool cutUL = true;
    29.         public bool cutUR;
    30.         public bool cutLL;
    31.         public bool cutLR;
    32.  
    33.         [Tooltip("Up-Down colors become Left-Right colors")]
    34.         public bool makeColumns = false;
    35.  
    36.         [Header("Color the cut bars differently")]
    37.         public bool useColorUp;
    38. //        [HideUnless("useColorUp")]
    39.         public Color32 colorUp = Color.blue;
    40.  
    41.         public bool useColorDown;
    42. //        [HideUnless("useColorDown")]
    43.         public Color32 colorDown = Color.green;
    44.  
    45.         protected override void OnPopulateMesh(Mesh m) {
    46.             var rect = rectTransform.rect;
    47.             var rectNew = rect;
    48.  
    49.             Color32 color32 = color;
    50.             using (var vh = new VertexHelper()) {
    51.                 bool up = cutUL | cutUR;
    52.                 bool down = cutLL | cutLR;
    53.                 bool left = cutLL | cutUL;
    54.                 bool right = cutLR | cutUR;
    55.                 bool any = up | down;
    56.  
    57.                 if (any && cornerSize.sqrMagnitude > 0) {
    58.                     //nibble off the sides
    59.  
    60.                     if (left)
    61.                         rectNew.xMin += cornerSize.x;
    62.                     if (down)
    63.                         rectNew.yMin += cornerSize.y;
    64.                     if (up)
    65.                         rectNew.yMax -= cornerSize.y;
    66.                     if (right)
    67.                         rectNew.xMax -= cornerSize.x;
    68.  
    69.                     //add two squares to the main square
    70.                     Vector2 ul, ur, ll, lr;
    71.  
    72.                     if (makeColumns) {
    73.                         ul = new Vector2(rect.xMin, cutUL ? rectNew.yMax : rect.yMax);
    74.                         ur = new Vector2(rect.xMax, cutUR ? rectNew.yMax : rect.yMax);
    75.                         ll = new Vector2(rect.xMin, cutLL ? rectNew.yMin : rect.yMin);
    76.                         lr = new Vector2(rect.xMax, cutLR ? rectNew.yMin : rect.yMin);
    77.  
    78.                         if (left)
    79.                             AddSquare(
    80.                                 ll, ul,
    81.                                 new Vector2(rectNew.xMin, rect.yMax),
    82.                                 new Vector2(rectNew.xMin, rect.yMin),
    83.                                 rect, useColorUp ? colorUp : color32, vh);
    84.                         if (right)
    85.                             AddSquare(
    86.                                 ur, lr,
    87.                                 new Vector2(rectNew.xMax, rect.yMin),
    88.                                 new Vector2(rectNew.xMax, rect.yMax),
    89.                                 rect, useColorDown ? colorDown : color32, vh);
    90.                     } else {
    91.                         ul = new Vector2(cutUL ? rectNew.xMin : rect.xMin, rect.yMax);
    92.                         ur = new Vector2(cutUR ? rectNew.xMax : rect.xMax, rect.yMax);
    93.                         ll = new Vector2(cutLL ? rectNew.xMin : rect.xMin, rect.yMin);
    94.                         lr = new Vector2(cutLR ? rectNew.xMax : rect.xMax, rect.yMin);
    95.                         if (down)
    96.                             AddSquare(
    97.                                 lr, ll,
    98.                                 new Vector2(rect.xMin, rectNew.yMin),
    99.                                 new Vector2(rect.xMax, rectNew.yMin),
    100.                                 rect, useColorDown ? colorDown : color32, vh);
    101.                         if (up)
    102.                             AddSquare(
    103.                                 ul, ur,
    104.                                 new Vector2(rect.xMax, rectNew.yMax),
    105.                                 new Vector2(rect.xMin, rectNew.yMax),
    106.                                 rect, useColorUp ? colorUp : color32, vh);
    107.                     }
    108.                 }
    109.  
    110.                 //center
    111.                 if (makeColumns)
    112.                     AddSquare(new Rect(rectNew.xMin, rect.yMin, rectNew.width, rect.height), rect, color32, vh);
    113.                 else
    114.                     AddSquare(new Rect(rect.xMin, rectNew.yMin, rect.width, rectNew.height), rect, color32, vh);
    115.  
    116.                 vh.FillMesh(m);
    117.             }
    118.         }
    119.  
    120.         private static void AddSquare(Rect rect, Rect rectUV, Color32 color32, VertexHelper vh) {
    121.             int v0 = AddVert(rect.xMin, rect.yMin, rectUV, color32, vh);
    122.             int v1 = AddVert(rect.xMin, rect.yMax, rectUV, color32, vh);
    123.             int v2 = AddVert(rect.xMax, rect.yMax, rectUV, color32, vh);
    124.             int v3 = AddVert(rect.xMax, rect.yMin, rectUV, color32, vh);
    125.  
    126.             vh.AddTriangle(v0, v1, v2);
    127.             vh.AddTriangle(v2, v3, v0);
    128.         }
    129.  
    130.         private static void AddSquare(Vector2 a, Vector2 b, Vector2 c, Vector2 d, Rect rectUV, Color32 color32, VertexHelper vh) {
    131.             int v0 = AddVert(a.x, a.y, rectUV, color32, vh);
    132.             int v1 = AddVert(b.x, b.y, rectUV, color32, vh);
    133.             int v2 = AddVert(c.x, c.y, rectUV, color32, vh);
    134.             int v3 = AddVert(d.x, d.y, rectUV, color32, vh);
    135.  
    136.             vh.AddTriangle(v0, v1, v2);
    137.             vh.AddTriangle(v2, v3, v0);
    138.         }
    139.  
    140.         /// <summary>
    141.         /// Auto UV handler within the assigned area
    142.         /// </summary>
    143.         /// <param name="x"></param>
    144.         /// <param name="y"></param>
    145.         /// <param name="area"></param>
    146.         /// <param name="color32"></param>
    147.         /// <param name="vh"></param>
    148.         private static int AddVert(float x, float y, Rect area, Color32 color32, VertexHelper vh) {
    149.             var uv = new Vector2(
    150.                 Mathf.InverseLerp(area.xMin, area.xMax, x),
    151.                 Mathf.InverseLerp(area.yMin, area.yMax, y)
    152.             );
    153.             vh.AddVert(new Vector3(x, y), color32, uv);
    154.             return vh.currentVertCount - 1;
    155.         }
    156.     }
    157. }
    158.  
     
    Last edited: Oct 7, 2015
    MaxEden and Senshi like this.