通过代码实现有描边有背景的雷达图
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI ;
using System.Linq;
public class LDT5 : MaskableGraphic
{
public Texture sprite;
public Texture backsprite;
public Color col = Color.white;
public float[] arr=new float[] { 1,1,2,1,1};
Texture2D texture;
public int num = 5;
public override Texture mainTexture
{
get
{
if (texture == null)
{
if (sprite == null)
{
if (material != null && material.mainTexture != null)
{
return material.mainTexture;
}
}
return sprite;
}
return texture;
}
}
protected override void OnPopulateMesh(VertexHelper vh)
{
texture = new Texture2D(1024, 1024);
Rect[] rects = texture.PackTextures(new Texture2D[] { backsprite as Texture2D, sprite as Texture2D }, 0);
vh.Clear();
Rect rect = rectTransform.rect;
float r = rect.width < rect.height ? rect.width / 2 : rect.height / 2;
float b = r / arr.Max();
vh.AddVert(new Vector3(rect.x, rect.y, 0), col, new Vector2(rects[0].x, rects[0].y));
vh.AddVert(new Vector3(rect.x, rect.y+rect.height, 0), col, new Vector2(rects[0].x, rects[0].y + rects[0].height));
vh.AddVert(new Vector3(rect.x+rect.width, rect.y+rect.height, 0), col, new Vector2(rects[0].x + rects[0].width, rects[0].y + rects[0].height));
vh.AddVert(new Vector3(rect.x+rect.width, rect.y, 0), col, new Vector2(rects[0].x + rects[0].width, rects[0].y));
vh.AddTriangle(0, 1, 2);
vh.AddTriangle(0, 2, 3);
vh.AddVert(Vector3.zero, color, new Vector2(0.5f * rects[1].width + rects[1].x, 0.5f * rects[1].height + rects[1].y));
float ang = 2 * Mathf.PI / arr.Length;
for (int i = 0; i < arr.Length; i++)
{
float x = Mathf.Sin(ang * i) * arr[i] * b;
float y = Mathf.Cos(ang * i) * arr[i] * b;
float uvx = (r + x) / (r * 2) * rects[1].x + rects[1].width;
float uvy = (r + y) / (r * 2) * rects[1].y + rects[1].height;
vh.AddVert(new Vector3(x, y, 0), color, new Vector2(uvx, uvy));
if (i == 0)
{
vh.AddTriangle(4, arr.Length + 4, 5);
}
else
{
vh.AddTriangle(4, i + 4, i + 5);
}
}
int over = 5 + num;
for (int i = 0; i < num; i++)
{
float x = Mathf.Sin(ang * i) * arr[i] * (b - 2);
float y = Mathf.Cos(ang * i) * arr[i] * (b - 2);
vh.AddVert(new Vector3(x, y, 0), Color.red, Vector2.zero);
float x1 = Mathf.Sin(ang * i) * arr[i] * (b + 2);
float y1 = Mathf.Cos(ang * i) * arr[i] * (b + 2);
vh.AddVert(new Vector3(x1, y1, 0), Color.red, Vector2.zero);
if (i == num - 1)
{
vh.AddTriangle(i * 2 + over, i * 2 + 1 + over, 1 + over);
vh.AddTriangle(i * 2 + over, 1 + over, over);
}
else
{
vh.AddTriangle(i * 2 + over, i * 2 + 1 + over, (i + 1) * 2 + 1 + over);
vh.AddTriangle(i * 2 + over, (i + 1) * 2 + 1 + over, (i + 1) * 2 + over);
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!