-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathLabelDrawer.cs
More file actions
163 lines (135 loc) · 4.62 KB
/
LabelDrawer.cs
File metadata and controls
163 lines (135 loc) · 4.62 KB
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
///
/// LabelDrawer by Nothke
///
/// Draws labels at a 3D position at runtime while LabelDrawer.Label() is called.
///
/// Do not call from OnDrawGizmos, this is intended for runtime,
/// see GizmosX.cs instead.
///
/// ============================================================================
///
/// MIT License
///
/// Copyright(c) 2021 Ivan Notaroš
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
/// SOFTWARE.
///
/// ============================================================================
///
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LabelDrawer : MonoBehaviour
{
public Font font;
struct Label3D
{
public readonly string text;
public readonly Vector3 position;
public readonly Color color;
public Label3D(string text, Vector3 position, Color color)
{
this.text = text;
this.position = position;
this.color = color;
}
}
List<Label3D> labels = new List<Label3D>();
static readonly Color defaultColor = Color.white;
GUIStyle style = new GUIStyle();
public static LabelDrawer e;
void Awake()
{
e = this;
if (font)
style.font = font;
style.normal.textColor = defaultColor;
// Needs to wait for end of frame because it's the only event that happens after OnGUI
StartCoroutine(EndOfFrameLoop());
}
WaitForEndOfFrame waifu = new WaitForEndOfFrame();
IEnumerator EndOfFrameLoop()
{
while (true)
{
yield return waifu;
labels.Clear();
}
}
static void CreateIfDoesntExist()
{
if (!e)
{
var go = new GameObject("-- LabelDrawer");
var drawer = go.AddComponent<LabelDrawer>();
e = drawer;
}
}
public static void Label(string text, Vector3 position)
{
CreateIfDoesntExist();
if (e.enabled)
e.labels.Add(new Label3D(text, position, defaultColor));
}
public static void Label(string text, Vector3 position, Color color)
{
CreateIfDoesntExist();
if (e.enabled)
e.labels.Add(new Label3D(text, position, color));
}
private void OnDisable()
{
labels.Clear();
}
private void OnGUI()
{
Camera cam = Camera.main;
Color originalContentColor = GUI.contentColor;
foreach (var label in labels)
{
Vector3 screenPos = cam.WorldToScreenPoint(label.position);
if (screenPos.z < 0) // if behind
continue;
GUI.contentColor = label.color;
GUI.Label(new Rect(screenPos.x, Screen.height - screenPos.y, 1000, 1000), label.text, style);
}
GUI.contentColor = originalContentColor;
}
#if UNITY_EDITOR
private void OnDrawGizmos()
{
bool isGameView = UnityEditor.SceneView.currentDrawingSceneView == null;
if (Application.isPlaying && !isGameView && e && e.enabled)
{
if (Application.isEditor)
{
foreach (var label in labels)
{
style.normal.textColor = label.color;
UnityEditor.Handles.Label(label.position, label.text, style);
}
}
// Required because WaitForEndOfFrame doesn't work when Game view is not visible
// When both Game and Scene view are visible at the same time tho, it should only clear when Game is not in focus
if (!Application.isFocused)
labels.Clear();
}
}
#endif
}