Follow this guide to quickly get simple procedural generation of chunks up and running in Unity.
Create layer and chunk classes
First we need to create a layer-and-chunk pair of classes.
Below is a minimal example that creates three random points within each chunk, and where the layer has an API for retrieving all points within given bounds.
You can save this example code as a file named PointsLayer.cs
:
using System.Collections.Generic;
public class PointsChunk :
LayerChunk<PointsLayer, PointsChunk> {
public List<Point> pointList = new List<Point>();
public override void Create(int level, bool destroy) {
if (destroy) {
pointList.Clear();
}
else {
for (int i = 0; i < 3; i++) {
layer.rand.Range(bounds.min.x, bounds.max.x, index.x, index.y, i * 2),
layer.rand.Range(bounds.min.y, bounds.max.y, index.x, index.y, i * 2 + 1));
pointList.Add(point);
}
}
}
}
public override int chunkW { get { return 256; } }
public override int chunkH { get { return 256; } }
public PointsLayer() {
LayerManagerBehavior.OnUpdate -= Update;
LayerManagerBehavior.OnUpdate += Update;
}
void Update() {
HandleAllChunks(0, c => {
foreach (
Point p
in c.pointList)
DebugDrawer.DrawCross(p, 16f, UnityEngine.Color.green);
});
}
public void GetPointsInBounds(
ILC q, List<Point> outPoints,
GridBounds bounds) {
HandleChunksInBounds(q, bounds, 0, chunk => {
foreach (var point in chunk.pointList)
if (bounds.Contains(point))
outPoints.Add(point);
});
}
}
A utility for drawing lines for debug visualizations.
Definition DebugDrawer.cs:22
In a layer-and-chunk pair of classes, the layer inherits from this class.
Definition ChunkBasedDataLayer.cs:72
In a layer-and-chunk pair of classes, the chunk inherits from this class.
Definition LayerChunk.cs:235
Represents a layer "user" that requires data within the specified bounds.
Definition LayerChunk.cs:47
Definition CollectionExtensions.cs:12
Definition AbstractDataLayer.cs:13
Definition GridBounds.cs:14
Definition RandomHash.cs:44
Add components that set things in motion
Next, the framework needs two things to be set in motion: The LayerManager and a TopLayerDependecy.
- Create an empty GameObject named "LayerManager" and add the LayerManagerBehavior component. This component is a wrapper for the
LayerManager
class. Keep the default settings.
- Also add the DebugDrawer component to the GameObject. This ensures we can see things drawn with the
DebugDrawer
utility class.
- Create an empty GameObject named "GenerationSource" and add the GenerationSource component. This component creates a TopLayerDependency object that establishes a dependency on the specified layer at the specified size.
- In the
Layer
dropdown, select your PointsLayer
.
- Set the
Size
property to 2000, 2000.
Test the generation
Now you're ready to test the generation!
- Enter Play Mode in Unity.
- Ensure the Scene View is looking at the XY plane (or the XZ plane if you specified that in the LayerManager component).
- Select the "Generation Source" GameObject and frame select it by pressing the F key.
- You should be able to see a bunch of green crosses.
- You can now drag the GenerationSource GameObject around, and observe the generated area of green crosses follow along.
Bonus - setting up debug visualizations:
- Stop Play Mode and select the LayerManager GameObject.
- Add the VisualizationManager component.
- Add an entry to the
Layers
list in the component.
- Choose the
PointsLayer
in the dropdown.
- Choose a color for the layer.
- Open the Debug Options window via the menu item
Window > Debug Options
.
- Enter Play Mode.
- In the Debug Options window, enable
LayerVis
and Layers > PointsLayer
to see chunk bounds and the layer bounds.