Follow this guide to quickly get simple procedural generation of chunks up and running in Unity.
Installation in Unity
LayerProcGen requires Unity 2019.4 or later.
Install LayerProcGen as a Unity Package Manager package from this Git URL:
https://github.com/runevision/LayerProcGen.git#upm
See Unity's instructions here. If you already have Git installed, it's simply these steps:
Importing samples
You can also import samples for how to use the framework on the Samples tab of the package.
The Simple Samples have no special requirements.
The Terrain Sample requires:
- Packages:
- Burst package
- Input System package
- Player Settings:
- Enabling Allow unsafe code
- Setting Active Input Handling to either Input System Package (New) or Both
- Setting Color Space to Linear (recommended)
- Only tested with Builtin Render Pipeline
Your first test
Here's how to create your own layer-and-chunk pair of classes and get them running in a simple scene.
Create layer and chunk 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 (also included in the Simple Samples):
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 class PointsLayer : ChunkBasedDataLayer<PointsLayer, PointsChunk> {
public override int chunkW { get { return 256; } }
public override int chunkH { get { return 256; } }
public RandomHash rand = new RandomHash(1234);
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);
});
}
}
Definition CollectionExtensions.cs:12
Definition AbstractDataLayer.cs:13
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.