Skip to content

This repo represents the ideal starting point for new MULTIPLAYER NETCODE FOR GAME OBJECTS (NGO) Unity projects. It includes best practices for project structure and C# coding standards.

License

Notifications You must be signed in to change notification settings

SamuelAsherRivello/unity-multiplayer-ngo-2d-playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Unity Multiplayer Playground 2D

Featuring 2D via Unity's Netcode For Game Objects (NGO).

YouTube Video Overview

TLDR;

Table Of Contents

  1. Getting Started
  2. Overview
  3. Details
  4. Resources

Getting Started

  1. Download this repo (*.zip or *.git)
  2. Download the Unity Editor (See Unity Version below)
  3. Open this repo in the Unity Editor
  4. Choose one of the Scenes...
  • For single-player:
    • Play the scene
    • Use arrow keys to move
    • Enjoy!
  • For multiplayer:
    • Set only that one Scene in the build settings. Build the app. (See Unity Target below)
    • Open the first build instance. Click 'H' for host
    • Open 1 or more other build instances. Click 'C' for client
    • Use arrow keys to move
    • Enjoy!

Overview

Features

Starting Point

This repo started as a fork of my own Github.com/SamuelAsherRivello/unity-project-template project. It contains "Unity Best Practices for Project Structure and C# Coding Standards". It is a recommended starting point for new projects.

Ending Point

This repo includes various versions of similar gameplay. There is a playable character and a crate in a playful environment. Use the arrow keys to move.

The demos required 8-10 hours total, from initial planning through completion of development.

Notes

The client-authoritative approach represents a quick and dirty way to get started and prototype, while the server-authoritative approach represents the real-world solution path for a commercial multiplayer game. But I like the academic challenge of exploring both at the same time, adding features, and keeping feature parity. For future prototypes, I'll likely use only server-authoritative.

Apparently, the "com.unity.netcode" package creates a "DefaultNetworkPrefabs" asset. No problem. However, its created at the root of the Assets folder, and it cannot be safely moved to a subfolder. I don't like that.

Challenges

I enjoyed iterating on the game versions from single-player to server-authoritative multiplayer, but there were some challenges along the way.

  1. Choosing to create multiple versions at once introduced some organizational and structural challenges. I first tried to keep each version's assets completely distinct. But that proved to be too much of a redundancy (e.g., copies of Sprites). So, I chose the Game folder vs Shared folder approach. Works well.
  2. Between the multiplayer versions, it would be intuitive to have my custom classes extend each other (e.g. PlayerCA.cs and PlayerSA.cs extend some non-existent BasePlayer.cs), but I chose to keep the custom class structure flatter and simply duplicate logic via copy/paste. This way, each game version is (more) self-contained.
  3. The QuickLobby is purposefully lean. It has some bugs when (very rapidly) joining/quitting with many app instances.
  4. The QuickLobby is purposefully limited. It supports multiple app instances running on only one PC. A more robust version would allow each player to be on a unique PC. Using Unity's optional services (e.g. Matchmaking, Lobby, Relay) would help.
  5. I notice bugs if I spawn an app instance and leave it open, then continue to do more development and play the newer Unity Editor vs. the older app instances. I think the NetworkObject IDs of the crate, for example, fall out of sync. Rebuilding all app instances solves the issue. I'd like to know more about workarounds to understand exactly when rebuilding a new app instance is really needed and when it's not.

Possible Improvements

With infinite time, some additional improvements can be made. Some would be especially fun.

  1. I'd like to have a unique character appearance per app instance. Solution path, likely: Don't automatically spawn a player prefab. Instead capture the player connecting and spawn a unique prefab per player. Using a base player prefab and per-character player prefab variants could help the workflow.
  2. I'd like to show all player's scores onscreen for all app instances. Solution path, likely: Have the host listen to a NetworkVariable change for the score for all clients, and then concatenate the results for display onscreen.
  3. I'd like to make a build tool that when I hit Control-B to build, it automatically puts the currently active Scene as the FIRST index in the build settings. This would facilitate multiplayer testing so the active Scene in the Unity Editor matches the active Scene in the built app.

Bonus Content

The supporting material (video & slides) required additional creation time beyond the timeline for the content above.

Details

Documentation

  • ReadMe.md - The primary documentation for this repo
  • Unity/Assets/Documentation/ReadMe.asset - Select this asset in Unity to get started

Configuration

Structure

  • Unity - Open this folder in the Unity Editor
  • Unity/Assets/Game/ - Several game versions exist. Open any Scene. Press 'Play'!
  • Unity/Assets/Shared/ - These are shared assets used across several game versions.

Dependencies


Resources

Links

Best Practices

Unity Web3

Unity Courses

Created By

  • Samuel Asher Rivello
  • Over 25 years XP with game development (2024)
  • Over 11 years XP with Unity (2024)

Contact

License

Provided as-is under MIT License | Copyright ™ & © 2006 - 2024 Rivello Multimedia Consulting, LLC

About

This repo represents the ideal starting point for new MULTIPLAYER NETCODE FOR GAME OBJECTS (NGO) Unity projects. It includes best practices for project structure and C# coding standards.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages