bazi

C#雷达图

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

public class RadarMap :MaskableGraphic
{
  public Texture2D sprite;
  public float[] arr;
  public float r;
  public override Texture mainTexture
  {  
    get
    {
      if (sprite!=null)
      {
        return sprite;
      }
      if (material != null && material.mainTexture!= null)
      {
        return material.mainTexture;
      }
      return s_WhiteTexture;
    }
  }
  //执行比awake快,编辑模式下能立马改变场景中的物体
  ·protected override void OnPopulateMesh(VertexHelper vh)
  {
    Rect rect = this.rectTransform.rect;
    int n = arr.Length;
    r = rect.width < rect.height ? rect.width / 2 : rect.height / 2;
    if (n >= 3)
    {
      vh.Clear();
      //圆心
      vh.AddVert(Vector3.zero, color, new Vector2(0.5f, 0.5f));
      //计算每个角的弧度
      float ang = 2 * Mathf.PI / n;
      //计算数组的最大值与半径的比值,可以不用
      float p = r/arr.Max();
      for (int i = 0; i < n; i++)
      {
        float x = Mathf.Sin(i * ang) * (arr[i] > r ? r : arr[i]);
        float y = Mathf.Cos(i * ang) * (arr[i] > r ? r : arr[i]);
        float uvx = (x + r) / (2 * r);
        float uvy = (y + r) / (2 * r);
        //添加顶点
        vh.AddVert(new Vector3(x, y, 0), color, new Vector2(uvx,uvy));
        if (i == 0)
        {
          //添加绘制
          vh.AddTriangle(0, n, 1);
        }
        else
        {
          vh.AddTriangle(0, i, i + 1);
        }
      }
    }
  }
}

 

posted on 2023-02-23 08:40  芭梓  阅读(137)  评论(0编辑  收藏  举报

导航