1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
using UnityEngine.UI; using UnityEngine;
[RequireComponent(typeof(CanvasRenderer))] public class RadarChart : MaskableGraphic { [SerializeField] private bool showOutline; [SerializeField] private Color outlineColor; [SerializeField] private float outlineWidth; [SerializeField] private bool showInline; [SerializeField] private Color inlineColor; [SerializeField] private float inlineWidth; [SerializeField] private bool showCenterPoint; [SerializeField] private Color centerPointColor; [SerializeField] private float centerPointRadius; [SerializeField] private RadarData[] dataArray;
[System.Serializable] public class RadarData { [Range(0f, 1f)] public float value = 1f; public float angle; public float maxLength = 40f; }
protected override void OnPopulateMesh(VertexHelper vh) { vh.Clear(); if (dataArray == null || dataArray.Length < 3) { return; }
var center = Vector2.zero; var vertices = new Vector2[dataArray.Length]; for (var i = 0; i < dataArray.Length; i++) { var radian = dataArray[i].angle * Mathf.Deg2Rad; var distance = Mathf.Lerp(0, dataArray[i].maxLength, dataArray[i].value); vertices[i] = new Vector2(Mathf.Cos(radian) * distance, Mathf.Sin(radian) * distance); }
for (var i = 0; i < dataArray.Length; i++) { var nextIndex = (i + 1) % dataArray.Length; AddTriangle(vh, center, vertices[i], vertices[nextIndex], color); }
if (showOutline) { DrawOuterOutline(vh, vertices, outlineColor, outlineWidth); }
if (showInline) { for (var i = 0; i < dataArray.Length; i++) { DrawLine(vh, center, vertices[i], inlineColor, inlineWidth); } }
if (showCenterPoint) { DrawCenterPoint(vh, center, centerPointColor, centerPointRadius); } }
public void UpdateRadarData(RadarData[] newData) { if (newData == null || newData.Length < 3) { Debug.LogError("非法数据"); return; }
dataArray = newData; SetVerticesDirty(); }
public void UpdateRadarData(int index, float value) { if (index >= dataArray.Length) { Debug.LogError("越界"); return; }
var data = dataArray[index]; data.value = value; SetVerticesDirty(); }
private static void AddTriangle(VertexHelper vh, Vector2 v0, Vector2 v1, Vector2 v2, Color color) { var vert0 = UIVertex.simpleVert; var vert1 = UIVertex.simpleVert; var vert2 = UIVertex.simpleVert; vert0.color = color; vert1.color = color; vert2.color = color; vert0.position = v0; vert1.position = v1; vert2.position = v2; vh.AddVert(vert0); vh.AddVert(vert1); vh.AddVert(vert2);
var index = vh.currentVertCount; vh.AddTriangle(index - 3, index - 2, index - 1); }
private static void DrawOuterOutline(VertexHelper vh, Vector2[] vertices, Color colorT, float width) { for (var i = 0; i < vertices.Length; i++) { var nextIndex = (i + 1) % vertices.Length; DrawLine(vh, vertices[i], vertices[nextIndex], colorT, width); } }
private static void DrawLine(VertexHelper vh, Vector2 start, Vector2 end, Color colorT, float width) { var direction = (end - start).normalized; var perpendicular = new Vector2(-direction.y, direction.x) * width * 0.5f;
var v0 = start - perpendicular; var v1 = start + perpendicular; var v2 = end + perpendicular; var v3 = end - perpendicular; var vert0 = UIVertex.simpleVert; var vert1 = UIVertex.simpleVert; var vert2 = UIVertex.simpleVert; var vert3 = UIVertex.simpleVert; vert0.color = colorT; vert1.color = colorT; vert2.color = colorT; vert3.color = colorT; vert0.position = v0; vert1.position = v1; vert2.position = v2; vert3.position = v3; vh.AddVert(vert0); vh.AddVert(vert1); vh.AddVert(vert2); vh.AddVert(vert3);
var index = vh.currentVertCount; vh.AddTriangle(index - 4, index - 3, index - 2); vh.AddTriangle(index - 4, index - 2, index - 1); }
private static void DrawCenterPoint(VertexHelper vh, Vector2 center, Color color, float radius) { var prevPoint = center + new Vector2(radius, 0); for (var i = 1; i <= 20; i++) { var angle = i * 20 * Mathf.Deg2Rad; var nextPoint = center + new Vector2(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius); AddTriangle(vh, center, prevPoint, nextPoint, color); prevPoint = nextPoint; } } }
|