Essential Systems / Object Spawner

Object Spawner

Enjoying Essential Systems?Leave a review

Learn to use the included object spawners.

Spawners

SpawnpointsSpawner

Spawns objects at random spawnpoints. Spawnpoints are placed as children of the spawner.

public class BallSpawner : SpawnpointsSpawner<Ball, BallSpawner>
{
    public override Ball Spawn(Object obj, Vector3 point)
        => Instantiate(obj.obj, point, Quaternion.identity);

    public override void Dispose(Ball copy)
    {
        base.Dispose(copy);
        Destroy(copy.gameObject);
    }
}

SinglePointSpawner

Like SpawnpointsSpawner, but limits each spawnpoint to one active object at a time. Override PerformSpawn() instead of Spawn().

public class BallSpawner : SinglePointSpawner<Ball, BallSpawner>
{
    protected override Ball PerformSpawn(Object obj, Vector3 point)
        => Instantiate(obj.obj, point, Quaternion.identity);

    public override void Dispose(Ball copy)
    {
        base.Dispose(copy);
        Destroy(copy.gameObject);
    }
}

InAreaSpawner

Spawns objects at random positions within a bounding area, defined by a Collider on the same object as the spawner.

public class BallSpawner : InAreaSpawner<Ball, BallSpawner>
{
    public override Ball Spawn(Object obj, Vector3 point)
        => Instantiate(obj.obj, point, Quaternion.identity);

    public override void Dispose(Ball copy)
    {
        base.Dispose(copy);
        Destroy(copy.gameObject);
    }
}

InAreaSeparatedSpawner

Like InAreaSpawner, but enforces a minimum distance between spawned objects. Override PerformSpawn() instead of Spawn().

public class BallSpawner : InAreaSeparatedSpawner<Ball, BallSpawner>
{
    protected override Ball PerformSpawn(Object obj, Vector3 point)
        => Instantiate(obj.obj, point, Quaternion.identity);

    public override void Dispose(Ball copy)
    {
        base.Dispose(copy);
        Destroy(copy.gameObject);
    }
}

LoopableSpawner

Spawns objects repeatedly within a bounding area on a set interval. Configure the delay in the inspector.

public class BallSpawner : LoopableSpawner<Ball, BallSpawner>
{
    public override Ball Spawn(Object obj, Vector3 point)
        => Instantiate(obj.obj, point, Quaternion.identity);

    public override void Dispose(Ball copy)
    {
        base.Dispose(copy);
        Destroy(copy.gameObject);
    }
}

Spawning objects

Reference your spawner and use the functions to below to use it.

public BallSpawner spawner;

Ball ball = spawner.Spawn();   // spawn
spawner.Dispose(ball);         // destroy one
spawner.RemoveAll();           // destroy all

Custom spawner

Extend ASpawner directly for full control over spawn and location logic.

public class BallSpawner : ASpawner<Ball, BallSpawner>
{
    public override Ball Spawn(Object obj, Vector3 point)
        => // instantiate logic

    protected override Vector3 GetNewPoint()
        => // location logic

    // optional
    protected override void Dispose()
    {
        base.Dispose();
        // dispose all logic
    }

    public override void Dispose(Ball copy)
    {
        base.Dispose(copy);
        // dispose single logic
    }
}
Last updated