Overview of Systems

General explanations for all systems will be written soon.

Room Manager


Introduction

The system is designed to facilitate dynamic room creation, management, and interactions within multiplayer games.

Core Purpose

The Room Management System is designed to provide game developers with the ability to dynamically create and manage rooms in multiplayer games. The system supports fundamental features such as room creation, player joining, network connection management, and in-room interactions.

Key Features

1. Room Creation and Joining

  • Room Creation: The system allows for the creation of a specific game room with parameters such as room name and maximum player count.

  • Room Joining: Players can join existing rooms based on specific criteria, such as room name.

2. Network Management

  • Using Mirror: The Mirror library is employed to manage network connections and facilitate multiplayer interactions.

3. Synchronization and Interaction

  • Player Synchronization: Synchronization among players within a game room is managed over the network.

  • Interaction Management: The system enables player interactions with each other and the game world.

4. Security and Error Handling

  • Error Handling: The system addresses error scenarios during room creation or joining and provides user feedback.

  • Security Measures: Anti-cheat and security measures are implemented by the system.

5. Room Management

  • Room Deletion: Rooms can be deleted under certain conditions, either by the room owner or as defined by the system.

  • Updating Room Status: Room status, such as the number of players, is continuously updated by the system.

Usage

Game developers can utilize this system to create flexible and dynamic room structures in their multiplayer games.

Example Usage

// Room creation for Server
RoomManager.Instance.CreateRoom(new RoomInfo("RoomName", 4));

// Joining a room
RoomManager.Instance.JoinRoom(NetworkClient.connection, "RoomName");

Conclusion

This room management system, aims to provide Mirror users with effective room management capabilities for multiplayer games. With its core features, developers can create more flexible and dynamic multiplayer experiences in their games.

Room Container


The Room Container System is designed to streamline the management of singleton instances and event listeners within specific rooms in a multiplayer game. It ensures unique singleton instances per room and facilitates event communication. Here's a concise overview:

Purpose:

  1. Singleton Management:

    • Ensures only one instance of a specific type exists in a room.

    • Provides methods to add, remove, and retrieve singleton instances.

  2. Listener Management:

    • Enables registration and unregistration of event listeners for room-specific events.

    • Enhances communication within a room.

How to Use:

  1. Singleton Management with RoomContainerHelper:

    • Add Singleton: AddSingleton<T>(value) - Adds a singleton instance of type T to the Room Container.

    • Remove Singleton: RemoveSingleton<T>() - Removes a singleton instance of type T from the Room Container.

    • Get Singleton: GetSingleton<T>() - Retrieves a singleton instance of type T from the Room Container.

  2. Listener Management with RoomContainerHelper:

    • Register Listener: RegisterListener<T>(listener) - Registers a listener to receive updates from a room.

    • Unregister Listener: UnregisterListener<T>(listener) - Unregisters a listener from a room.

Example Usage:

// Add a single instance to the Room Container for the room of the scene it is in.
RoomContainer.Singleton.Add(scene, SingletonInstance);;

// Retrieves a singleton instance of the specified type
// in the room of the scene it is in
var instance = gameObject.GetSingleton<MySingletonType>();

// Deletes a singleton instance of the type specified
// to the Room Container for the room of the scene in which it resides.
RoomContainer.Singleton.Remove<MySingletonType>(scene);

// Register a listener for room-specific events.
gameObject.RegisterListener<MyRoomListenerType>(listenerInstance);

// Unregister the listener when it's no longer needed.
gameObject.UnregisterListener<MyRoomListenerType>(listenerInstance);

Benefits:

  • Organization: Keeps singleton instances organized per room.

  • Communication: Enhances communication by facilitating listener registration for room-specific events.

  • Singleton Integrity: Ensures singleton uniqueness within each room.

Conclusion

The Room Container System simplifies multiplayer game development by providing a structured approach to manage resources and events within specific game rooms.

Room Loader


The Room Loader System facilitates the loading and unloading of game rooms. It offers flexibility through different loading strategies. Here's a brief summary of its components:

  1. IRoomLoader Interface:

    • Load Room: Loads a specified room using room information.

    • Unload Room: Unloads a specified room.

  2. RoomLoaderType Enum:

    • NoneScene: No scene loading required.

    • SingleScene: Loads a single scene for the room.

    • AdditiveScene: Loads additional scenes for the room.

  3. SceneRoomLoader Class:

    • Purpose: Loads a room by loading its associated scene.

    • How to Use:

      • Create an instance.

      • Call LoadRoom to load a room with specified info.

      • Call UnLoadRoom to unload the room.

  4. NoneSceneRoomLoader Class:

    • Purpose: Represents a loader where no scene loading is required.

    • How to Use:

      • Create an instance.

      • Call LoadRoom for any loading actions.

      • Call UnLoadRoom for any unloading actions.

Benefits:

  • Abstraction: Hides scene loading details.

  • Flexibility: Supports different loading strategies.

Usage Examples:

Using SceneRoomLoader for loading a room:

var roomLoader = new SceneRoomLoader();
roomLoader.LoadRoom(myRoom, myRoomInfo, () => { /* Room loaded, additional actions */ });
roomLoader.UnLoadRoom(myRoom);

Using NoneSceneRoomLoader for a room without scene loading:

var noneSceneLoader = new NoneSceneRoomLoader();
noneSceneLoader.LoadRoom(myRoom, myRoomInfo, () => { /* Room loaded, additional actions */ });
noneSceneLoader.UnLoadRoom(myRoom);

The Room Loader System simplifies room transitions, offering a clear interface and flexibility in loading strategies.

Services


RoomServer


RoomServer is a static class acting as a service, facilitating server-side operations related to game rooms. It abstracts and simplifies the interaction with the underlying RoomManagerBase for managing rooms and scenes.

Key Features:

  1. Room Transactions:

    • Create Room: Creates a room with specified information (name, scene, max players, custom data).

    • Join Room: Allows a client to join a room.

    • Exit Room: Handles a client exiting a room, considering disconnection status.

    • Remove Room: Removes a specific room or all rooms forcibly if needed.

  2. Scene Operations:

    • Change Scene: Changes the scene for a specified room, providing an option to keep client objects.

How to Use:

  1. Create a Room:

    • Call CreateRoom with necessary parameters.

    • Optionally, specify a NetworkConnectionToClient if applicable.

  2. Join a Room:

    • Call JoinRoom with the room name.

    • Optionally, provide a NetworkConnectionToClient if needed.

  3. Exit a Room:

    • Call ExitRoom with a NetworkConnectionToClient and disconnection status.

  4. Remove a Room:

    • Call RemoveRoom with the room name and specify if removal is forced.

  5. Remove All Rooms:

    • Call RemoveAllRoom and specify if removal is forced.

  6. Change Scene:

    • Call ChangeScene with the room name and the new scene name.

    • Optionally, provide a Room instance for more control.

Example Usage:

Creating a room and joining:

// Create a room
RoomServer.CreateRoom("MyRoom", "MyScene", 4, ("setting1", "value1"), ("setting2", "value2"));

// Join the room
RoomServer.JoinRoom("MyRoom");

Changing the scene for a room:

// Change the scene for a room
RoomServer.ChangeScene("MyRoom", "NewScene", true);

RoomServer simplifies room management on the server, offering a convenient and clear API for common operations.

Room Client


RoomClient is a static class serving as a client-side service for room-related operations. It encapsulates functionality provided by the underlying RoomManagerBase for ease of use.

Key Features:

  1. Room Transactions:

    • Create Room: Initiates a request to create a room with specified parameters.

    • Join Room: Initiates a request to join a room by its name.

    • Exit Room: Initiates a request to exit the current room.

  2. Data Methods:

    • Get Room Custom Data: Retrieves custom data associated with the current room.

How to Use:

  1. Create a Room:

    • Call CreateRoom with the necessary parameters (name, scene, max players).

    • Optionally, specify custom data or privacy settings.

  2. Join a Room:

    • Call JoinRoom with the name of the room to join.

  3. Exit a Room:

    • Call ExitRoom to leave the current room.

    • Optionally, provide a parameter indicating if the exit is due to disconnection.

  4. Retrieve Room Custom Data:

    • Call GetRoomCustomData with the desired custom data name.

    • Returns the value associated with the specified custom data name.

Example Usage:

Creating a room and joining:

// Create a room
RoomClient.CreateRoom("MyRoom", "MyScene", 4, true, ("setting1", "value1"), ("setting2", "value2"));

// Join the room
RoomClient.JoinRoom("MyRoom");

Exiting a room:

// Exit the current room
RoomClient.ExitRoom();

Retrieving room custom data:

// Get custom data value by name
string settingValue = RoomClient.GetRoomCustomData("setting1");

RoomClient simplifies client-side interactions with rooms, providing a straightforward API for common operations such as creating, joining, and exiting rooms.

Scene Manager


ReflectiveSceneManager is a static class designed to manage scene-related operations in a flexible and event-driven manner.

How to Use:

  1. Load a Scene:

    • Call LoadScene with the name of the scene to load.

    • Optionally, provide a callback function to be executed upon completion.

    • Subscribe to OnSceneLoaded event to be notified when the scene has finished loading.

  2. Unload a Scene:

    • Call UnLoadScene with the scene to unload.

    • Optionally, provide a callback function to be executed upon completion.

    • Subscribe to OnSceneUnloaded event to be notified when the scene has finished unloading.

  3. Initialization:

    • The class automatically initializes the SceneProcessor based on the configured RoomLoaderType during runtime.

Example Usage:

Loading a scene:

// Load a scene named "GameScene"
ReflectiveSceneManager.LoadScene("GameScene", loadedScene =>
{
    // Code to execute after the scene is loaded
    Debug.Log("Scene loaded: " + loadedScene.name);
});

Unloading a scene:

// Assuming currentScene is the scene to unload
ReflectiveSceneManager.UnLoadScene(currentScene, unloadedScene =>
{
    // Code to execute after the scene is unloaded
    Debug.Log("Scene unloaded: " + unloadedScene.name);
});

ReflectiveSceneManager simplifies the process of loading and unloading scenes in Unity, providing an event-driven approach for handling completion events and allowing customization of the scene loading strategy.

Scene Processor


The Scene Processor system is responsible for managing scene loading and unloading operations in Unity. It utilizes the Factory pattern to create different processors based on the specified room loader type. The processors, implemented as SceneProcessor classes, handle the actual loading and unloading tasks.

How to Use:

  1. Scene Processor Factory:

    • Call SceneProcessorFactory.Create with the desired RoomLoaderType to get an instance of SceneProcessor.

  2. Scene Loading/Unloading:

    • Call LoadScene or UnLoadScene on the obtained SceneProcessor instance.

    • Optionally provide a callback function to execute upon completion.

    • Subscribe to ReflectiveSceneManager.OnSceneLoaded or ReflectiveSceneManager.OnSceneUnloaded events for additional notifications.

  3. Processing Scenes:

    • Call the Process method on the SceneProcessor instance to start processing scene loading tasks.

    • Use the GetLoadingState method to check the loading state.

Example Usage:

Creating a Scene Processor:

// Create a SceneProcessor based on the room loader type
var processor = SceneProcessorFactory.Create(RoomLoaderType.AdditiveScene);

Loading a Scene:

// Load a scene named "GameScene"
processor.LoadScene("GameScene", loadedScene =>
{
    // Code to execute after the scene is loaded
    Debug.Log("Scene loaded: " + loadedScene.name);
});

Unloading a Scene:

// Assuming currentScene is the scene to unload
processor.UnLoadScene(currentScene, unloadedScene =>
{
    // Code to execute after the scene is unloaded
    Debug.Log("Scene unloaded: " + unloadedScene.name);
});

Processing Scenes:

// Start processing scene loading tasks
yield return processor.Process();

The Scene Processor system provides a modular and extensible solution for handling scene-related operations in Unity, accommodating different loading strategies based on the room loader type.

Scene Loader


The Scene Loader system provides a simplified interface for initiating scene loading and unloading operations in Unity. It utilizes a MonoBehaviourHook to start coroutines for processing scene-related tasks.

How to Use:

  1. Initialization:

    • The SceneLoader initializes by creating a MonoBehaviourHook if not already present.

  2. Loading a Scene:

    • Call the LoadScene method to initiate the loading process.

    • The method checks if the scene processor is already loading a scene.

    • If not, it starts the coroutine to process scene-related tasks asynchronously.

  3. Unloading a Scene:

    • Call the UnloadScene method to initiate the unloading process.

    • The method checks if the scene processor is already loading a scene.

    • If not, it starts the coroutine to process scene-related tasks asynchronously.

Example Usage:

Loading a Scene:

// Call the LoadScene method to initiate scene loading
SceneLoader.LoadScene();

Unloading a Scene:

// Call the UnloadScene method to initiate scene unloading
SceneLoader.UnloadScene();

Benefits:

  • Provides a centralized interface for scene loading and unloading.

  • Simplifies the initiation of scene-related tasks.

  • Ensures that scene loading is not triggered while another loading operation is in progress.

Considerations:

  • Developers need to ensure that the ReflectiveSceneManager.Processor is set before using the SceneLoader.

  • Initialization occurs automatically when using the LoadScene or UnloadScene methods.

The Scene Loader system abstracts the complexities of starting coroutines and managing the initialization of required components, offering a straightforward approach to scene-related operations in Unity.

Physic Simulator


The Physics Simulator system provides a flexible and modular approach to handling physics simulations in Unity. It consists of a PhysicSimulator component and a set of simulator classes for 2D and 3D physics. The system is designed to be network-aware, allowing for server-only or client-only physics simulations.

How to Use:

  1. PhysicSimulator Component:

    • Attach the PhysicSimulator component to a GameObject in the scene.

    • Configure the component in the Inspector, setting the _isOnlyServer flag if needed.

  2. Initialization:

    • The PhysicSimulator component automatically initializes based on the physics mode and network context.

    • The factory class creates the appropriate Simulator instance.

  3. Physics Updates:

    • The FixedUpdate method of the associated Simulator is automatically called during the FixedUpdate phase.

    • Physics simulations are executed based on the selected physics mode (2D or 3D).

Example Usage:

Benefits:

  • Modular design allows easy extension for different physics modes.

  • Network-aware, enabling server-only or client-only physics simulations.

  • Centralized factory class simplifies the creation of simulator instances.

Considerations:

  • Developers need to ensure that the physics scene is appropriately set up in Unity.

  • The _isOnlyServer flag can be used to enable physics simulations only on the server or client.

The Physics Simulator system provides a convenient way to handle physics simulations in Unity, offering flexibility for different physics modes and network contexts.

Utilities


Network Spawn Utilities


The NetworkSpawnUtilities class provides a set of utility methods for spawning networked GameObjects in Unity using the Unity Networking system. These methods simplify the process of instantiating and synchronizing objects across the network, and they also include functionality for spawning objects in specific scenes.

How to Use:

  1. SpawnObject Methods:

    • Call one of the SpawnObject methods to instantiate and spawn a networked GameObject.

    • Pass the GameObject prefab to be spawned and optional parameters such as parent, position, rotation, and network connection.

  2. SpawnObjectForScene Methods:

    • Similar to SpawnObject but also moves the spawned object to a specified scene.

  3. GetSpawnablePrefabs:

    • Call GetSpawnablePrefabs to retrieve a collection of spawnable prefabs from the "SpawnablePrefabs" resource folder.

    • Useful for dynamically loading available prefabs for spawning.

Example Usage:

// Spawn a prefab across the network
var spawnedObject = NetworkSpawnUtilities.SpawnObject(prefab);

// Spawn a prefab with specific position and rotation
var spawnedObject = NetworkSpawnUtilities.SpawnObject(prefab, new Vector3(1, 2, 3), Quaternion.identity);

// Get a list of spawnable prefabs
var spawnablePrefabs = NetworkSpawnUtilities.GetSpawnablePrefabs();

Benefits:

  • Simplifies the process of spawning networked objects in Unity.

  • Handles the synchronization of spawned objects across the network.

  • Provides a convenient way to organize and retrieve spawnable prefabs.

Considerations:

  • Ensure that the NetworkManager is set up and running in the scene.

  • The "SpawnablePrefabs" resource folder should contain prefabs that can be instantiated across the network.

The NetworkSpawnUtilities class streamlines the spawning of networked GameObjects, contributing to a more efficient and organized Unity networking workflow.

Room List Utilities


The RoomListUtility class provides utility methods for managing and updating a list of rooms in a networked multiplayer game. These methods focus on converting room information, updating, adding, and removing rooms from a list, and sending corresponding messages to notify clients about room changes.

How to Use:

  1. ConvertToRoomList:

    • Call ConvertToRoomList to convert a Room object to a RoomInfo object.

  2. UpdateRoomToList:

    • Call UpdateRoomToList to update an existing room in the list with new information.

  3. AddRoomToList:

    • Call AddRoomToList to add a new room to the list.

  4. RemoveRoomToList:

    • Call RemoveRoomToList to remove a room from the list.

Example Usage:

// Convert a Room to RoomInfo
var roomInfo = RoomListUtility.ConvertToRoomList(existingRoom);

// Update an existing room in the list
RoomListUtility.UpdateRoomToList(roomList, updatedRoom);

// Add a new room to the list
RoomListUtility.AddRoomToList(roomList, newRoom);

// Remove a room from the list
RoomListUtility.RemoveRoomToList(roomList, removedRoom);

Benefits:

  • Simplifies the process of managing and updating a list of rooms in a multiplayer game.

  • Provides a standardized way to convert room information for communication with clients.

Considerations:

  • Ensure that the RoomMessageUtility class is appropriately implemented to handle room update messages.

The RoomListUtility class facilitates the management of room lists in a networked game, making it easier to handle updates, additions, and removals of rooms, and ensuring proper communication with connected clients.

Room Message Utilities


The RoomMessageUtility class provides utility methods for sending room-related messages in a networked multiplayer game. It includes methods to send room update messages to all connected clients and to send specific room messages to individual clients.

How to Use:

  1. SendRoomUpdateMessage:

    • Call SendRoomUpdateMessage when you want to notify all connected clients about a change in the room list.

    • Provide the RoomInfo representing the updated room and the RoomMessageState indicating the type of change (add, update, remove).

  2. SendRoomMessage:

    • Call SendRoomMessage when you want to send a specific room-related message to an individual client.

    • Provide the NetworkConnection of the target client and the ClientRoomState representing the specific state.

Example Usage:

// Notify all clients about a room update
var updatedRoomInfo = new RoomInfo { /* ... */ };
RoomMessageUtility.SendRoomUpdateMessage(updatedRoomInfo, RoomMessageState.Update);

// Send a specific room-related message to a client
var clientConnection = /* obtain the NetworkConnection of the target client */;
var clientRoomState = ClientRoomState.PlayerJoined;
RoomMessageUtility.SendRoomMessage(clientConnection, clientRoomState);

Benefits:

  • Provides a centralized utility for sending room-related messages, promoting consistency in message construction and delivery.

  • Simplifies the process of notifying clients about changes in the room list or sending specific room-related messages.

Considerations:

  • Ensure that the RoomListChangeMessage and ClientRoomMessage classes are correctly implemented and that the message types align with the expected behavior in the client application.

  • The implementation assumes the usage of the Unity Networking framework (NetworkServer and NetworkConnection). Adjustments may be needed based on the networking library in use.

The RoomMessageUtility class serves as a convenient tool for handling room-related communication in a networked multiplayer game, offering methods to broadcast room updates to all clients or send specific messages to individual clients.

Last updated