Painting at Runtime
The runtime API makes it easy to paint and erase tiles at runtime by reusing the brushes that you have already defined.
Painting and erasing tiles
The following source code demonstrates how to paint and erase tiles at runtime. When creating an in-game level designer it is likely that you would want to create some sort of user interface for selecting brushes, etc.
To use the above script:
- Create a script called "RuntimePaintingExample.cs".
- Copy source listing from above.
- Attach script to a game object and specify the following
properties using the Inspector panel:
- The Tile System that you would like to paint on.
- Brush to use when painting with left mouse button.
See Rotorz/Tile System/Demo/Hat Guy/hatguy_level_designer.unity
Improve performance using bulk edit mode
The performance of painting multiple tiles sequentually can be improved using bulk edit mode:
- Tiles that are painted using orienting brushes (including autotile brushes) can change multiple tiles as their surrounding tiles are changes. During bulk edit mode the actual creation of tiles is suppressed until all changes have been made.
- Procedural tiles are presented using procedurally generated meshes which only need to be updated once regardless of how many tiles have been changed.
By default tiles are painted using the default runtime object factory which simply instantiates tile prefabs to create tiles, and destroys tiles upon erasing them. This is generally acceptable for in-game level designers or when generating tiles at the start of a level, though you should consider a pooling solution if painting and erasing tiles in-game.
A pooling system can be utilized by providing a custom object factory implementation. You might opt to implement your very own specialized pooling solution, or simply to integrate a third-party solution.