Unity Tutorial - Basic Multiplayer - Part 1 - Setup
Need help with the tutorial? Join our Discord server!
Prepare Project Structure
This project is separated into two sub-projects;
- Server (module) code
- Client code
First, we'll create a project root directory (you can choose the name):
mkdir SpacetimeDBUnityTutorial
cd SpacetimeDBUnityTutorial
We'll start by populating the client directory.
Setting up the Tutorial Unity Project
In this section, we will guide you through the process of setting up a Unity Project that will serve as the starting point for our tutorial. By the end of this section, you will have a basic Unity project and be ready to implement the server functionality.
Step 1: Create a Blank Unity Project
Open Unity and create a new project by selecting "New" from the Unity Hub or going to File -> New Project.
⚠️ Important: Ensure 3D (URP) is selected to properly render the materials in the scene!
For Project Name use client. For Project Location make sure that you use your SpacetimeDBUnityTutorial directory. This is the directory that we created in a previous step.
Click "Create" to generate the blank project.
Step 2: Adding Required Packages
To work with SpacetimeDB and ensure compatibility, we need to add some essential packages to our Unity project. Follow these steps:
- Open the Unity Package Manager by going to Window -> Package Manager.
- In the Package Manager window, select the "Unity Registry" tab to view unity packages.
- Search for and install the following package:
- Input System: Enables the use of Unity's new Input system used by this project.
- You may need to restart the Unity Editor to switch to the new Input system.
Step 3: Importing the Tutorial Package
In this step, we will import the provided Unity tutorial package that contains the basic single-player game setup. Follow these instructions:
- Download the tutorial package from the releases page on GitHub: https://github.com/clockworklabs/SpacetimeDBUnityTutorial/releases/latest
- In Unity, go to Assets -> Import Package -> Custom Package.
- Browse and select the downloaded tutorial package file.
- Unity will prompt you with an import settings dialog. Ensure that all the files are selected and click "Import" to import the package into your project.
- At this point in the project, you shouldn't have any errors.
Step 4: Running the Project
Now that we have everything set up, let's run the project and see it in action:
- Open the scene named "Main" in the Scenes folder provided in the project hierarchy by double-clicking it.
NOTE: When you open the scene you may get a message saying you need to import TMP Essentials. When it appears, click the "Import TMP Essentials" button.
🧹 Clear any false-positive TMPro errors that may show.
- Press the Play button located at the top of the Unity Editor.
Enter any name and click "Continue"
You should see a character loaded in the scene, and you can use the keyboard or mouse controls to move the character around.
Congratulations! You have successfully set up the basic single-player game project. In the next section, we will start integrating SpacetimeDB functionality to enable multiplayer features.
Writing our SpacetimeDB Server Module
At this point you should have the single player game working. In your CLI, your current working directory should be within your SpacetimeDBUnityTutorial directory that we created in a previous step.
Create the Module
It is important that you already have the SpacetimeDB CLI tool installed.
Run SpacetimeDB locally using the installed CLI. In a new terminal or command window, run the following command:
spacetime start
💡 Standalone mode will run in the foreground. 💡 Below examples Rust language, but you may also use C#.
The Entity Component Systems (ECS)
Before we continue to creating the server module, it's important to understand the basics of the ECS. This is a game development architecture that separates game objects into components for better flexibility and performance. You can read more about the ECS design pattern here.
We chose ECS for this example project because it promotes scalability, modularity, and efficient data management, making it ideal for building multiplayer games with SpacetimeDB.
Create the Server Module
From here, the tutorial continues with your favorite server module language of choice: