雷达图案例——待完善

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class RadarMap : MaskableGraphic
{
    public Texture texture;
    public float[] nums;
    public float thick = 1;
    public Color sideColor;

    public override Texture mainTexture
    {
        get
        {
            if(texture==null)
            {
                if(material!=null&&material.mainTexture!=null)
                {
                    return material.mainTexture;
                }
                return s_WhiteTexture;
            }
            return texture;
        }
    }

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();
        Rect rect = GetPixelAdjustedRect();
        float ang = Mathf.PI * 2 / nums.Length;
        float r = rect.width < rect.height ? (rect.width / 2) : (rect.height / 2);
        float p = r / nums.Max();
        //添加圆心
        vh.AddVert(Vector3.zero, color, Vector4.zero);

        for (int i = 0; i < nums.Length; i++)
        {        
            //添加顶点
            float x = Mathf.Sin(i * ang) * nums[i] * p;
            float y = Mathf.Cos(i * ang) * nums[i] * p;

            float uvx = (x + r) / rect.width;
            float uvy = (y + r) / rect.height;
            vh.AddVert(new Vector3(x, y, 0), color, new Vector2(uvx,uvy));
            //绘制雷达图
            if (i == 0)
            {
                vh.AddTriangle(0,nums.Length, 1);
            }
            else
            {
                vh.AddTriangle(0, i, i + 1);
            }
        }

        //描边
        int n = nums.Length;
        for (int i = 0; i < nums.Length; i++)
        {
            float x = Mathf.Sin(i * ang) * nums[i] * p;
            float y = Mathf.Cos(i * ang) * nums[i] * p;
            vh.AddVert(new Vector3(x, y, 0), sideColor, Vector4.zero);

            float x1 = Mathf.Sin(i * ang) * (nums[i] * p-thick);
            float y1= Mathf.Cos(i * ang) * (nums[i] * p-thick);
            vh.AddVert(new Vector3(x1, y1, 0), sideColor, Vector4.zero);

            if(i==0)
            {
                vh.AddTriangle((nums.Length * 2 - 1) + n, 1 + n, 2 + n);
                vh.AddTriangle((nums.Length * 2 - 1) + n, 2 + n, nums.Length * 2 + n);
            }
            else
            {
                vh.AddTriangle(i * 2 - 1 + n, (i + 1) * 2 - 1 + n, (i + 1) * 2 + n);
                vh.AddTriangle(i * 2 - 1 + n, (i + 1) * 2 + n, i * 2 + n);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值