分类 English 下的文章

Problem raised: As the original scene manager required synchronizing all scenes from the server to the client, this is suitable for end-to-end game mode and is not common in C/S mode. Opening a room game requires dynamically creating new scenes, which was not supported by the original scene manager and needs to be implemented by oneself.

Change 1: Because it is to realize online games in the game hall on the Internet, the scene manager is replaced with a simple scene manager. In this simple scene manager, the client has only one scene, while the server has many scenes. The client selects the scene, and the server synchronizes the scene accordingly.

Change 2: It is agreed that all scenes have unique names, and an empty scene named Active is loaded on the server and set as Active Scene. That is to say, all dynamically created network objects are created by default in this empty scene. After creation, use SceneManager again MoveGameObject To Scene moves it to the specified scene. In the online environment, it is impossible for everyone to specify a specific activity scenario, as this can lead to conflicts. Once agreed upon, there will be no conflicts.

Change 3: Added a method to create, load, and merge scenes. Everyone knows about the game of opening a room. There is a room creator who names newly opened room scenes using a naming convention similar to "Room_player username". And this newly created scene is loaded as a fixed scene Room and merged into a new name "Room_Player Username". Similarly, the main game scene goes through the same process, loading a fixed Game scene and merging it into the newly created "Games_Player Username" scene. Then, the scene manager implements scene synchronization between the server and client.

The instance reference for the custom scene manager is NetworkManager Singleton.SceneManager, His method is as follows:

//Server loading scenario
//SceneName is the name of the scene
void SvrLoadScene(string sceneName);

//Server uninstallation scenario
//SceneName Scene Name
void SvrUnloadScene(string sceneName);

//The server creates a new scene and loads another scene, then merges the loaded scene with offset into the newly created scene.
//ID specifies the client ID
//DesScene Target Scene Name
//SrcScene Source Scene Name
//Offset "Scene offset
void SvrCreateAndMergeScene(ulong id,string desScene,string srcScene,Vector3 offset);

//Client switches to the current scene
//CurScene specifies the scene name
void ClientSwitchScene(string curScene);

//Scenario of switching a client on the server
//ID specifies the client ID
//CurScene specifies the scene name
void ServerSwitchScene(ulong id,string curScene);

//Set the default scenario, and when the client first connects to the server, the server will synchronize the default scenario with the client.
//DefaultScene Default Scene Name
void SetDefaultScene(string defaultScene);

//The server-side scene loading completion event is triggered when the server-side scene is loaded.
//Use NetworkManager.Singleton.SceneManager.OnServerSceneLoadComplete += OnServerSceneLoadComplete;
//Add an event trigger function.
OnServerSceneLoadComplete(string scname)

The first step is to prepare the development environment

Prepare the Unity Editor with version number Unity 2022.3.53f1c1. Select the Windows Dedicated Server Build Surport module in Unity Hub, which will be used when exporting to the server.

Step 2: Download the project files and dependency packages

Download the source code file, unzip it, and then add the project from the disk in Unityhub. Add the fwhas directory as the engineering project. Then double-click on the project to launch the Unity editor and open the project file.

Download the custom netcode package, open the package manager in the Unity editor, and use "Add tarball from disk" to add the downloaded package named "com. unity. netcode. gameobjects s-1.12.2. tgz", ignoring the prompt for invalid certificates.

In the package manager of the Unity editor, select Unity Registry, search for Localization, and then click Install.

Step 3: Select Export Configuration

There is a configuration file called Init in the Assets/Config directory. Click on this file and in the inspector, you can switch the StartType to Client and Server. When you want to export client files, select Client before exporting. If you want to export server files, select Server before exporting, and then switch the export platform to Dedicated Server.
You should be able to export server and client packages here.

Step 4: Run the server and client

Double click the mouse to run fwhas.exe in the server package, which is a command-line program. After he starts, the message will no longer scroll, and then start fwhas.exe in the client package. The client can open several more tests to test the effectiveness of networked operation.

This template file can provide the function of the game hall on the Internet, providing six functional modules: user registration, user login, new room, join room, preparation, and game start.

The template is developed using Unity 2022.3.53f1c1, references the NetCode For GameObjects 1.12.2 package and Localization package, and replaces the scene manager in the NetCode For GameObjects 1.12.2 package, so that it can easily support online games that open rooms on the Internet.

Demo Windows platform exe package file exported from game template file: client server

Source code engineering file for game template file: Source code

NetCode For GameObjects 1.12.2 package file for custom scene manager: package file

After downloading the package file, open the package manager and install it by adding the tarball package from the disk.

Localization package, use the unity registry in the package manager to search for installation.