├── LICENSE ├── README.md └── UIPolygon.cs /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Ciacco Davide 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity-UI-Polygon 2 | Polygon renderer for the new Unity UI 3 | 4 | This is a graphic control for drawing polygons in the UI System. 5 | 6 | [UI > Extensions > Primitives > UI Polygon] 7 | 8 | I wrote this script as an addition to the extension project for the new Unity UI system mantained by Simon "ddreaper" Jackson (https://bitbucket.org/ddreaper/unity-ui-extensions) and because I needed to draw an hexagon in one of my games ui. 9 | 10 | I really recommend to try out the [Unity UI Extensions](https://bitbucket.org/ddreaper/unity-ui-extensions), but this script works also on its own: just import it in your Unity project. 11 | 12 | ## Usage 13 | 14 | Make sure that "UIPolygon.cs" in imported in your project folder. 15 | 16 | Add the Canvas: 17 | 18 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/tut_00_addCanvas.png) 19 | 20 | Create an empty object: 21 | 22 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/tut_01_createEmpty.png) 23 | 24 | Add the script to the empty object: 25 | 26 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/tut_02_addUIPoly.png) 27 | 28 | Edit the polygon properties as you like! 29 | 30 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/tut_03_edit.png) 31 | 32 | 33 | ## Edit your polygon 34 | 35 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/sides.gif) 36 | 37 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/deformation.gif) 38 | 39 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/rothikness.gif) 40 | 41 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/rotation.gif) 42 | 43 | Examples: 44 | 45 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/shapes.png) 46 | 47 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/screen0.png) 48 | 49 | > ![](http://ciaccodavide.altervista.org/storage/uipolygon/screen1.png) 50 | 51 | ## Need help? 52 | Contact me [here](http://ciaccodavi.de/about)! 53 | -------------------------------------------------------------------------------- /UIPolygon.cs: -------------------------------------------------------------------------------- 1 | /// Credit CiaccoDavide 2 | /// Sourced from - http://ciaccodavi.de/unity/uipolygon 3 | 4 | using System.Collections.Generic; 5 | 6 | namespace UnityEngine.UI.Extensions 7 | { 8 | [AddComponentMenu("UI/Extensions/Primitives/UI Polygon")] 9 | public class UIPolygon : MaskableGraphic 10 | { 11 | [SerializeField] 12 | Texture m_Texture; 13 | public bool fill = true; 14 | public float thickness = 5; 15 | [Range(3, 360)] 16 | public int sides = 3; 17 | [Range(0, 360)] 18 | public float rotation = 0; 19 | [Range(0, 1)] 20 | public float[] VerticesDistances = new float[3]; 21 | private float size = 0; 22 | 23 | public override Texture mainTexture 24 | { 25 | get 26 | { 27 | return m_Texture == null ? s_WhiteTexture : m_Texture; 28 | } 29 | } 30 | public Texture texture 31 | { 32 | get 33 | { 34 | return m_Texture; 35 | } 36 | set 37 | { 38 | if (m_Texture == value) return; 39 | m_Texture = value; 40 | SetVerticesDirty(); 41 | SetMaterialDirty(); 42 | } 43 | } 44 | public void DrawPolygon(int _sides) 45 | { 46 | sides = _sides; 47 | VerticesDistances = new float[_sides + 1]; 48 | for (int i = 0; i < _sides; i++) VerticesDistances[i] = 1; ; 49 | rotation = 0; 50 | } 51 | public void DrawPolygon(int _sides, float[] _VerticesDistances) 52 | { 53 | sides = _sides; 54 | VerticesDistances = _VerticesDistances; 55 | rotation = 0; 56 | } 57 | public void DrawPolygon(int _sides, float[] _VerticesDistances, float _rotation) 58 | { 59 | sides = _sides; 60 | VerticesDistances = _VerticesDistances; 61 | rotation = _rotation; 62 | } 63 | void Update() 64 | { 65 | size = rectTransform.rect.width; 66 | if (rectTransform.rect.width > rectTransform.rect.height) 67 | size = rectTransform.rect.height; 68 | else 69 | size = rectTransform.rect.width; 70 | thickness = (float)Mathf.Clamp(thickness, 0, size / 2); 71 | } 72 | protected UIVertex[] SetVbo(Vector2[] vertices, Vector2[] uvs) 73 | { 74 | UIVertex[] vbo = new UIVertex[4]; 75 | for (int i = 0; i < vertices.Length; i++) 76 | { 77 | var vert = UIVertex.simpleVert; 78 | vert.color = color; 79 | vert.position = vertices[i]; 80 | vert.uv0 = uvs[i]; 81 | vbo[i] = vert; 82 | } 83 | return vbo; 84 | } 85 | protected override void OnPopulateMesh(VertexHelper vh) 86 | { 87 | vh.Clear(); 88 | Vector2 prevX = Vector2.zero; 89 | Vector2 prevY = Vector2.zero; 90 | Vector2 uv0 = new Vector2(0, 0); 91 | Vector2 uv1 = new Vector2(0, 1); 92 | Vector2 uv2 = new Vector2(1, 1); 93 | Vector2 uv3 = new Vector2(1, 0); 94 | Vector2 pos0; 95 | Vector2 pos1; 96 | Vector2 pos2; 97 | Vector2 pos3; 98 | float degrees = 360f / sides; 99 | int vertices = sides + 1; 100 | if (VerticesDistances.Length != vertices) 101 | { 102 | VerticesDistances = new float[vertices]; 103 | for (int i = 0; i < vertices - 1; i++) VerticesDistances[i] = 1; 104 | } 105 | // last vertex is also the first! 106 | VerticesDistances[vertices - 1] = VerticesDistances[0]; 107 | for (int i = 0; i < vertices; i++) 108 | { 109 | float outer = -rectTransform.pivot.x * size * VerticesDistances[i]; 110 | float inner = -rectTransform.pivot.x * size * VerticesDistances[i] + thickness; 111 | float rad = Mathf.Deg2Rad * (i * degrees + rotation); 112 | float c = Mathf.Cos(rad); 113 | float s = Mathf.Sin(rad); 114 | uv0 = new Vector2(0, 1); 115 | uv1 = new Vector2(1, 1); 116 | uv2 = new Vector2(1, 0); 117 | uv3 = new Vector2(0, 0); 118 | pos0 = prevX; 119 | pos1 = new Vector2(outer * c, outer * s); 120 | if (fill) 121 | { 122 | pos2 = Vector2.zero; 123 | pos3 = Vector2.zero; 124 | } 125 | else 126 | { 127 | pos2 = new Vector2(inner * c, inner * s); 128 | pos3 = prevY; 129 | } 130 | prevX = pos1; 131 | prevY = pos2; 132 | vh.AddUIVertexQuad(SetVbo(new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 })); 133 | } 134 | } 135 | } 136 | } 137 | --------------------------------------------------------------------------------