Mesh Leo雷达图!

 

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

public class LeoMap : MaskableGraphic
{
public float[] arr = { 1, 1, 1, 1,1 };
public Texture texture;

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();
if (arr.Length >= 3)
{

//找圆心
vh.AddVert(Vector3.zero, Color.white, new Vector2(0.5f, 0.5f));
//获取弧度
float ang = 2 * Mathf.PI / arr.Length;
//获取图片的短边
Rect rect = rectTransform.rect;
//再获取合适的半径
float r = rect.width < rect.height ? rect.width / 2 : rect.height / 2;
//比例
float p = r / arr.Max();
for (int i = 0; i < arr.Length; i++)
{
float x = Mathf.Sin(ang * i) * (arr[i] < r ? arr[i] : r) * p;
float y = Mathf.Cos(ang * i) * (arr[i] < r ? arr[i] : r) * p;
float uvx = (r + x) / (2 * r);
float uvy = (r + y) / (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);
}

}
}
}
}

 

posted @ 2022-11-11 20:12  梦凡尘_mzz  阅读(40)  评论(0)    收藏  举报