Unity developer with VR and mobile experience - Programmer
Having spent the majority of my career working with idTech3 game engine technology (yes, the tried and true Quake 3 engine), coming to Unity was a breath fresh air in most ways. In some ways, however, it also felt like a bit of a step backwards.
Particularly lacking I found were visual debugging tools using lines, triangles, and other geometric primitives. I would commonly use such a "debug drawing" library to visually display things like positions, velocity, collisions, and mesh bounding areas. Luckily, Unity exposes a low-level interface to the graphics system that neatly works across mobile and desktop platforms.
Scripts attached to Unity Cameras have a handy function called once the rendering has completed - this function is OnPostRender() and will be where we insert our GL code:
public Material material;
public void OnPostRender()
// draws a quad 1 meter wide around the world origin
Vector3 verts = new Vector3
new Vector3(0.5f, -0.5f, -0.5f),
new Vector3(0.5f, -0.5f, 0.5f),
new Vector3(-0.5f, -0.5f, 0.5f),
new Vector3(-0.5f, -0.5f, -0.5f)
for (int i = 0; i < verts.Length; i++)
GL.Vertex(verts[(i + 1) % verts.Length]);
You may have noticed we've also exposed a Material variable to the Unity Inspector. More to come on this property in a future article, but for now you can assign it to the Default-Material which will be used to draw our four lines which represent our quad.
Note also that you may have to move the camera slightly (take a look at the Position in the screenshot above) in order to focus it on 0, 0, 0.
We can alter our code and material set up slightly to be able to control the color that is used to draw the lines. First, select the material Sprites-Default in the Inspector. Second, change the material color before your call to SetPass as below:
public void OnPostRender()
material.color = Color.red;
Here we chose to render our lines as red, but we could use any Color that we can assign to the .color property of our material.
What's powerful about this technique is that you're no longer limited to just debugging in the editor. You can now build and run this application as a standalone or mobile application and the same debug quad will be visible in the final game.
This means that during development of your game, you can distribute special "debug" builds of your application to other developers or QA testers. Being able to visualize gameplay elements such as position, velocity, and collision on the device that you're targeting is a powerful iteration tool to let you quickly find and fix a variety of gameplay problems.
The next part of this article will demonstrate how to render a model's Bounds, even on a character that is moving.
For a practical look at the power of being able to draw debug primitives, have a look at my project in the Asset Store called Collider View. You can also contact me directly and I'll hook you up with a promo code for a free download while I have some available.