如图所示:
下面是脚本:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
//雷达图
public class Day2Test : MaskableGraphic
{
public Texture2D texture;
public override Texture mainTexture => texture;
[Range(0,5)]public float[] arr = { 5, 5, 5, 5, 5 };
protected override void OnPopulateMesh(VertexHelper vh)
{
base.OnPopulateMesh(vh);
vh.Clear();
Rect rect = rectTransform.rect;
float r = rect.width<rect.height ? rect.width/2 : rect.height/2;
float hu = 2 * Mathf.PI / arr.Length;
float max = arr.Max();
float bl = r / max;
vh.AddVert(Vector3.zero, color, new Vector2(0.5f, 0.5f));
for (int i = 0; i < arr.Length; i++)
{
float x = MathF.Sin(hu*i)*bl*arr[i];
float y = MathF.Cos(hu*i)*bl*arr[i];
float uvx = (x + r) / (2 * r);
float uvy = (y + r) / (2 * r);
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);
}
}
}
}