|
1 | 1 | # unity-debug-overlay |
2 | | -A fast and garbage free debug overlay for Unity |
| 2 | +A fast and (almost) garbage free debug overlay for Unity. The projects contains two primary components: a debug overlay |
| 3 | +and a console. |
| 4 | + |
| 5 | +# Debug overlay |
| 6 | +The debug overlay, useful for displaying text and graphs that update every frame. |
| 7 | +Like this:  |
| 8 | + |
| 9 | +This can be done with some level of convenience using this code: |
| 10 | + |
| 11 | +```c# |
| 12 | +// FPS in top left corner |
| 13 | + DebugOverlay.Write(1, 0, "FPS:{0,6:###.##}", 1.0f / Time.deltaTime); |
| 14 | + |
| 15 | + // Small graph of FPS below |
| 16 | + fpsHistory[Time.frameCount % fpsHistory.Length] = 1.0f / Time.deltaTime; |
| 17 | + DebugOverlay.DrawGraph(1, 1, 9, 1.5f, fpsHistory, Time.frameCount % fpsHistory.Length, Color.green); |
| 18 | +``` |
| 19 | + |
| 20 | +Even though it involves string formatting, not garbage will be generated. |
| 21 | + |
| 22 | +# Console |
| 23 | +The console is useful for checking logs / output while ingame and also for easily registrering commands |
| 24 | +that can be used to tweak the game behaviour or turn on/off debugging aspects. |
3 | 25 |
|
4 | 26 | You can write stuff like |
5 | 27 |
|
6 | 28 | ```c# |
7 | | - DebugOverlay.Write(2, 2, "Hello, {0,-5} world!", Time.frameCount%100<50 ? "Happy" : "Evil"); |
8 | | - DebugOverlay.Write("FrameNo: {0,7}", Time.frameCount); |
9 | | - DebugOverlay.Write("Time: {0,7:0000.00}", Time.time); |
10 | | - DebugOverlay.Write("FPS: {0,7:###.##}", 1.0f / Time.deltaTime); |
11 | | - DebugOverlay.Write("MonoHeap:{0,7} kb", (int)(UnityEngine.Profiling.Profiler.GetMonoUsedSizeLong() / 1024)); |
12 | | - DebugOverlay.DrawRect(20, 5.2f, 1.0f/Time.deltaTime*0.1f, 0.6f, Color.green); |
13 | | - DebugOverlay.Write("PlayerPos: {0,6:000.0} {1,6:000.0} {2,6:000.0}", |
14 | | - transform.position.x, |
15 | | - transform.position.y, |
16 | | - transform.position.z); |
17 | | - |
18 | | - /// Graphing |
19 | | - System.Array.Copy(fpsArray, 1, fpsArray, 0, fpsArray.Length - 1); |
20 | | - fpsArray[fpsArray.Length - 1] = 1.0f / Time.deltaTime; |
21 | | - DebugOverlay.DrawHist(20, 10, 20, 3, fpsArray, Color.blue, 120.0f); |
22 | | - |
23 | | - System.Array.Copy(moveArray, 1, moveArray, 0, moveArray.Length - 1); |
24 | | - moveArray[moveArray.Length - 1] = transform.parent.position.y; |
25 | | - DebugOverlay.DrawHist(20, 15, 20, 3, moveArray, Color.red, 4.0f); |
| 29 | + |
| 30 | +// Register quit command |
| 31 | + Game.console.AddCommand("quit", CmdQuit, "Quit game"); |
| 32 | + |
| 33 | + /* ... */ |
| 34 | + |
| 35 | + void CmdQuit(string[] args) |
| 36 | + { |
| 37 | + Game.console. |
| 38 | + Application.Quit(); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + |
26 | 43 | ``` |
27 | 44 |
|
28 | | -and it will look like |
| 45 | +and it will look like this |
| 46 | + |
| 47 | + |
| 48 | + |
29 | 49 |
|
30 | | - |
|
0 commit comments