Prefab Object
Prefab objects let you create any type of interactive object like furniture, vending machines, vehicles, interactive jukeboxes, etc. These can be interacted with using your avatar or even other custom objects.
Check out the 1980s boombox in the Starter Template for an example of a Prefab Object package.
How to Use
Creating a prefab object is similar to creating a prefab in Unity!
-
Start by creating your custom object as a Unity Prefab, then and add the
Spatial Prefab Object
component to the rootGameObject
. You may add additional effects, like particle systems or audio sources, if you would like. Creating a custom object without any visuals is not recommended since the avatar will not be able to interact with it.Caution
Also be sure to set its Position to
(0, 0, 0)
, otherwise you’ll encounter odd behavior when attempting to place it in a space. -
Create a new
Prefab Object
package within the configuration window. -
Give your package a name. This name will be what shows up in the content menu / inventory inside the Spatial app. Afterwards, drag the prefab you created in step 1 into the
Prefab
field. -
Provide a thumbnail of exactly
512x512
with compression disabled in the import settings. -
To test this package, make sure it is the “active package” inside the project configuration panel and then press the
Test Package
button inside the main toolbar.
Spawning Methods
A user can spawn prefab objects in two ways:
- Built-in "Add Content" menu, if the user owns the package or item, and they have permission to add content to the space.
- Scripting behavior, either via C# or Visual Scripting.
Scripting
Warning
When spawning prefab objects, you will need to manage their lifecycle; there’s currently no API available for removing them from the scene. In addition, the next time you load the space any previously spawned prefab objects will be loaded, since the spawned objects will persist in save data (similar to spawning through the content menu).
C#:
Visual Scripting: