Drawing in a game

Want to draw things in your game too? This is supported out of the box with the ALINE package. You get the same high performance and almost the same ease of use as in the editor.

Using Draw.ingame

The simplest way is to call methods on Draw.ingame instead of just the Draw class:

// Use the exact same API as the global Draw class
using (Draw.ingame.WithColor(Color.green)) {
Draw.ingame.WireBox(Vector3.zero, Vector3.one);
}

Warning

A common error is trying to do something like: // This will *not* work. The WithColor scope will not affect the ingame commands.
using (Draw.WithColor(Color.red)) {
Draw.ingame.WireBox(Vector3.zero, Vector3.one);
}
This will not work, because scopes for the Draw class will not affect Draw.ingame, and vice versa. So if you for example draw something using Draw.ingame, then it will only be affected by scopes created with Draw.ingame.

If you are drawing many things you can cache the Draw.ingame property in a local variable to reduce the amount of typing you have to do. This is also a tiny bit faster.

var draw = Draw.ingame;

draw.WireBox(Vector3.zero, Vector3.one*1);
draw.WireBox(Vector3.zero, Vector3.one*2);
draw.WireBox(Vector3.zero, Vector3.one*3);
For symmetry the Draw.editor property exist which is analogous to Draw.ingame. All method calls on the static Draw class are forwarded to Draw.editor.

Creating custom command builders

For more complicated use cases you can also create a custom CommandBuilder instance and pass a boolean to it which indicates if it should be rendered in standalone builds or not:

// Create a new CommandBuilder configured to draw in the game
using (var draw = DrawingManager.GetBuilder(true)) {
// Use the exact same API as the global Draw class
draw.WireBox(Vector3.zero, Vector3.one);
}

Note

Creating a new command builder has a small bit of overhead, so it is recommended that you combine your drawing so that you only create a few command builders each frame. Creating 1000 command builders to draw one line each is not going to be as fast as creating 5 command builders to draw 200 lines each. Each command builder is rendered as a separate mesh. Having a few more than one can be faster though as it allows for more parallelization.