ISpaceContentService

Space Content Service

Interface

Service for interacting with a space's synchronized content.

Properties

allObjects

A dictionary of all space objects currently in the space, keyed by ObjectID.

avatars

A dictionary of all avatar space object components in the space, keyed by ObjectID.

isSceneInitialized

True if the scene has been fully initialized.

networkObjects

A dictionary of all network objects in the space, keyed by ObjectID.

prefabs

A dictionary of all prefab space object components in the space, keyed by ObjectID.

Methods

DestroySpaceObject(int)

Destroy the Space Object with the given ID if the local client has ownership.

GetOwner(ISpatialComponentWithOwner)

Gets the actor ID of the actor that owns the spatial component.

ReleaseOwnership(int)

If the local actor is currently the owner of the object, this will release the ownership of the object and set the owner to 0. This will not destroy the object, but it will make it available for other actors to take ownership of. If succeeded the AllowOwnershipTransfer flag will be enabled if it wasn't, to allow other actors to take ownership of it.

SpawnAvatar(AssetType, string, Vector3, Quaternion, string)

Spawns an NPC avatar, owned by the local actor and visible to all other actors. This object will remain alive in the server until explicitly destroyed (or the server instance was shut down). The ownership of this object can be transferred to another actor if requested, and by default the ownership is transferred to the master client when the owner disconnects.

SpawnNetworkObject(SpatialNetworkObject, Vector3?, Quaternion?)

Spawns an instance of a networked prefab in the space.

SpawnPrefabObject(AssetType, string, Vector3, Quaternion)

Spawns a prefab object from a specific package uploaded thru Spatial Studio.

SpawnSpaceObject()

Spawn a space object that doesn't have any visual representation.

SpawnSpaceObject(Vector3, Quaternion)

Spawn a space object that doesn't have any visual representation.

TakeOwnership(int)

Try to take ownership of a Space Object. This can fail if the object doesn't exist or if the ownership is fixed to another actor. Since the server can reject the request, the ownership change is not immediate.

TransferOwnership(int, int)

Transfer the ownership of an object to another actor. This will only succeed if the local actor is the owner or hasControl of the object. This will fail if:

TryFindNetworkObject(int, out SpatialNetworkObject)

Try to find an instance of SpatialNetworkObject by its object ID.

TryGetSpaceObjectID(GameObject, out int)

Try find the space object ID for a given game object in the scene. Any child game object of a visual representation of a space object can be used to retrieve the space object ID.

Events

onObjectDestroyed

Event fired when a space object is destroyed. The returned object is disposed, meaning it can not be edited, but can still be read from.

onObjectSpawned

Event fired when a space object is spawned.

onSceneInitialized

Event fired when the main space package has been fully initialized.

Examples

private void GetSpaceObjects()
{
foreach (IReadOnlySpaceObject obj in SpatialBridge.spaceContentService.allObjects.Values)
{
Debug.Log($"ID: {obj.objectID} Owner: {obj.ownerActorNumber} Type: {obj.objectType}");
}
}

private void Cast()
{
IReadOnlySpaceObject readOnlyObj = SpatialBridge.spaceContentService.allObjects.Values.First();

// Cast the readonly interface to the writable interface
ISpaceObject spaceObject = readOnlyObj as ISpaceObject;

// Casting doesn't dictate object ownership
// We should still make sure the object is owned locally
if (!spaceObject.isMine)
{
spaceObject.position = new Vector3(0, 0, 0);
spaceObject.SetVariable(0, "hello world!");
}
}

private ISpaceObject mySpaceObject;

private void CreateNewObject()
{
// Create a new space object
SpatialBridge.spaceContentService.SpawnSpaceObject().SetCompletedEvent((resp) => {
// This code gets executed after the object is created, which might be delayed
if (resp.succeeded)
{
ISpaceObject spaceObject = resp.spaceObject;
spaceObject.SetVariable(0, "hello world!"); // Write custom state to the object
mySpaceObject = spaceObject; // Cache it for later reference
}
});
}
private void GetSpaceObjects()
{
foreach (IReadOnlySpaceObject obj in SpatialBridge.spaceContentService.allObjects.Values)
{
Debug.Log($"ID: {obj.objectID} Owner: {obj.ownerActorNumber} Type: {obj.objectType}");
}
}

private void Cast()
{
IReadOnlySpaceObject readOnlyObj = SpatialBridge.spaceContentService.allObjects.Values.First();

// Cast the readonly interface to the writable interface
ISpaceObject spaceObject = readOnlyObj as ISpaceObject;

// Casting doesn't dictate object ownership
// We should still make sure the object is owned locally
if (!spaceObject.isMine)
{
spaceObject.position = new Vector3(0, 0, 0);
spaceObject.SetVariable(0, "hello world!");
}
}

private ISpaceObject mySpaceObject;

private void CreateNewObject()
{
// Create a new space object
SpatialBridge.spaceContentService.SpawnSpaceObject().SetCompletedEvent((resp) => {
// This code gets executed after the object is created, which might be delayed
if (resp.succeeded)
{
ISpaceObject spaceObject = resp.spaceObject;
spaceObject.SetVariable(0, "hello world!"); // Write custom state to the object
mySpaceObject = spaceObject; // Cache it for later reference
}
});
}