Unity 6.3
0 онлайн 55 гостей 3 в системе
Вход
Введение в сетевое взаимодействие многопользовательских игр Глава 4 из 11 Оригинал, стр. 22

Сетевые решения Unity

Unity networking solutions

Unity provides comprehensive solutions for developing multiplayer games across various genres and scales. Selecting the right netcode solution is crucial because different game types have specific networking demands. Consider several factors here, including the game genre, scale, level of competitiveness, and the desired control over the networking layer. Casual games often prioritize simplicity and cost-effectiveness, while competitive games require precise and robust network management to ensure fairness and responsiveness. Whether you are building a casual cooperative game or a competitive action title, Unity offers powerful netcode packages and complementary services to meet your needs.

Netcode for GameObjects For casual cooperative multiplayer games, Unity recommends using the Netcode for GameObjects (NGO) package. This high-level solution simplifies developing multiplayer games by abstracting networking logic, making it easier to manage the game state for all players. If you’re just getting started with multiplayer development, NGO serves as an excellent starting point. Netcode for GameObjects includes a few key features: —

NetworkObject: This represents any object in the game that should be synchronized over the network. It handles object spawning, despawning, and ownership.

NetworkBehaviour: This is a specialized MonoBehaviour that provides networking capabilities. It offers built-in callbacks for handling network events and allows you to write server-side and client-side code.

Remote procedure calls (RPCs): These send messages and invoke methods on remote instances of your GameObjects using Server and Client RPCs.

NetworkVariable: This is a class that allows you to sync states across the network.

NetworkManager: This is a central component that manages the network state of your game, handling tasks such as connection, disconnection, and scene management.

These components work at the application layer to help you implement multiplayer functionality. Netcode for GameObjects is designed to be simple yet powerful, providing the tools needed to create robust and scalable multiplayer games.

Netcode for Entities Built on top of Unity’s Data-Oriented Technology Stack (DOTS) and the Entity Component System (ECS), Netcode for Entities is designed for server-authoritative gameplay. It includes advanced features like client-side prediction, interpolation, and lag compensation. In this setup, a centralized server handles all game simulations, reducing cheating by controlling game outcomes. Clients send inputs to the server, which processes these inputs and sends back the updated game state, minimizing potential manipulation. Netcode for Entities includes client-side prediction to mitigate latency, creating a smoother, near zero-lag experience. Compared to Netcode for GameObjects, it offers better scalability and bandwidth optimization. If you’re an experienced multiplayer developer with a project that requires a high degree of performance and determinism, DOTS and ECS might be the right base for your game. To learn more about data-oriented design in Unity read our DOTS e-book and reference this DOTS resources list.

The Unity sample ECS Network Racing is built with Netcode for Entities.

Choosing the right Netcode solution depends on your project’s requirements and your team’s expertise. You can start by using the table below to help inform your decision. Feature

Netcode for GameObjects

Netcode for Entities

Target audience

Beginners and intermediate developers

Advanced developers

Architecture

Object-oriented (MonoBehaviour-based)

Data-oriented (Entity Component System ECS and DOTS)

Performance

Suitable for smaller scale games

Optimized for high performance and scalability

Scalability

Limited, ideal for a small number of players

High, designed for large-scale games

Networking features

NetworkVariables, RPCs, NetworkTransform, limited client-side prediction (anticipation), interpolation, supports UnityTransport

Full featured clientside prediction, interpolation, lag compensation, supports optimized UnityTransport

Unity Services Integration

Full support for Lobby, Relay, etc.

Full support for Lobby, Relay, etc.

Sample projects

Boss Room, Bite Size Samples

Megacity Metro, ECS Racing, Netcode Samples

Another great resource is Unity Multiplayer Center. The Multiplayer Center provides a starting point to create a multiplayer game. It recommends Unity multiplayer packages based on the needs of your game, and gives you access to samples and tutorials to help you use them. See the Multiplayer Center documentation for instructions on how to get and set up the package in your project.

Unity Transport The Unity Transport Package is a netcode-agnostic library that provides a low-level network layer focused on performance and reliability – a modern, secure, and portable transport library that extends the conventional UDP with advanced features: —

Reliability: Adds reliable communication over UDP, ensuring critical messages are delivered without the overhead of TCP

Security: Incorporates encryption and authentication to protect data from unauthorized access

Performance: Optimized for low latency and high throughput

Cross-platform compatibility: Designed to work seamlessly across different platforms and devices

Both Netcode for GameObjects and Netcode for Entities rely by default on Unity Transport. Developers looking to keep fine-grain control over the network can also use Unity Transport as a standalone library and build their own customized netcode on top for specific game needs. Unity Transport now adds WebGL support that can enhance the cross-platform and web multiplayer experience.

Third-party networking Though we recommend starting with Netcode for GameObjects, it’s important to understand it’s not your only option. The Unity community features many different solutions to fit your specific needs. Here are some third-party networking options available from the Unity Asset Store: —

Photon Unity Networking (PUN): Photon offers a comprehensive and scalable solution suitable for games demanding high concurrent user numbers.

Mirror: Known for its simplicity and ease of use, Mirror is a free open-source networking library originally based on Unity’s UNet.

DarkRift Networking 2: This solution provides both high performance and flexibility and is tailored for developers looking for detailed control over their networking.

Forge Networking Remastered: This is an open-source option that allows for advanced customization and control.