雷达图绘制

五边形+材质

  • 填充网格

  • 定点助手 ,

  • 图片的sharder更改,赋上材质

 若脚本挂载到图片上,把图片的image组件去除

 

点击按钮变换雷达图形状

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

public class MeshImage : MaskableGraphic
{
    public float[] arr;
    //精灵
    public Sprite activeSprite;
    public float m = 1;
    //纹理
    public override Texture mainTexture  {
        get
        {
            return material.mainTexture;
        }
        }
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        
        if (arr.Length<3)
        {
            base.OnPopulateMesh(vh);
            return;

        }
        float w = rectTransform.sizeDelta.x;
        float h = rectTransform.sizeDelta.y;
        //数组最大值
        float max = arr.Max();
        float p = (w < h ? ((w / 2) / max) : ((h / 2) / max));
        vh.Clear();
        //每个角的弧度
        float angle = 2 * Mathf.PI / arr.Length;
        //圆心
        vh.AddVert(Vector3.zero, color,new  Vector2(0.5f,0.5f));

        for (int i = 0; i < arr.Length; i++)
        {
            float x = Mathf.Sin(i * angle) * arr[i];
            float y = Mathf.Cos(i * angle) * arr[i];
            float uvx = (x + w / 2) / w;//坐标
            float uvy = (y + h / 2) / h;//坐标
            vh.AddVert(new Vector3(x,y, 0), color, new Vector2(uvx, uvy));
            if (i==0)
            {
                vh.AddTriangle(0, arr.Length, 1);
            }
            else
            {
                vh.AddTriangle(0, i, i + 1);
            }
        }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Btn : MonoBehaviour
{
    public MeshImage MeshImage;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void OnBtn()
    {
        List<float>lis=new List<float>();
        for (int i = 0; i < Random.Range(3,9); i++)
        {
            lis.Add(Random.Range(0.1f, 100));
        }
        MeshImage.arr = lis.ToArray();
        MeshImage.SetVerticesDirty();
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

描边

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

public class MeshMiaaoBian : MaskableGraphic
{ /// <summary>
  /// 添加图片所需的源码(可在上述的源代码里查找到)
  /// </summary>
    public override Texture mainTexture
    {
        get
        {
            if (material != null && material.mainTexture != null)
            {
                return material.mainTexture;
            }
            return s_WhiteTexture;
        }
    }
    public float[] arr;
    public float m = 1;
    public Color mcolor = Color.red;
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        if (arr.Length < 3)///三个点才能形成图形,可以防止报错
        {
            base.OnPopulateMesh(vh);
            return;
        }


        vh.Clear();
        //每个角的弧度
        float ang = 2 * Mathf.PI / arr.Length;
        float w = rectTransform.sizeDelta.x;
        float h = rectTransform.sizeDelta.y;
        float max = arr.Max();
        float p = w < h ? ((w / 2) / max) : ((h / 2) / max);
        //先画圆心
        vh.AddVert(Vector3.zero, color, new Vector2(0.5f, 0.5f));//一个参数为顶点的坐标,第二个参数为颜色,第二个参数为图片的坐标
        for (int i = 0; i < arr.Length; i++)
        {
            float x = Mathf.Sin(i * ang) * arr[i] * p;
            float y = Mathf.Cos(i * ang) * arr[i] * p;
            float uvx = (x + w / 2) / w;
            float uvy = (y + h / 2) / h;
            vh.AddVert(new Vector3(x, y, 0), color, new Vector2(uvx, uvy));
            if (i == 0)
            {
                vh.AddTriangle(0, arr.Length, 1);
            }
            else
            {
                vh.AddTriangle(0, i, i + 1);
            }
        }

        //画描边
        int next = arr.Length + 1;
        for (int i = 0; i < arr.Length; i++)
        {
            float x0 = Mathf.Sin(i * ang) * (arr[i] * p - m);
            float y0 = Mathf.Cos(i * ang) * (arr[i] * p - m);
            float uvx0 = (x0 + w / 2) / w;
            float uvy0 = (y0 + h / 2) / h;
            vh.AddVert(new Vector3(x0, y0, 0), mcolor, new Vector2(uvx0, uvy0));


            float x = Mathf.Sin(i * ang) * (arr[i] * p);
            float y = Mathf.Cos(i * ang) * (arr[i] * p);
            float uvx = (x + w / 2) / w;
            float uvy = (y + h / 2) / h;
            vh.AddVert(new Vector3(x, y, 0), mcolor, new Vector2(uvx, uvy));
            if (i == arr.Length - 1)
            {
                vh.AddTriangle(i * 2 + next, i * 2 + 1 + next, 1 + next);
                vh.AddTriangle(i * 2 + next, 1 + next, next);
            }
            else
            {
                vh.AddTriangle(i * 2 + next, i * 2 + 1 + next, (i + 1) * 2 + 1 + next);
                vh.AddTriangle(i * 2 + next, (i + 1) * 2 + 1 + next, (i + 1) * 2 + next);
            }
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值