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:
    // properties

    GameObjectSpawner Instance;
    UnityEvent ObjectDestroyEvent;
    UnityEvent ObjectSpawnEvent;

    // methods

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

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

    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 ObjectSpawnEvent

Gets or sets UnityEvent called on object spawn.

Methods

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

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.

Returns:

Spawned GameObject.

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

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.

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.