Essential Systems
  • Welcome
  • Getting Started
    • Quickstart
    • ‼️Installation Guide
  • Systems
    • Object Pooling
    • Audio
    • Camera Shake
    • Console
    • Dialogue
    • Interface
    • Inputs
    • Proximity
    • Spawner
    • UI Components
    • Particles
    • Objects
    • Editor
    • Scenes
    • Utility
Powered by GitBook
On this page
  • Spawn object
  • List of all pre-made spawners
  • Create custom spawner
  1. Systems

Spawner

Most games need some sort of object spawner. The internal spawn logic can look a bit different depending on scenario and context. This is why we created a scalable and modular Object Spawner.

PreviousProximityNextUI Components

Last updated 3 months ago

Underlying all spawners is the ASpawner script. This is an extendable class that handles all the boring setup logic for a spawner. But, because of this, the direct inheritance could be abit difficult to grasp. Therefor, we created some sub-spawner systems to allow you to get the ball rolling faster.

To understand how we can create a new spawner to spawn a specific type, we can extend the SpawnpointsSpawner and create a script that spawns Balls.

// we extend SpawnpointsSpawner which requires two "arguments":
//    1: type to spawn (Ball)    2: the current type (BallSpawner)
public class BallSpawner : SpawnpointsSpawner<Ball, BallSpawner>
{
    public override Ball Spawn(Object obj, Vector3 point)
    {
        return Instantiate(obj.obj, point, Quaternion.identity); // instantiate the object.
    }
    
    public override void Dispose(Ball copy)
    {
        base.Dispose(copy); // must call.
        Destroy(copy.gameObject); // destroy the game object.
    }
}

Spawn object

To spawn an object from another script we simply need to call:

public BallSpawner spawner; // reference to your desired spawner.
Ball ball = spawner.Spawn(); // spawn.

spawner.Dispose(ball); // to destroy object.
spawner.RemoveAll(); // to remove all objects that has spawned.

List of all pre-made spawners

SpawnpointsSpawner - spawns an object randomly between different spawnpoints. Spawnpoints are given as children of the spawner itself so it's easy to configure and visually see spawnpoints.
// we extend SpawnpointsSpawner which requires two "arguments":
//    1: type to spawn (Ball)    2: the current type (BallSpawner)
public class BallSpawner : SpawnpointsSpawner<Ball, BallSpawner>
{
    public override Ball Spawn(Object obj, Vector3 point)
    {
        return Instantiate(obj.obj, point, Quaternion.identity); // instantiate the object.
    }
    
    public override void Dispose(Ball copy)
    {
        base.Dispose(copy); // must call.
        Destroy(copy.gameObject); // destroy the game object.
    }
}
SinglePointSpawner - spawns an object randomly between different spawnpoints, but only 1 object can spawn per spawnpoint until that object gets disposed. Spawnpoints are given as children of the spawner itself so it's easy to configure and visually see spawnpoints.
// we extend SinglePointSpawner which requires two "arguments":
//    1: type to spawn (Ball)    2: the current type (BallSpawner)
public class BallSpawner : SinglePointSpawner<Ball, BallSpawner>
{
    // instead of overriding Spawn(), we override PerformSpawn() due to SinglePointSpawner
    //    requires extra Spawn() logic.
    protected override Ball PerformSpawn(Object obj, Vector3 point)
    {
        return Instantiate(obj.obj, point, Quaternion.identity); // instantiate the object.
    }
    
    public override void Dispose(Ball copy)
    {
        base.Dispose(copy); // must call.
        Destroy(copy.gameObject); // destroy the game object.
    }
}
InAreaSpawner - spawns an object in some pre-defined area. Area is defined by a Collider's (on the same object as the spawner) bounds.
// we extend InAreaSpawner which requires two "arguments":
//    1: type to spawn (Ball)    2: the current type (BallSpawner)
public class BallSpawner : InAreaSpawner<Ball, BallSpawner>
{
    public override Ball Spawn(Object obj, Vector3 point))
    {
        return Instantiate(obj.obj, point, Quaternion.identity); // instantiate the object.
    }
    
    public override void Dispose(Ball copy)
    {
        base.Dispose(copy); // must call.
        Destroy(copy.gameObject); // destroy the game object.
    }
}
InAreaSeparatedSpawner - spawns an object in some pre-defined area. Area is defined by a Collider's (on the same object as the spawner) bounds. Unlike InAreaSpawner, InAreaSeparatedSpawner allows a minimum distance between two objects.
// we extend InAreaSeparatedSpawner which requires two "arguments":
//    1: type to spawn (Ball)    2: the current type (BallSpawner)
public class BallSpawner : InAreaSeparatedSpawner<Ball, BallSpawner>
{
    // instead of overriding Spawn(), we override PerformSpawn() due to InAreaSeparatedSpawner
    //    requires extra Spawn() logic
    protected override Ball PerformSpawn(Object obj, Vector3 point)
    {
        return Instantiate(obj.obj, point, Quaternion.identity); // instantiate the object.
    }
    
    public override void Dispose(Ball copy)
    {
        base.Dispose(copy); // must call.
        Destroy(copy.gameObject); // destroy the game object.
    }
}
LoopableSpawner - spawns an object in some pre-defined area. Area is defined by a Collider's (on the same object as the spawner) bounds. Spawning is loopable, meaning that it will constantly spawn new objects defined by inspector property delay.
// we extend LoopableSpawner which requires two "arguments":
//    1: type to spawn (Ball)    2: the current type (BallSpawner)
public class BallSpawner : LoopableSpawner<Ball, BallSpawner>
{
    public override Ball Spawn(Object obj, Vector3 point)
    {
        return Instantiate(obj.obj, point, Quaternion.identity); // instantiate the object.
    }
    
    public override void Dispose(Ball copy)
    {
        base.Dispose(copy); // must call.
        Destroy(copy.gameObject); // destroy the game object.
    }
}

Create custom spawner

Sometimes, the pre-made spawners are not enough and you would like to create your own spawning logic. Here's how:

// we extend ASpawner which requires two "arguments":
//    1: type to spawn (Ball)    2: the current type (BallSpawner)
public class BallSpawner : ASpawner<Ball, BallSpawner>
{
    // must be overriden.
    public override Ball Spawn(Object obj, Vector3 point)
    {
        return // instantiate logic goes here
    }
    
    // must be overriden
    protected override Vector3 GetNewPoint()
    {
        return // custom location logic here
    }
    
    // can be overriden
    protected override void Dispose()
    {
        base.Dispose();
        // dispose for EVERYTHING logic here
    }
    // can be overriden
    public override void Dispose(Ball copy)
    {
        base.Dispose(copy);
        // dispose for specific object logic here
    }
}