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);
}
}
}
}
雷达图案例——待完善
最新推荐文章于 2024-10-10 18:32:28 发布