- Extending EzyFox Server's room management to launch a dedicated Unity server
- One Unity-Server for all rooms/servers
Clone the repository:
git clone git@github.com:Assambra/Master-Server-Unity.git
Get submodules:
Open your Git command client, go to the Master-Server-Unity directory, and clone the submodules.
cd Master-Server-Unity
git submodule update --init --recursive
Now open both Unity projects, Unity-Server and Unity-Client, in your Unity editor. You will get a bunch of errors; ignore them for now as we will fix this in the next steps. Do the following step for both projects because it is identical in both projects.
Go to the folder Assets/EzyFox Server - C-Sharp Client - Unity
in your Unity Editor, right-click -> Create -> Assembly Definition, and rename it to
com.tvd12.ezyfoxserver.client
Server: In the Unity Editor, open the folder and select the file Assets/Server/Scripts/Assambra.Server
.
Client: In the Unity Editor, open the folder and select the file Assets/Client/Scripts/Assambra.Client
.
In the Inspector under Assembly Definition Reference, there is some missing reference starting with GUID:. Delete this one and add a new one with the plus sign and select the earlier created com.tvd12.ezyfoxserver.client
assembly definition. Don't forget to hit apply, then it should recompile and the errors should be gone.
A brief overview of the structure of the project. We use three projects: master-server (EzyFox Server), Unity-Server, and Unity-Client. It is important to know that the Unity-Servers are also clients for the master-server, and we always use the Ezyfox Client SDK for communication between Unity-Client <--> master-server and master-server <--> Unity-Server.
We use an EzyFox server that acts as a master server. Its tasks are user management, processing database requests, and starting and managing a Unity server for each room. The client makes a request to the master server, which forwards the request to the Unity-Server. Once these requests have been processed by the Unity server, they are sent to the master server and from there sent back to the client(s).
- Install mongoDB.
- Open your mongosh.
- Create your Database:
use master-server
- Create a new user and password and give it access to the created database:
db.createUser({user: "root", pwd: "123456", roles: [{role: "readWrite", db:"master-server" }] })
- Create the following collections:
db.createCollection("user", { collation: { locale: 'en_US', strength: 2 } })
db.user.createIndex({ username: 1 })
db.createCollection("character", { collation: { locale: 'en_US', strength: 2 } })
db.character.createIndex({ name: 1 })
db.createCollection("character_location", { collation: { locale: 'en_US', strength: 2 } })
- Use this file for the next step:
Location: master-server/master-server-common/src/main/resources/master-server-common-config.properties
- Insert the following values for your database and change them to your needs:
database.mongo.uri=mongodb://root:123456@127.0.0.1:27017/master-server
database.mongo.database=master-server
database.mongo.collection.naming.case=UNDERSCORE
database.mongo.collection.naming.ignored_suffix=Entity
In this example file, we use:
- user: root
- password: 123456
- database: master-server
We use a single project for the Unity Servers. The server contains all server scenes that correspond to a room. When it starts, the server receives three parameters: username, password, and room. The server then uses our Module-GameManager to start the corresponding server scene. This has the advantage that, on the one hand, we only use one project and do not have to create another server project for each server and can therefore share the code that is the same for all servers. The Module GameManager uses a persistent scene where all manager classes/GameObjects are located. The room scene with the 3D objects such as terrain are additionally loaded.
Open the Unity-Server project in Unity. Open Build Settings: File -> Build Settings. Be sure to include all server scenes in "Scenes In Build"; if not, add them. Select as Platform Dedicated Server and choose under Target Platform your platform, Windows or Linux, and build the project.
Open the master-server project in your IDE, edit the file: master-server/master-server-app-api/resources/application.properties
and add the path to your server executables from the previous step where you saved them. E.g., D:/Game Builds/Unity-Server/Unity-Server.exe
.
Open the Unity-Client project in Unity and use the Persistent Unity Scene location Assets/Client/Scenes/Persistent
. If the master-server is running, you can click Play in the Unity Editor.
Join us on Discord