-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnityOnGUIHelper.cs
213 lines (180 loc) · 7.83 KB
/
UnityOnGUIHelper.cs
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
using System.Collections.Generic;
using UnityEngine;
namespace asim.unity.helpers
{
/// <summary>
/// Wrapper/Helper class that helps to draw Simple Lines and Circles, Rect, in unity's OnGUI
/// Supports custom origin, and rotation , save and apply GUI Matrix
/// Will Add basic shapes
/// </summary>
public static class UnityOnGUIHelper
{
public static int Width => Screen.width;
public static int Height => Screen.height;
public static Vector2 Center => new Vector2(Screen.width / 2, Screen.height / 2);
static Vector2 OriginPos = new Vector2(0, 0);
static float OriginRotation = 0;
static Vector2 OriginScale = new Vector2(1, 1);
static Stack<Vector2> savedorigin = new Stack<Vector2>();
static Stack<float> savedrotation = new Stack<float>();
static Stack<Vector2> savedscale = new Stack<Vector2>();
/// <summary>
/// Save the current Origin to a stack
/// </summary>
public static void SaveOrigin()
{
savedorigin.Push(OriginPos);
savedrotation.Push(OriginRotation);
savedscale.Push(OriginScale);
}
/// <summary>
/// Load the Origin pop from the stack
/// </summary>
public static void LoadOrigin()
{
OriginPos = savedorigin.Pop();
OriginRotation = savedrotation.Pop();
OriginScale = savedscale.Pop();
}
/// <summary>
/// Reset the Origin to Default, 0 position, 0 rotation, 1 scale
/// </summary>
public static void ResetOrigin()
{
OriginPos = Vector2.zero;
OriginRotation = 0;
OriginScale = Vector2.one;
}
public static void SetOriginPosition(Vector2 newPos)
{
OriginPos = newPos;
}
public static void TranslateOrigin(Vector2 translateAmt)
{
OriginPos += translateAmt;
}
public static void SetOriginRotattion(float radians)
{
OriginRotation = radians;
}
public static void RotateOrigin(float radians)
{
OriginRotation += radians;
}
public static void SetOriginScale(Vector2 newScale)
{
OriginScale = newScale;
}
public static void Scale(Vector2 scaleAmt)
{
OriginScale += scaleAmt;
}
#region Drawing
static Texture2D defaultTexture;
static Texture2D DefaultTexture
{
get
{
if(defaultTexture == null)
defaultTexture = new Texture2D(2, 2);
return defaultTexture;
}
}
/// <summary>
/// Draw Text adjusted by originscale and position, rotation
/// </summary>
public static void DrawText(string text, Vector2 position,Vector2 size,float rotation, Color color, float fontsize = 1)
{
Matrix4x4 originalMatrix = GUI.matrix;
Color originalColor = GUI.color;
GUI.matrix = Matrix4x4.TRS(OriginPos + position, Quaternion.AngleAxis((OriginRotation + rotation) * Mathf.Rad2Deg, Vector3.forward), new Vector3(OriginScale.x * fontsize, OriginScale.y * fontsize, 1));
GUI.color = color;
GUI.Label(new Rect(0,0, size.x, size.y), text);
GUI.color = originalColor;
GUI.matrix = originalMatrix;
}
/// <summary>
/// Draw Line adjusted by originscale and position, rotation
/// </summary>
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width)
{
// Determine the angle of the line.
float angle = Vector3.Angle(pointB - pointA, Vector2.right);
// If pointB is above pointA, then angle needs to be negative.
if (pointA.y > pointB.y) { angle = -angle; }
Vector3 scale = new Vector3((pointB - pointA).magnitude + 0.1f, width, 1);
Matrix4x4 originalMatrix = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(OriginPos + pointA, Quaternion.AngleAxis(OriginRotation * Mathf.Rad2Deg + angle, Vector3.forward), new Vector3(OriginScale.x * scale.x, OriginScale.y * scale.y, 1 * scale.z));
GUI.DrawTexture(new Rect(0, -0.5f, 1, 1), DefaultTexture, ScaleMode.ScaleToFit, false, 0, color, width, 0);
GUI.matrix = originalMatrix;
}
/// <summary>
/// Draw a Dot / Point / Circle adjusted by originscale and position, rotation
/// </summary>
public static void DrawDot(Vector2 position, float radius, float rotation, Color32 color, Color32 borderColor, float thickness = 0)
{
DrawEllipse(position, new Vector2(radius, radius), rotation, color, borderColor, thickness);
}
/// <summary>
/// Draw a Ellipse adjusted by originscale and position, rotation
/// </summary>
public static void DrawEllipse(Vector2 position, Vector2 radius, float rotation, Color32 color, Color32 borderColor, float thickness = 0)
{
Vector2 size = radius * 2;
Matrix4x4 originalMatrix = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(OriginPos + position, Quaternion.AngleAxis((OriginRotation + rotation) * Mathf.Rad2Deg, Vector3.forward), new Vector3(OriginScale.x * size.x, OriginScale.y * size.y, 1));
Rect rect = new Rect(-0.5f, -0.5f, 1, 1);
GUI.DrawTexture(rect, DefaultTexture, ScaleMode.ScaleToFit, false, 0, color, 0, 1);
if (thickness > 0) GUI.DrawTexture(rect, DefaultTexture, ScaleMode.ScaleToFit, false, 0, borderColor, thickness/size.x, 1);
GUI.matrix = originalMatrix;
}
/// <summary>
/// Draws a Rect OnGUI adjusted by originscale and position, rotation
/// </summary>
public static void DrawRect(Vector2 position, Vector2 size, float rotation, Color32 color, Color32 borderColor, bool IsCenterOrigin = false, float thickness = 0)
{
Matrix4x4 originalMatrix = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(OriginPos + position, Quaternion.AngleAxis((OriginRotation + rotation) * Mathf.Rad2Deg , Vector3.forward),new Vector3(OriginScale.x * size.x, OriginScale.y * size.y, 1));
Rect rect;
if (IsCenterOrigin) rect = new Rect(-0.5f,-0.5f,1,1);
else rect = new Rect(0,0,1,1);
GUI.DrawTexture(rect, DefaultTexture, ScaleMode.StretchToFill, false, 0, color, 0, 0);
if (thickness > 0) GUI.DrawTexture(rect, DefaultTexture, ScaleMode.StretchToFill, false, 0, borderColor, thickness/size.x, 0);
GUI.matrix = originalMatrix;
}
#endregion
//Extras
/// <summary>
/// Set Background Color,
/// Option to Do Only onces or repeatetly
/// Option to change Main camera Clear mode to 'dont clear'
/// </summary>
static bool HasBGDrawned = false;
public static void SetBGColor(Color32 bgColor,bool repeat = true, bool setCameraFlag = false)
{
if(setCameraFlag) Camera.main.clearFlags = CameraClearFlags.Nothing;
if (!repeat && !HasBGDrawned)
{
GL.Clear(false, true, bgColor, 0);
HasBGDrawned = true;
}
else if(repeat)
{
GL.Clear(false, true, bgColor, 0);
HasBGDrawned = true;
}
}
/// <summary>
/// Set Target Framerate
/// </summary>
public static void SetFrameRate(int rate)
{
Application.targetFrameRate = rate;
}
public static Vector3 WorldToScreenPos(Camera cam, Vector3 position)
{
position.y = -position.y;
return cam.WorldToScreenPoint(position);
}
}
}