using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class RenderMap : MaskableGraphic
{
public Button but;
public float[] arr;
public Texture tex;
public override Texture mainTexture
{
get
{
if(tex==null)
{
if(material!=null && material.mainTexture!=null)
{
return material.mainTexture;
}
}
return s_WhiteTexture;
}
}
protected override void Start()
{
but.onClick.AddListener(()=>{
for (int i = 0; i < arr.Length; i++)
{
int r = Random.Range(1, 10);
arr[i] = r;
SetAllDirty();
}
});
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
int num = arr.Length;
if(num>=3)
{
Rect rect = this.rectTransform.rect;
float r = rect.width < rect.height ? rect.width / 2 : rect.height / 2;
float p = r / arr.Max();
float ang = 2 * Mathf.PI / num;
vh.AddVert(Vector3.zero, color, Vector2.zero * 0.5f);
for (int i = 0; i < num; i++)
{
float x = Mathf.Sin(i * ang) * arr[i] * p;
float y = Mathf.Cos(i * ang) * arr[i] * p;
float vux = (x + r) / (r + r);
float vuy = (y + r) / (r + r);
vh.AddVert(new Vector3(x, y, 0), color, new Vector2(vux, vuy));
if(i==0)
{
vh.AddTriangle(0, num, 1);
}
else
{
vh.AddTriangle(0, i, i + 1);
}
}
int after = num + 1;
for (int i = 0; i < num; i++)
{
//描边外顶点
float x = Mathf.Sin(i * ang) * arr[i] * p;
float y = Mathf.Cos(i * ang) * arr[i] * p;
float vux = (x + r) / (r + r);
float vuy = (y + r) / (r + r);
vh.AddVert(new Vector3(x, y, 0), Color.red, new Vector2(vux, vuy));
//描边内顶点
float x0 = Mathf.Sin(i * ang) * (arr[i] * p-5);
float y0 = Mathf.Cos(i * ang) * (arr[i] * p-5);
float vux0 = (x + r) / (r + r);
float vuy0 = (y + r) / (r + r);
vh.AddVert(new Vector3(x0, y0, 0), Color.red, new Vector2(vux0, vuy0));
if(i==num-1)
{
vh.AddTriangle(i * 2 + after, i * 2 + 1 + after, 1 + after);
vh.AddTriangle(i * 2 + after, 1 + after, after);
}
else
{
vh.AddTriangle(i * 2 + after, i * 2 + 1 + after, (i+1)*2+1 + after);
vh.AddTriangle(i * 2 + after, (i + 1) * 2 + 1 + after, (i+1)*2 + after);
}
}
}
}
}
雷达图 带描边
于 2023-01-30 19:04:37 首次发布