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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| using System.Collections.Generic; using SFramework.Mathematics; using UnityEngine; using UnityEngine.UI;
namespace XFramework.UI { public class LineChat : Graphic { [Header("坐标轴显示属性")] public float width = 200; public float height = 200; public float lineWidth = 5; public Vector2 offset = new Vector2(2, 2); public Color axleColor = Color.red;
[Header("坐标轴数据属性")] public Vector2 axleMaxValue = new Vector2(100, 100); public Vector2 axleMinValue = Vector3.zero; public List<Vector2[]> datas = new List<Vector2[]>();
[Space] [Header("刻度线")] public int scaleLineHeight = 5; public Vector2 subdivisions = new Vector2(10, 10);
[Space] [Header("数据")] public Color lineColor = Color.blue;
protected override void OnPopulateMesh(VertexHelper vh) { vh.Clear(); DrawAxle(vh); DrawLine(vh); }
private void DrawAxle(VertexHelper vh) { UIVertex[] verts = new UIVertex[4]; for (int i = 0; i < verts.Length; i++) { verts[i].color = axleColor; }
verts[0].position = offset; verts[1].position = new Vector2(lineWidth, 0) + offset; verts[2].position = new Vector2(lineWidth, height) + offset; verts[3].position = new Vector2(0, height) + offset; vh.AddUIVertexQuad(verts);
verts[0].position = offset; verts[1].position = new Vector2(width, 0) + offset; verts[2].position = new Vector2(width, lineWidth) + offset; verts[3].position = new Vector2(0, lineWidth) + offset; vh.AddUIVertexQuad(verts);
ScaleLine(); Arrow();
void ScaleLine() { Vector2 offset;
for (int i = 1; i < subdivisions.x; i++) { offset = this.offset + new Vector2(width / subdivisions.x * i, 0); verts[0].position = offset; verts[1].position = new Vector2(lineWidth, 0) + offset; verts[2].position = new Vector2(lineWidth, scaleLineHeight) + offset; verts[3].position = new Vector2(0, scaleLineHeight) + offset; vh.AddUIVertexQuad(verts); }
for (int i = 1; i < subdivisions.y; i++) { offset = this.offset + new Vector2(0, height / subdivisions.y * i); verts[0].position = offset; verts[1].position = new Vector2(scaleLineHeight, 0) + offset; verts[2].position = new Vector2(scaleLineHeight, lineWidth) + offset; verts[3].position = new Vector2(0, lineWidth) + offset; vh.AddUIVertexQuad(verts); } }
void Arrow() { Vector2 offset; float arrowLength = width / subdivisions.x / 2;
offset = this.offset + new Vector2(width, 0); verts[0].position = offset; verts[1].position = new Vector2(0, lineWidth) + offset; verts[2].position = new Vector2(-arrowLength, lineWidth + scaleLineHeight) + offset; verts[3].position = new Vector2(-arrowLength, scaleLineHeight) + offset; vh.AddUIVertexQuad(verts);
verts[0].position = new Vector2(0, lineWidth) + offset; verts[1].position = offset; verts[2].position = new Vector2(-arrowLength, -lineWidth - scaleLineHeight) + offset; verts[3].position = new Vector2(-arrowLength, -scaleLineHeight) + offset; vh.AddUIVertexQuad(verts);
offset = this.offset + new Vector2(0, height); verts[0].position = offset; verts[1].position = new Vector2(lineWidth, 0) + offset; verts[2].position = new Vector2(lineWidth + scaleLineHeight, -arrowLength) + offset; verts[3].position = new Vector2(scaleLineHeight, -arrowLength) + offset; vh.AddUIVertexQuad(verts);
verts[0].position = offset; verts[1].position = new Vector2(lineWidth, 0) + offset; verts[2].position = new Vector2(lineWidth - scaleLineHeight, -arrowLength) + offset; verts[3].position = new Vector2(-scaleLineHeight, -arrowLength) + offset; vh.AddUIVertexQuad(verts); } }
private void DrawLine(VertexHelper vh) { foreach (var data in datas) { Vector2[] pixelPoints = new Vector2[data.Length]; for (int i = 0; i < data.Length; i++) { pixelPoints[i].x = (data[i].x - axleMinValue.x) / axleMaxValue.x * width; pixelPoints[i].y = (data[i].y - axleMinValue.y) / axleMaxValue.y * height; pixelPoints[i] += offset; }
UIVertex[] verts = new UIVertex[4]; for (int i = 0; i < verts.Length; i++) { verts[i].color = lineColor; }
for (int i = 0; i < pixelPoints.Length - 1; i++) { SetVerts(pixelPoints[i], pixelPoints[i + 1], lineWidth, verts); vh.AddUIVertexQuad(verts); }
void SetVerts(Vector2 _start, Vector2 _end, float _width, UIVertex[] _verts) { Vector2[] tmp = PhysicsMath.GetRect(_start, _end, _width); _verts[0].position = tmp[0]; _verts[1].position = tmp[1]; _verts[2].position = tmp[3]; _verts[3].position = tmp[2]; } } }
public void AddDatas(Vector2[] datas) { this.datas.Add(datas); SetVerticesDirty(); }
public void Clear() { datas.Clear(); SetVerticesDirty(); }
public void Refresh() { SetAllDirty(); }
#if UNITY_EDITOR protected override void OnValidate() { SetVerticesDirty(); } #endif } }
|