class MAGES::Utilities::GameObjectSpawner

Overview

The GameObjectSpawner class provides a singleton implementation to spawn and manage GameObjects in a Unity scene. More…

class GameObjectSpawner: public MonoBehaviour
{
public:
    // classes

    class SpawnParams;

    // properties

    GameObjectSpawner Instance;
    UnityEvent ObjectDestroyEvent;
    UnityEvent<SpawnParams, GameObject> ObjectSpawnEvent;

    // methods

    GameObject SpawnObject(
        SerializableObjectReference objectReference,
        Transform parent = null,
        bool isUnique = true,
        bool isLocal = false
    );

    GameObject SpawnObject(
        GameObject prefab,
        Transform parent = null,
        bool isUnique = true,
        bool isLocal = false
    );

    GameObject SpawnObject(
        string path,
        Transform parent = null,
        bool isUnique = true,
        bool isLocal = false
    );

    void DestroyObject(GameObject instantiatedObject);
    GameObject GetDummyGameObject(int prefabID);

    bool TryGetInstantiatedGameObject(
        int prefabID,
        out GameObject instantiatedGameObject
    );

    GameObject AddInstantiatedObject(int prefabID, GameObject prefab);
};

Detailed Documentation

The GameObjectSpawner class provides a singleton implementation to spawn and manage GameObjects in a Unity scene.

Properties

GameObjectSpawner Instance

Gets instance of the GameObjectSpawner.

UnityEvent ObjectDestroyEvent

Gets or sets UnityEvent called on object destroyed.

UnityEvent<SpawnParams, GameObject> ObjectSpawnEvent

Gets or sets UnityEvent called on object spawn.

Methods

GameObject SpawnObject(
    SerializableObjectReference objectReference,
    Transform parent = null,
    bool isUnique = true,
    bool isLocal = false
)

Spawn an object based on the provided prefab.

Parameters:

objectReference

Object reference to spawn.

parent

The parent for the spawned object.

isUnique

If true and the object is already spawned the instance of it will be retrieved from the scene, otherwise a new instance will be created.

isLocal

If true the object will only spawn locally, else it will spawn across network.

Returns:

Spawned GameObject.

GameObject SpawnObject(
    GameObject prefab,
    Transform parent = null,
    bool isUnique = true,
    bool isLocal = false
)

Spawn an object based on the provided prefab.

Parameters:

prefab

Object reference to spawn. If it’s a scene object will be ignored.

parent

The parent for the spawned object.

isUnique

If true and the object is already spawned the instance of it will be retrieved from the scene, otherwise a new instance will be created.

isLocal

If true the object will only spawn locally, else it will spawn across network.

Returns:

Spawned GameObject.

GameObject SpawnObject(
    string path,
    Transform parent = null,
    bool isUnique = true,
    bool isLocal = false
)

Spawn an object based on the provided path.

Parameters:

path

Object path. Note: Prefab must be in resources folder.

parent

The parent for the spawned object.

isUnique

If true and the object is already spawned the instance of it will be retrieved from the scene, otherwise a new instance will be created.

isLocal

If true the object will only spawn locally, else it will spawn across network.

Returns:

Spawned GameObject.

void DestroyObject(GameObject instantiatedObject)

Destroy an object.

Parameters:

instantiatedObject

The gameobject to be destroyed.

GameObject GetDummyGameObject(int prefabID)

Get client temporary spawned gameobject.

Parameters:

prefabID

The local prefab id of the prefab.

Returns:

Returns referenced to the spawned object.

bool TryGetInstantiatedGameObject(
    int prefabID,
    out GameObject instantiatedGameObject
)

Tries to get the instantiated gameobject of a prefab.

Parameters:

prefabID

Prefab instance ID to look for the instantiated game object.

instantiatedGameObject

The instantiated gameobject, null in case prefab ID does not exist.

Returns:

True if instance of this prefab was found.

GameObject AddInstantiatedObject(int prefabID, GameObject prefab)

Attempts to add a new instantiated object to the list of instantiated objects.

Parameters:

prefabID

The instance id of the prefab.

prefab

The instance of the prefab that has been spawned.

Returns:

The instantiated prefab if it was correctly added. Null if another instance exists with this ID.