-
Notifications
You must be signed in to change notification settings - Fork 23
/
RuntimeDebugDraw.cs
891 lines (769 loc) · 24.6 KB
/
RuntimeDebugDraw.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
using System;
using System.Collections.Generic;
using UnityEngine;
using RuntimeDebugDraw.Internal;
using Conditional = System.Diagnostics.ConditionalAttribute;
/*
* Runtime Debug Draw
* Single file debuging DrawLine/DrawText/etc that works in both Scene/Game view, also works in built PC/mobile builds.
*
* Very Important Notes:
* 1. You are expected to make some changes in this file before intergrating this into you projects.
* a. `_DEBUG` symbol, you should this to your project's debugging symbol so these draw calls will be compiled away in final release builds.
* If you forget to do this, DrawXXX calls won't be shown.
* b. `RuntimeDebugDraw` namespace and `Draw` class name, you can change this into your project's namespace to make it more accessable.
* c. `Draw.DrawLineLayer` is the layer the lines will be drawn on. If you have camera postprocessing turned on, set this to a layer that is ignored
* by the post processor.
* d. `GetDebugDrawCamera()` will be called to get the camera for line drawings and text coordinate calcuation.
* It defaults to `Camera.main`, returning null will mute drawings.
* e. `DrawTextDefaultSize`/`DrawDefaultColor` styling variables, defaults as Unity Debug.Draw.
* 2. Performance should be relatively ok for debugging, but it's never intended for release use. You should use conditional to
* compile away these calls anyway. Additionally DrawText is implemented with OnGUI, which costs a lot on mobile devices.
* 3. Don't rename this file of 'RuntimeDebugDraw' or this won't work. This file contains a MonoBehavior also named 'RuntimeDebugDraw' and Unity needs this file
* to have the same name. If you really want to rename this file, remember to rename the 'RuntimeDebugDraw' class below too.
*
* License: Public Domain
*/
namespace RuntimeDebugDraw
{
public static class Draw
{
#region Main Functions
/// <summary>
/// Which layer the lines will be drawn on.
/// </summary>
public const int DrawLineLayer = 4;
/// <summary>
/// Default font size for DrawText.
/// </summary>
public const int DrawTextDefaultSize = 12;
/// <summary>
/// Default color for Draws.
/// </summary>
public static Color DrawDefaultColor = Color.white;
/// <summary>
/// Which camera to use for line drawing and texts coordinate calculation.
/// </summary>
/// <returns>Camera to debug draw on, returns null will mute debug drawing.</returns>
public static Camera GetDebugDrawCamera()
{
return Camera.main;
}
/// <summary>
/// Draw a line from <paramref name="start"/> to <paramref name="end"/> with <paramref name="color"/>.
/// </summary>
/// <param name="start">Point in world space where the line should start.</param>
/// <param name="end">Point in world space where the line should end.</param>
/// <param name="color">Color of the line.</param>
/// <param name="duration">How long the line should be visible for.</param>
/// <param name="depthTest">Should the line be obscured by objects closer to the camera?</param>
[Conditional("_DEBUG")]
public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration, bool depthTest)
{
CheckAndBuildHiddenRTDrawObject();
_rtDraw.RegisterLine(start, end, color, duration, !depthTest);
return;
}
/// <summary>
/// Draws a line from start to start + dir in world coordinates.
/// </summary>
/// <param name="start">Point in world space where the ray should start.</param>
/// <param name="dir">Direction and length of the ray.</param>
/// <param name="color">Color of the drawn line.</param>
/// <param name="duration">How long the line will be visible for (in seconds).</param>
/// <param name="depthTest">Should the line be obscured by other objects closer to the camera?</param>
[Conditional("_DEBUG")]
public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration, bool depthTest)
{
CheckAndBuildHiddenRTDrawObject();
_rtDraw.RegisterLine(start, start + dir, color, duration, !depthTest);
return;
}
/// <summary>
/// Draw a text at given position.
/// </summary>
/// <param name="pos">Position</param>
/// <param name="text">String of the text.</param>
/// <param name="color">Color for the text.</param>
/// <param name="size">Font size for the text.</param>
/// <param name="duration">How long the text should be visible for.</param>
/// <param name="popUp">Set to true to let the text moving up, so multiple texts at the same position can be visible.</param>
[Conditional("_DEBUG")]
public static void DrawText(Vector3 pos, string text, Color color, int size, float duration, bool popUp)
{
CheckAndBuildHiddenRTDrawObject();
_rtDraw.RegisterDrawText(pos, text, color, size, duration, popUp);
return;
}
/// <summary>
/// Attach text to a transform.
/// </summary>
/// <param name="transform">Target transform to attach text to.</param>
/// <param name="strFunc">Function will be called on every frame to get a string as attached text. </param>
/// <param name="offset">Text attach offset to transform position.</param>
/// <param name="color">Color for the text.</param>
/// <param name="size">Font size for the text.</param>
[Conditional("_DEBUG")]
public static void AttachText(Transform transform, Func<string> strFunc, Vector3 offset, Color color, int size)
{
CheckAndBuildHiddenRTDrawObject();
_rtDraw.RegisterAttachText(transform, strFunc, offset, color, size);
return;
}
#endregion
#region Overloads
/*
* These are tons of overloads following how 'Debug.DrawXXX' are overloaded.
*/
/// <summary>
/// Draw a line from <paramref name="start"/> to <paramref name="end"/> with <paramref name="color"/>.
/// </summary>
/// <param name="start">Point in world space where the line should start.</param>
/// <param name="end">Point in world space where the line should end.</param>
[Conditional("_DEBUG")]
public static void DrawLine(Vector3 start, Vector3 end)
{
DrawLine(start, end, DrawDefaultColor, 0f, true);
return;
}
/// <summary>
/// Draw a line from <paramref name="start"/> to <paramref name="end"/> with <paramref name="color"/>.
/// </summary>
/// <param name="start">Point in world space where the line should start.</param>
/// <param name="end">Point in world space where the line should end.</param>
/// <param name="color">Color of the line.</param>
[Conditional("_DEBUG")]
public static void DrawLine(Vector3 start, Vector3 end, Color color)
{
DrawLine(start, end, color, 0f, true);
return;
}
/// <summary>
/// Draw a line from <paramref name="start"/> to <paramref name="end"/> with <paramref name="color"/>.
/// </summary>
/// <param name="start">Point in world space where the line should start.</param>
/// <param name="end">Point in world space where the line should end.</param>
/// <param name="color">Color of the line.</param>
/// <param name="duration">How long the line should be visible for.</param>
[Conditional("_DEBUG")]
public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration)
{
DrawLine(start, end, color, duration, true);
return;
}
/// <summary>
/// Draws a line from start to start + dir in world coordinates.
/// </summary>
/// <param name="start">Point in world space where the ray should start.</param>
/// <param name="dir">Direction and length of the ray.</param>
[Conditional("_DEBUG")]
public static void DrawRay(Vector3 start, Vector3 dir)
{
DrawRay(start, dir, DrawDefaultColor, 0f, true);
return;
}
/// <summary>
/// Draws a line from start to start + dir in world coordinates.
/// </summary>
/// <param name="start">Point in world space where the ray should start.</param>
/// <param name="dir">Direction and length of the ray.</param>
/// <param name="color">Color of the drawn line.</param>
[Conditional("_DEBUG")]
public static void DrawRay(Vector3 start, Vector3 dir, Color color)
{
DrawRay(start, dir, color, 0f, true);
return;
}
/// <summary>
/// Draws a line from start to start + dir in world coordinates.
/// </summary>
/// <param name="start">Point in world space where the ray should start.</param>
/// <param name="dir">Direction and length of the ray.</param>
/// <param name="color">Color of the drawn line.</param>
/// <param name="duration">How long the line will be visible for (in seconds).</param>
[Conditional("_DEBUG")]
public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration)
{
DrawRay(start, dir, color, duration, true);
return;
}
/// <summary>
/// Draw a text at given position.
/// </summary>
/// <param name="pos">Position</param>
/// <param name="text">String of the text.</param>
[Conditional("_DEBUG")]
public static void DrawText(Vector3 pos, string text)
{
DrawText(pos, text, DrawDefaultColor, DrawTextDefaultSize, 0f, false);
return;
}
/// <summary>
/// Draw a text at given position.
/// </summary>
/// <param name="pos">Position</param>
/// <param name="text">String of the text.</param>
/// <param name="color">Color for the text.</param>
[Conditional("_DEBUG")]
public static void DrawText(Vector3 pos, string text, Color color)
{
DrawText(pos, text, color, DrawTextDefaultSize, 0f, false);
return;
}
/// <summary>
/// Draw a text at given position.
/// </summary>
/// <param name="pos">Position</param>
/// <param name="text">String of the text.</param>
/// <param name="color">Color for the text.</param>
/// <param name="size">Font size for the text.</param>
[Conditional("_DEBUG")]
public static void DrawText(Vector3 pos, string text, Color color, int size)
{
DrawText(pos, text, color, size, 0f, false);
return;
}
/// <summary>
/// Draw a text at given position.
/// </summary>
/// <param name="pos">Position</param>
/// <param name="text">String of the text.</param>
/// <param name="color">Color for the text.</param>
/// <param name="size">Font size for the text.</param>
/// <param name="duration">How long the text should be visible for.</param>
[Conditional("_DEBUG")]
public static void DrawText(Vector3 pos, string text, Color color, int size, float duration)
{
DrawText(pos, text, color, size, duration, false);
return;
}
/// <summary>
/// Attach text to a transform.
/// </summary>
/// <param name="transform">Target transform to attach text to.</param>
/// <param name="strFunc">Function will be called on every frame to get a string as attached text. </param>
[Conditional("_DEBUG")]
public static void AttachText(Transform transform, Func<string> strFunc)
{
AttachText(transform, strFunc, Vector3.zero, DrawDefaultColor, DrawTextDefaultSize);
return;
}
/// <summary>
/// Attach text to a transform.
/// </summary>
/// <param name="transform">Target transform to attach text to.</param>
/// <param name="strFunc">Function will be called on every frame to get a string as attached text. </param>
/// <param name="offset">Text attach offset to transform position.</param>
[Conditional("_DEBUG")]
public static void AttachText(Transform transform, Func<string> strFunc, Vector3 offset)
{
AttachText(transform, strFunc, offset, DrawDefaultColor, DrawTextDefaultSize);
return;
}
/// <summary>
/// Attach text to a transform.
/// </summary>
/// <param name="transform">Target transform to attach text to.</param>
/// <param name="strFunc">Function will be called on every frame to get a string as attached text. </param>
/// <param name="offset">Text attach offset to transform position.</param>
/// <param name="color">Color for the text.</param>
[Conditional("_DEBUG")]
public static void AttachText(Transform transform, Func<string> strFunc, Vector3 offset, Color color)
{
AttachText(transform, strFunc, offset, color, DrawTextDefaultSize);
return;
}
#endregion
#region Internal
/// <summary>
/// Singleton RuntimeDebugDraw component that is needed to call Unity APIs.
/// </summary>
private static Internal.RuntimeDebugDraw _rtDraw;
/// <summary>
/// Check and build
/// </summary>
private static string HIDDEN_GO_NAME = "________HIDDEN_C4F6A87F298241078E21C0D7C1D87A76_";
private static void CheckAndBuildHiddenRTDrawObject()
{
if (_rtDraw != null)
return;
// try reuse existing one first
_rtDraw = GameObject.FindObjectOfType<RuntimeDebugDraw.Internal.RuntimeDebugDraw>();
if (_rtDraw != null)
return;
// instantiate an hidden gameobject w/ RuntimeDebugDraw attached.
// hardcode an GUID in the name so one won't accidentally get this by name.
var go = new GameObject(HIDDEN_GO_NAME);
var childGo = new GameObject(HIDDEN_GO_NAME);
childGo.transform.parent = go.transform;
_rtDraw = childGo.AddComponent<RuntimeDebugDraw.Internal.RuntimeDebugDraw>();
// hack to only hide outer go, so that RuntimeDebugDraw's OnGizmos will work properly.
go.hideFlags = HideFlags.HideAndDontSave;
if (Application.isPlaying)
GameObject.DontDestroyOnLoad(go);
return;
}
#endregion
}
#region Editor
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoad]
public static class DrawEditor
{
static DrawEditor()
{
// set a low execution order
var name = typeof(RuntimeDebugDraw.Internal.RuntimeDebugDraw).Name;
foreach (UnityEditor.MonoScript monoScript in UnityEditor.MonoImporter.GetAllRuntimeMonoScripts())
{
if (name != monoScript.name)
continue;
if (UnityEditor.MonoImporter.GetExecutionOrder(monoScript) != 9990)
{
UnityEditor.MonoImporter.SetExecutionOrder(monoScript, 9990);
return;
}
}
}
}
#endif
#endregion
}
namespace RuntimeDebugDraw.Internal
{
internal class RuntimeDebugDraw : MonoBehaviour
{
#region Basics
private void CheckInitialized()
{
// as RuntimeDebugDraw component has a very low execution order, other script might Awake()
// earlier than this and at that moment it's not initialized. check and init on every public
// member
if (_drawTextEntries == null)
{
_ZTestBatch = new BatchedLineDraw(depthTest: true);
_AlwaysBatch = new BatchedLineDraw(depthTest: false);
_lineEntries = new List<DrawLineEntry>(16);
_textStyle = new GUIStyle();
_textStyle.alignment = TextAnchor.UpperLeft;
_drawTextEntries = new List<DrawTextEntry>(16);
_attachTextEntries = new List<AttachTextEntry>(16);
}
return;
}
private void Awake()
{
CheckInitialized();
return;
}
private void OnGUI()
{
DrawTextOnGUI();
return;
}
#if UNITY_EDITOR
private void OnDrawGizmos()
{
DrawTextOnDrawGizmos();
return;
}
#endif
private void LateUpdate()
{
TickAndDrawLines();
TickTexts();
return;
}
private void OnDestroy()
{
_AlwaysBatch.Dispose();
_ZTestBatch.Dispose();
return;
}
private void Clear()
{
_drawTextEntries.Clear();
_lineEntries.Clear();
_linesNeedRebuild = true;
return;
}
#endregion
#region Draw Lines
private class DrawLineEntry
{
public bool occupied;
public Vector3 start;
public Vector3 end;
public Color color;
public float timer;
public bool noZTest;
}
private List<DrawLineEntry> _lineEntries;
// helper class for batching
private class BatchedLineDraw : IDisposable
{
public Mesh mesh;
public Material mat;
private List<Vector3> _vertices;
private List<Color> _colors;
private List<int> _indices;
public BatchedLineDraw(bool depthTest)
{
mesh = new Mesh();
mesh.MarkDynamic();
// relying on a builtin shader, but it shouldn't change that much.
mat = new Material(Shader.Find("Hidden/Internal-Colored"));
mat.SetInt("_ZTest", depthTest
? 4 // LEqual
: 0 // Always
);
_vertices = new List<Vector3>();
_colors = new List<Color>();
_indices = new List<int>();
return;
}
public void AddLine(Vector3 from, Vector3 to, Color color)
{
_vertices.Add(from);
_vertices.Add(to);
_colors.Add(color);
_colors.Add(color);
int verticeCount = _vertices.Count;
_indices.Add(verticeCount - 2);
_indices.Add(verticeCount - 1);
return;
}
public void Clear()
{
mesh.Clear();
_vertices.Clear();
_colors.Clear();
_indices.Clear();
return;
}
public void BuildBatch()
{
mesh.SetVertices(_vertices);
mesh.SetColors(_colors);
mesh.SetIndices(_indices.ToArray(), MeshTopology.Lines, 0); // cant get rid of this alloc for now
return;
}
public void Dispose()
{
GameObject.DestroyImmediate(mesh);
GameObject.DestroyImmediate(mat);
return;
}
}
private BatchedLineDraw _ZTestBatch;
private BatchedLineDraw _AlwaysBatch;
private bool _linesNeedRebuild;
public void RegisterLine(Vector3 start, Vector3 end, Color color, float timer, bool noZTest)
{
CheckInitialized();
DrawLineEntry entry = null;
for (int ix = 0; ix < _lineEntries.Count; ix++)
{
if (!_lineEntries[ix].occupied)
{
entry = _lineEntries[ix];
break;
}
}
if (entry == null)
{
entry = new DrawLineEntry();
_lineEntries.Add(entry);
}
entry.occupied = true;
entry.start = start;
entry.end = end;
entry.color = color;
entry.timer = timer;
entry.noZTest = noZTest;
_linesNeedRebuild = true;
return;
}
private void RebuildDrawLineBatchMesh()
{
_ZTestBatch.Clear();
_AlwaysBatch.Clear();
for (int ix = 0; ix < _lineEntries.Count; ix++)
{
var entry = _lineEntries[ix];
if (!entry.occupied)
continue;
if (entry.noZTest)
_AlwaysBatch.AddLine(entry.start, entry.end, entry.color);
else
_ZTestBatch.AddLine(entry.start, entry.end, entry.color);
}
_ZTestBatch.BuildBatch();
_AlwaysBatch.BuildBatch();
return;
}
private void TickAndDrawLines()
{
if (_linesNeedRebuild)
{
RebuildDrawLineBatchMesh();
_linesNeedRebuild = false;
}
// draw on UI layer which should bypass most postFX setups
Graphics.DrawMesh(_AlwaysBatch.mesh, Vector3.zero, Quaternion.identity, _AlwaysBatch.mat, layer: Draw.DrawLineLayer ,camera : null, submeshIndex : 0,properties: null, castShadows : false, receiveShadows : false);
Graphics.DrawMesh(_ZTestBatch.mesh, Vector3.zero, Quaternion.identity, _ZTestBatch.mat, layer: Draw.DrawLineLayer ,camera : null, submeshIndex : 0,properties: null, castShadows : false, receiveShadows : false);
// update timer late so every added entry can be drawed for at least one frame
for (int ix = 0; ix < _lineEntries.Count; ix++)
{
var entry = _lineEntries[ix];
if (!entry.occupied)
continue;
entry.timer -= Time.deltaTime;
if (entry.timer < 0)
{
entry.occupied = false;
_linesNeedRebuild = true;
}
}
return;
}
#endregion
#region Draw Text
[Flags]
public enum DrawFlag : byte
{
None = 0,
DrawnGizmo = 1 << 0,
DrawnGUI = 1 << 1,
DrawnAll = DrawnGizmo | DrawnGUI
}
private class DrawTextEntry
{
public bool occupied;
public GUIContent content;
public Vector3 anchor;
public int size;
public Color color;
public float timer;
public bool popUp;
public float duration;
// Text entries needs to be draw in both OnGUI/OnDrawGizmos, need flags for mark
// has been visited by both
public DrawFlag flag = DrawFlag.None;
public DrawTextEntry()
{
content = new GUIContent();
return;
}
}
private class AttachTextEntry
{
public bool occupied;
public GUIContent content;
public Vector3 offset;
public int size;
public Color color;
public Transform transform;
public Func<string> strFunc;
public DrawFlag flag = DrawFlag.None;
public AttachTextEntry()
{
content = new GUIContent();
return;
}
}
private List<DrawTextEntry> _drawTextEntries;
private List<AttachTextEntry> _attachTextEntries;
private GUIStyle _textStyle;
public void RegisterDrawText(Vector3 anchor, string text, Color color, int size, float timer, bool popUp)
{
CheckInitialized();
DrawTextEntry entry = null;
for (int ix = 0; ix < _drawTextEntries.Count; ix++)
{
if (!_drawTextEntries[ix].occupied)
{
entry = _drawTextEntries[ix];
break;
}
}
if (entry == null)
{
entry = new DrawTextEntry();
_drawTextEntries.Add(entry);
}
entry.occupied = true;
entry.anchor = anchor;
entry.content.text = text;
entry.size = size;
entry.color = color;
entry.duration = entry.timer = timer;
entry.popUp = popUp;
#if UNITY_EDITOR
entry.flag = DrawFlag.None;
#else
// in builds consider gizmo is already drawn
entry.flag = DrawFlag.DrawnGizmo;
#endif
return;
}
public void RegisterAttachText(Transform target, Func<string> strFunc, Vector3 offset, Color color, int size)
{
CheckInitialized();
AttachTextEntry entry = null;
for (int ix = 0; ix < _attachTextEntries.Count; ix++)
{
if (!_attachTextEntries[ix].occupied)
{
entry = _attachTextEntries[ix];
break;
}
}
if (entry == null)
{
entry = new AttachTextEntry();
_attachTextEntries.Add(entry);
}
entry.occupied = true;
entry.offset = offset;
entry.transform = target;
entry.strFunc = strFunc;
entry.color = color;
entry.size = size;
// get first text
entry.content.text = strFunc();
#if UNITY_EDITOR
entry.flag = DrawFlag.None;
#else
// in builds consider gizmo is already drawn
entry.flag = DrawFlag.DrawnGizmo;
#endif
return;
}
private void TickTexts()
{
for (int ix = 0; ix < _drawTextEntries.Count; ix++)
{
var entry = _drawTextEntries[ix];
if (!entry.occupied)
continue;
entry.timer -= Time.deltaTime;
if (entry.flag == DrawFlag.DrawnAll)
{
if (entry.timer < 0)
{
entry.occupied = false;
}
// actually no need to tick DrawFlag as it won't move
}
}
for (int ix = 0; ix < _attachTextEntries.Count; ix++)
{
var entry = _attachTextEntries[ix];
if (!entry.occupied)
continue;
if (entry.transform == null)
{
entry.occupied = false;
entry.strFunc = null; // needs to release ref to callback
}
else if (entry.flag == DrawFlag.DrawnAll)
{
// tick content
entry.content.text = entry.strFunc();
// tick flag
#if UNITY_EDITOR
entry.flag = DrawFlag.None;
#else
// in builds consider gizmo is already drawn
entry.flag = DrawFlag.DrawnGizmo;
#endif
}
}
return;
}
private void DrawTextOnGUI()
{
var camera = Draw.GetDebugDrawCamera();
if (camera == null)
return;
for (int ix = 0; ix < _drawTextEntries.Count; ix++)
{
var entry = _drawTextEntries[ix];
if (!entry.occupied)
continue;
GUIDrawTextEntry(camera, entry);
entry.flag |= DrawFlag.DrawnGUI;
}
for (int ix = 0; ix < _attachTextEntries.Count; ix++)
{
var entry = _attachTextEntries[ix];
if (!entry.occupied)
continue;
GUIAttachTextEntry(camera, entry);
entry.flag |= DrawFlag.DrawnGUI;
}
return;
}
private void GUIDrawTextEntry(Camera camera, DrawTextEntry entry)
{
Vector3 worldPos = entry.anchor;
Vector3 screenPos = camera.WorldToScreenPoint(worldPos);
screenPos.y = Screen.height - screenPos.y;
if (entry.popUp)
{
float ratio = entry.timer / entry.duration;
screenPos.y -= (1 - ratio * ratio) * entry.size * 1.5f;
}
_textStyle.normal.textColor = entry.color;
_textStyle.fontSize = entry.size;
Rect rect = new Rect(screenPos, _textStyle.CalcSize(entry.content));
GUI.Label(rect, entry.content, _textStyle);
return;
}
private void GUIAttachTextEntry(Camera camera, AttachTextEntry entry)
{
if (entry.transform == null)
return;
Vector3 worldPos = entry.transform.position + entry.offset;
Vector3 screenPos = camera.WorldToScreenPoint(worldPos);
screenPos.y = Screen.height - screenPos.y;
_textStyle.normal.textColor = entry.color;
_textStyle.fontSize = entry.size;
Rect rect = new Rect(screenPos, _textStyle.CalcSize(entry.content));
GUI.Label(rect, entry.content, _textStyle);
return;
}
#if UNITY_EDITOR
private void DrawTextOnDrawGizmos()
{
if (!(Camera.current == Draw.GetDebugDrawCamera()
|| Camera.current == UnityEditor.SceneView.lastActiveSceneView.camera))
return;
var camera = Camera.current;
if (camera == null)
return;
UnityEditor.Handles.BeginGUI();
for (int ix = 0; ix < _drawTextEntries.Count; ix++)
{
var entry = _drawTextEntries[ix];
if (!entry.occupied)
continue;
GUIDrawTextEntry(camera, entry);
entry.flag |= DrawFlag.DrawnGizmo;
}
for (int ix = 0; ix < _attachTextEntries.Count; ix++)
{
var entry = _attachTextEntries[ix];
if (!entry.occupied)
continue;
GUIAttachTextEntry(camera, entry);
entry.flag |= DrawFlag.DrawnGizmo;
}
UnityEditor.Handles.EndGUI();
return;
}
#endif
#endregion
}
}