Skip to content

Samples

A progressive tutorial series plus full game samples. Each builds on the previous.

Tutorial series (Trecs 101)

# Sample Concepts
01 Hello Entity Components, tags, templates, systems, world setup
02 Spawn & Destroy Entity lifecycle, dynamic spawning, timed destruction
03 Aspects Bundled component access for clean iteration
04 Predator Prey Cross-entity references, template inheritance, event cleanup
05 Job System Burst compilation, parallel jobs, WrapAsJob
06 Partitions Template partitions, partition transitions, partition-filtered iteration
07 Feeding Frenzy Complex simulation, visual smoothing, multiple interacting systems
08 Sets Dynamic entity subsets, overlapping membership
09 Interpolation Fixed-to-variable timestep interpolation
10 Dynamic Collections Five trail-storage strategies: UniquePtr Queue, FixedArray, FixedList, TrecsList, TrecsArray
13 Aspect Interfaces Shared aspect contracts via partial interface for reusable cross-species helpers
14 Blob Seed Pattern Content-pipeline-stable BlobIds for shared immutable assets on the heap
15 Reactive Events OnAdded / OnRemoved observers for cleanup and stat tracking
16 Multiple Worlds Two independent World instances ticking side-by-side
17 Heightmap Blobs Content-derived BlobIds via UniqueHashGenerator, plus a NativeSharedPtr Burst-job variant

Game samples

# Sample Description
11 Snake Complete grid-based game with input handling and recording/playback
12 Feeding Frenzy Benchmark Performance benchmark comparing partition approaches and iteration styles

Running the samples

Open Assets/Samples/Core/Main.unity in Unity 6000.3+ and press Play. A SampleCycler lets you switch between samples at runtime. Each sample has its own composition root.

Sample architecture: Bootstrap & CompositionRoot

Each sample uses a two-class pattern:

  • Bootstrap — MonoBehaviour driving the Unity lifecycle. Awake() calls CompositionRoot.Construct() and runs the initializers; Update() / LateUpdate() invoke tickables and late tickables; OnDestroy() runs disposables.
  • CompositionRootBase — abstract MonoBehaviour each sample subclasses. Construct() builds the WorldBuilder, creates systems, and returns four List<Action> callbacks (init, tick, late tick, dispose).
// Simplified — each sample's composition root looks roughly like this:
public class MyCompositionRoot : CompositionRootBase
{
    public override void Construct(
        out List<Action> initializables,
        out List<Action> tickables,
        out List<Action> lateTickables,
        out List<Action> disposables)
    {
        var world = new WorldBuilder()
            .AddTemplate(MyEntity.Template)
            .Build();

        world.AddSystems(new ISystem[] { new MySystem() });

        initializables = new() { world.Initialize };
        tickables = new() { world.Tick };
        lateTickables = new() { world.LateTick };
        disposables = new() { world.Dispose };
    }
}

Note

This pattern is a convenience for the samples. Trecs is unopinionated about application structure — it doesn't register MonoBehaviours, manage singletons, or hook into Unity's update loop. Use a DI framework (Reflex, Zenject, VContainer), plain MonoBehaviours, ScriptableObjects, or anything else. Trecs only requires that your code calls world.Tick(), world.LateTick(), and world.Dispose() at the right times.

Shared utilities

Common/ contains helpers shared across samples (sample code — not part of Trecs itself):

  • RenderableGameObjectManager — pools and spawns Unity GameObjects for entities carrying PrefabId + GameObjectId, driven by OnAdded / OnRemoved observers
  • IndirectRenderer — GPU-instanced indirect rendering for high entity counts (samples using IndirectRenderable)
  • Bootstrap / CompositionRootBase — the lifecycle wiring above
  • SampleUtil — primitive / material helpers
  • DisposeCollectionIDisposable aggregator
  • Common components and templates (Position, Rotation, UniformScale, ColorComponent, PrefabId, GameObjectId, CommonTemplates.IndirectRenderable, CommonTemplates.RenderableGameObject, etc.)

If you copy code from a sample, also copy its Common/ dependencies.