雷达图(五边形战士,角色属性)

 

 

 

 

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

public class Xingzhuang : MaskableGraphic
{
public Button button;
public Text[] text;
public int[] arr;
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
int num = arr.Length;//遍数


if (num >= 3)//画圆必须大于或等于3条边
{
Rect rect = this.rectTransform.rect;//获得矩形
float r = rect.width < rect.height ? rect.width / 2 : rect.height / 2;//判断矩形是宽长还是高长 哪个短就用哪个以防超出矩形边界。
float ang = 2 * Mathf.PI / num;//求平均每个角的值
float p = r / arr.Max();//半径除最大值获得比例,主要是防止出圈

//圆的顶点
vh.AddVert(Vector3.zero, color, new Vector2(0.5f, 0.5f));//值1:画圆的位置,值2:颜色,值3:对应渲染的UV坐标

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 uvx = (x + r) / (r + r);
float uvy = (y + r) / (r + r);
vh.AddVert(new Vector3(x, y, 0), color, new Vector2(uvx, uvy));

if (i == 0)
{
vh.AddTriangle(0, num, 1);
}
else
{
vh.AddTriangle(0, i, i + 1);
}

}

}
//上面是制作多边形最少3个   //两个脚本 底图用上面的代码,内图全部复制即可
//----------------------------------------
//下面是点击按钮后随机数值改变形状
button.onClick.AddListener(() => {



for (int i = 0; i < 5; i++)
{
int j = Random.Range(1, 5);
arr[i] = j;
}
gameObject.GetComponent<Xingzhuang>().SetAllDirty();

});
}
private void Update()
{
Debug.Log(arr[0]);
text[0].text = ""+ arr[0];
text[1].text = ""+ arr[1];
text[2].text = ""+ arr[2];
text[3].text = ""+ arr[3];
text[4].text = ""+ arr[4];
}


}

posted @ 2023-01-06 11:10  天分丶张三  阅读(298)  评论(0编辑  收藏  举报