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!

  1. Start by creating your custom object as a Unity Prefab, then and add the Spatial Prefab Object component to the root GameObject. 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.

    Step 1

  2. Create a new Prefab Object package within the configuration window.

    Step 2

  3. 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.

    Step 3

  4. Provide a thumbnail of exactly 512x512 with compression disabled in the import settings.

    Thumbnail Generation

    Import settings of prefab object thumbnail

  5. 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.

    Step 5

Spawning Methods

A user can spawn prefab objects in two ways:

  1. Built-in "Add Content" menu, if the user owns the package or item, and they have permission to add content to the space.
  2. 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#:

SpatialBridge.spaceContentService.SpawnPrefabObject(AssetType.Package, "sku", new Vector3(0f, 0f, 0f), Quaternion.identity);
SpatialBridge.spaceContentService.SpawnPrefabObject(AssetType.Package, "sku", new Vector3(0f, 0f, 0f), Quaternion.identity);

Visual Scripting:

Spawn Prefab Object