Unity利用UGUI绘制多边形实现属性图

游戏开发中,角色具备有多种属性,一般呈现成一个如下图的多边形属性图。

一、效果图

Unity利用UGUI绘制多边形实现属性图_第1张图片

二、展示效果

可以调整边数,调整每个点的数值。Unity利用UGUI绘制多边形实现属性图_第2张图片

三、实现原理

查看UGUI的代码可以得知,绘制ui图的方法其实是绘制mesh,然后对其进行填充。正常一个Image图片,绘制有四个顶点。

为了实现多边形,继承于MaskableGraphic,重写OnPopulateMesh,重新绘制顶点。

四、放上代码

//多边形绘制
using UnityEngine;
using UnityEngine.UI;

public class MyUIPolygon : MaskableGraphic
{
    public bool fill = true;
    public float thickness = 5;
    [Range(3, 360)]
    public int sides = 3;
    [Range(0, 360)]
    public float rotation = 0;
    [Range(0, 1)]
    public float[] VerticesDistances = new float[3];
    private float size = 0;

    public void DrawPolygon(int _sides)
    {
        sides = _sides;
        VerticesDistances = new float[_sides + 1];
        for (int i = 0; i < _sides; i++) VerticesDistances[i] = 1; ;
        rotation = 0;
    }
    public void DrawPolygon(int _sides, float[] _VerticesDistances)
    {
        sides = _sides;
        VerticesDistances = _VerticesDistances;
        rotation = 0;
    }
    public void DrawPolygon(int _sides, float[] _VerticesDistances, float _rotation)
    {
        sides = _sides;
        VerticesDistances = _VerticesDistances;
        rotation = _rotation;
    }
    void Update()
    {
        size = rectTransform.rect.width;
        if (rectTransform.rect.width > rectTransform.rect.height)
            size = rectTransform.rect.height;
        else
            size = rectTransform.rect.width;
        thickness = (float)Mathf.Clamp(thickness, 0, size / 2);
    }

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();

        Vector2 prevX = Vector2.zero;
        Vector2 prevY = Vector2.zero;
        Vector2 uv0 = new Vector2(0, 0);
        Vector2 uv1 = new Vector2(0, 1);
        Vector2 uv2 = new Vector2(1, 1);
        Vector2 uv3 = new Vector2(1, 0);
        Vector2 pos0;
        Vector2 pos1;
        Vector2 pos2;
        Vector2 pos3;
        float degrees = 360f / sides;
        int vertices = sides + 1;
        if (VerticesDistances.Length != vertices)
        {
            VerticesDistances = new float[vertices];
            for (int i = 0; i < vertices - 1; i++) VerticesDistances[i] = 1;
        }
        // last vertex is also the first!
        VerticesDistances[vertices - 1] = VerticesDistances[0];
        for (int i = 0; i < vertices; i++)
        {
            float outer = -rectTransform.pivot.x * size * VerticesDistances[i];
            float inner = -rectTransform.pivot.x * size * VerticesDistances[i] + thickness;
            float rad = Mathf.Deg2Rad * (i * degrees + rotation);
            float c = Mathf.Cos(rad);
            float s = Mathf.Sin(rad);
            uv0 = new Vector2(0, 1);
            uv1 = new Vector2(1, 1);
            uv2 = new Vector2(1, 0);
            uv3 = new Vector2(0, 0);
            pos0 = prevX;
            pos1 = new Vector2(outer * c, outer * s);
            if (fill)
            {
                pos2 = Vector2.zero;
                pos3 = Vector2.zero;
            }
            else
            {
                pos2 = new Vector2(inner * c, inner * s);
                pos3 = prevY;
            }
            prevX = pos1;
            prevY = pos2;
            SetVbo(new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 });
            vh.AddUIVertexQuad(vbo);
        }
    }
    private UIVertex[] vbo = new UIVertex[4];
    private void SetVbo(Vector2[] vertices, Vector2[] uvs)
    {
        for (int i = 0; i < vertices.Length; i++)
        {
            var vert = UIVertex.simpleVert;
            vert.color = color;
            vert.position = vertices[i];
            vert.uv0 = uvs[i];
            vbo[i] = vert;
        }
    }
}

代码来自UnityUIExtensions,做了一些修改,不依赖其他扩展基类脚本,里面还有其他比较实用的UI扩展代码,以后会逐一介绍。

你可能感兴趣的:(Unity,UGUI)