using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class RadarMap : MaskableGraphic
{
//public在外面设置的优先级高于里面设置的
public Color backGround;
public float[] arr;
public GameObject lab;
int space = 20;
//List<GameObject> labs = new List<GameObject>();
/// <summary>
/// 这个方法是用来添加雷达图周边的数字显示的
/// </summary>
protected override void Start()
{
base.Start();
//Rect rect = this.rectTransform.rect;
//float r = rect.width < rect.height ? rect.width / 2 : rect.height / 2;
//r += space;
//float ang = 2 * Mathf.PI / arr.Length;
//for (int i = 0; i < arr.Length; i++)
//{
// float x = Mathf.Sin(i * ang) * r;
// float y = Mathf.Cos(i * ang) * r;
// GameObject text = Instantiate(lab, transform);
// text.transform.localPosition = new Vector3(x, y, 0);
// text.GetComponent<Text>().text = arr[i].ToString();
// labs.Add(text);
//}
}
//public void OnSetText()
//{
// for (int i = 0; i < arr.Length; i++)
// {
// labs[i].GetComponent<Text>().text = arr[i].ToString();
// }
//}
protected override void OnPopulateMesh(VertexHelper vh)
{
int num = arr.Length;
vh.Clear();
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.cyan, new Vector2(0.5f, 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 uvx = (x + r) / (r + r);
float uvy = (y + r) / (r + r);
vh.AddVert(new Vector3(x, y, 0), Color.cyan, new Vector2(uvx, uvy));
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 uvx = (x + r) / (r + r);
float uvy = (y + r) / (r + r);
vh.AddVert(new Vector3(x, y, 0), backGround, new Vector2(uvx, uvy));
//描边外顶点
float x0 = Mathf.Sin(i * ang) * (arr[i] * p - 5);
float y0 = Mathf.Cos(i * ang) * (arr[i] * p - 5);
float uvx0 = (x + r) / (r + r);
float uvy0 = (y + r) / (r + r);
vh.AddVert(new Vector3(x0, y0, 0), backGround, new Vector2(uvx0, uvy0));
if (i == num - 1)
{
vh.AddTriangle(i * 2 + after, i * 2 + 1 + after, 1 + after);
vh.AddTriangle(i * 2 + after, 1 + after, 0 + 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);
}
}
}
}
// Update is called once per frame
void Update()
{
}
//当程序退出时调用
private void OnApplicationQuit()
{
//周边数字无法正常删除
//foreach (var item in labs)
//{
// Destroy(item.gameObject);
//}
}
}