简单记录

暂无例程

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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class UICurve : MaskableGraphic
{
public UICurveData GetCurveData(int id)
{
UICurveData curveData;
if (dCurveData.TryGetValue(id, out curveData))
{
return curveData;
}

return null;
}

public void ResetDraw()
{
SetAllDirty();
}

public void AddCurveData(int id, UICurveData curveData)
{
dCurveData.Add(id, curveData);
}

public void AddCurveData(UICurveData curveData)
{
dCurveData.Add(dCurveData.Count, curveData);
}

public void Clear()
{
dCurveData.Clear();
SetAllDirty();
}

public Dictionary<int, UICurveData> dCurveData = new Dictionary<int, UICurveData>();

// protected override void Start()
// {
// base.Start();
//
// var point1 = new UICurveData();
// point1.AddPos(0, 0);
// point1.AddPos(100, 100);
// var point2 = new UICurveData();
//
// point2.AddPos(1000, 1000);
// point2.AddPos(1000, 1100);
// dCurveData.Add(0, point1);
// dCurveData.Add(1, point2);
//
// ResetDraw();
// }

protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
if (dCurveData == null)
{
return;
}

foreach (var cd in dCurveData)
{
var lCurveData = cd.Value;
if (lCurveData.pos.Count < 2)
{
continue;
}

for (int i = 1; i < lCurveData.pos.Count; i++)
{
UIVertex[] verts = new UIVertex[4];

float x1 = lCurveData.pos[i - 1].x;
float y1 = lCurveData.pos[i - 1].y;
float x2 = lCurveData.pos[i].x;
float y2 = lCurveData.pos[i].y;
float xd = (y2 - y1) / Mathf.Sqrt(Mathf.Pow(x2 - x1, 2) + Mathf.Pow(y2 - y1, 2)) * cd.Value.thickness /
2;
float yd = (x2 - x1) / Mathf.Sqrt(Mathf.Pow(x2 - x1, 2) + Mathf.Pow(y2 - y1, 2)) * cd.Value.thickness /
2;

int idx = 0;
verts[idx].position = new Vector3(lCurveData.pos[i - 1].x - xd, lCurveData.pos[i - 1].y + yd);
verts[idx].color = lCurveData.color;
verts[idx].uv0 = Vector2.zero;

idx++;
verts[idx].position = new Vector3(lCurveData.pos[i].x - xd, lCurveData.pos[i].y + yd);
verts[idx].color = lCurveData.color;
verts[idx].uv0 = Vector2.zero;

idx++;
verts[idx].position = new Vector3(lCurveData.pos[i].x + xd, lCurveData.pos[i].y - yd);
verts[idx].color = lCurveData.color;
verts[idx].uv0 = Vector2.zero;

idx++;
verts[idx].position = new Vector3(lCurveData.pos[i - 1].x + xd, lCurveData.pos[i - 1].y - yd);
verts[idx].color = lCurveData.color;
verts[idx].uv0 = Vector2.zero;
vh.AddUIVertexQuad(verts);
}
}
}

internal void RemovePointIDs(params int[] remotePointIDs)
{
RemovePointIDs(remotePointIDs.ToList());
}

internal void RemovePointIDs(List<int> lRemotePointIDs)
{
foreach (var i in lRemotePointIDs)
{
if (!dCurveData.ContainsKey(i))
{
continue;
}

dCurveData.Remove(i);
}

SetAllDirty();
}
}

[Serializable]
public class UICurveData
{
public List<Vector2> pos = new List<Vector2>();
public Color color = Color.white;
public float thickness = 10;

public void AddPos(Vector2 v)
{
pos.Add(v);
}

public void AddPos(float x, float y)
{
AddPos(new Vector2(x, y));
}
}