b. Create GameObjects

The GameObject Creation module contains a default implementation of spawning GameObjects for your SpatialOS entities and offers the ability to customize that process.

This page covers how this works and how you can set it up to create GameObjects linked to the player entities in your world.

How GameObject creation works

The GameObject Creation module contains the IEntityGameObjectCreator interface. Any class that implements this interface is known as a GameObject creator. This interface contains a method called when a SpatialOS entity enters a worker’s view and another method called when a SpatialOS entity leaves a worker’s view.

The default GameObject creator is the GameObjectCreatorFromMetadata class, which instantiates the prefab mapped through a provided EntityRepresentationMapping asset, at the location defined by the entity's Position component.

Plan your entity representations

You’ll be adding more player functionality in later chapters of this tutorial series, so it is important to plan your client-side and server player representations.

Future chapters in this tutorial series will add more client-side behavior to players, so you’ll want a Player’s client-side and server-side representations to be different. This means you should create worker-specific prefabs for UnityClient and UnityGameLogic worker types.

The Player GameObject on a UnityClient worker-instance could either be owned by the client-worker or be just another player that has entered the client-worker’s view. This means that while we should still be able to see all the Player GameObjects, some behavior is only enabled on an authoritative client-worker.

The client-side Player object owned by a given client-worker will need to:

  • Rotate the camera using mouse input.
  • Move the player using keyboard input.
  • Send requests to spawn and interact with spheres.

The server-side representation only needs to receive and apply movement updates.

Create the Player prefabs

Create a UnityClient and UnityGameLogic folder under Assets/Prefabs/Entities.

        ├── Assets/
            ├── Prefabs/
                ├── Entities/
                    ├── UnityClient/
                    ├── UnityGameLogic/

Now, create a capsule in your scene hierarchy and drag it into the newly created UnityClient folder to make a new prefab. Remove the prefab’s capsule collider and rename it to “Player” to match the metadata defined in the Player entity template.

Make a duplicate of this prefab, and move the duplicate to Assets/Prefabs/Entities/UnityGameLogic. The duplicate will have a (1) at the end, so ensure that you rename it to “Player”.

Once you have created both Player prefabs, delete the remaining capsule GameObject from the scene hierarchy.

Update prefab mapping

You now need to update the prefab mapping to map your Player prefabs to the Player entity type.

In the Assets/Config/ClientPrefabMapping asset, select New Entity Type and pick the SimpleEntityResolver.

Expand the newly created element and change the Entity type to Player, and the Prefab to the Player prefab you've just created in Assets/Prefabs/Entities/UnityClient.

Repeat this step for the GamelogicPrefabMapping asset, but link to the prefab in the UnityGameLogic directory instead.

What should the ClientPrefabMapping look like when I'm done?

What should the GamelogicPrefabMappinglook like when I'm done?

To test that everything works, ensure a local deployment is running and hit the play button. In the “Scene” view, you should notice that a Player prefab is created on both client-worker and server-worker levels!

Add some color

To better distinguish between client-side and server-side GameObjects in the development scene, it would be good to also add some color to your GameObjects.

Create a Color folder under Assets/BlankProject/Scripts/. Inside this directory, create a file called ObjectColor.cs.

        ├── Assets/
            ├── BlankProject/
                ├── Scripts/
                    ├── Color/
                        ├── ObjectColor.cs

Copy the code below into the file:

using BlankProject;
using Improbable.Gdk.Subscriptions;
using UnityEngine;

namespace Scripts.Color
    public class ObjectColor : MonoBehaviour
        public UnityEngine.Color color;
        private Renderer renderer;

        private void OnEnable()
            renderer = GetComponent<Renderer>();

        private void Update()
            renderer.material.color = color;

In the Unity Editor, add the ObjectColor behavior to the Player prefab for the UnityClient only. This is so that only the client-side representations of a Player are given color. Choose a color for the player that makes it easy to distinguish from a plain object.

To test this change, hit the play button in the Editor.

You should observe that, as expected, the player object on the client-level has the color you chose, but the player object on the other level does not.

Image: The player GameObject on the client-level has the color you chose, but the player GameObject on the other level does not

This validates that the default GameObject creator makes use of a worker’s worker type to instantiate the correct representation of a Player.

Updated about a year ago

b. Create GameObjects

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.