The space content service provides access to all objects in a space, including some built-in object types like Avatars. It is also used to spawn and destroy objects in the space.
All space content is represented as IReadOnlySpaceObject and ISpaceObject interfaces. You can access all objects in the space with SpatialBridge.spaceContentService.allObjects or specific object types with SpatialBridge.spaceContentService.avatars, SpatialBridge.spaceContentService.networkObjects, etc.
// Loop through all spaceObject's in the space
foreach (IReadOnlySpaceObject obj in SpatialBridge.spaceContentService.allObjects.Values)
Notice how the allObjects dictionary contains objects of type IReadOnlySpaceObject. This read-only interface is used to communicate that, by default, you should be treating this object as read-only (as if you don't own it).
When you want to modify a SpaceObject you can cast it to an ISpaceObject. However, this cast only changes the interface and does not modify the ownership of the object. Trying to modify an ISpaceObject when the local client is not the owner will result in an error, or in most cases will be a no-op.
// Cache it in a local class variable for later access
mySpaceObject = spaceObject;
}
}
Each SpaceObject has a unique ID, which can be used to reference it later. If you access a SpaceObject frequently however, it is recommended to cache it locally.
// Get our object by ID
if (SpatialBridge.spaceContentService.allObjects.TryGetValue(mySpaceObject.objectID, out IReadOnlySpaceObject spaceObject))
Debug.Log($"Found object with ID: {spaceObject.objectID}; Equals: {spaceObject == mySpaceObject}");
// Get our object by ID
if (SpatialBridge.spaceContentService.allObjects.TryGetValue(mySpaceObject.objectID, out IReadOnlySpaceObject spaceObject))
Debug.Log($"Found object with ID: {spaceObject.objectID}; Equals: {spaceObject == mySpaceObject}");