Featuring 2D via Unity's Netcode For Game Objects (NGO).
YouTube Video Overview |
---|
TLDR;
- Check out the ending point, which took about 8-10 hours total from initial planning through completion of development
- Download and play the server-authoritative demo
- Read the notes, challenges, and possible improvements
Table Of Contents
- Download this repo (*.zip or *.git)
- Download the Unity Editor (See
Unity Version
below) - Open this repo in the Unity Editor
- 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!
- Set only that one Scene in the build settings. Build the app. (See
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.
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.
- Version 01 - Single Player - Simplest playground, without multiplayer
- Version 02 - Multiplayer CA - Intermediate playground, with client authoritative multiplayer
- Version 03 - Multiplayer SA - Advanced playground, with server authoritative multiplayer
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.
I enjoyed iterating on the game versions from single-player to server-authoritative multiplayer, but there were some challenges along the way.
- 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 vsShared
folder approach. Works well. - 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.
- The QuickLobby is purposefully lean. It has some bugs when (very rapidly) joining/quitting with many app instances.
- 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.
- 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.
With infinite time, some additional improvements can be made. Some would be especially fun.
- 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.
- 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.
- 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.
The supporting material (video & slides) required additional creation time beyond the timeline for the content above.
Documentation
ReadMe.md
- The primary documentation for this repoUnity/Assets/Documentation/ReadMe.asset
- Select this asset in Unity to get started
Configuration
Unity Target
- Standalone MAC/PCUnity Version
- Use this VersionUnity Rendering
- Universal Render Pipeline (URP)Unity Aspect Ratio
- Game View 10x16
Structure
Unity
- Open this folder in the Unity EditorUnity/Assets/Game/
- Several game versions exist. Open any Scene. Press 'Play'!Unity/Assets/Shared/
- These are shared assets used across several game versions.
Dependencies
Various
- Already included via Unity Package Manager as this Version
- Samuel Asher Rivello's Medium Article - Unity — Project Structure Best Practices
- Samuel Asher Rivello's Medium Article - Unity — C# Coding Standards
- Samuel Asher Rivello's Web3 Portfolio - SamuelAsherRivello.com/web3-portfolio
- Samuel Asher Rivello's Web3 Article - Unity — Web3 For Game Development
- Samuel Asher Rivello's Web3 Repos - Intro To Web3 For Unity (Students Only)
- Samuel Asher Rivello's Udemy Course - Physics For Unity
- Samuel Asher Rivello's Udemy Course - Unit Testing For Unity
- Samuel Asher Rivello's Udemy Course - MVC Architecture For Unity
- Samuel Asher Rivello
- Over 25 years XP with game development (2024)
- Over 11 years XP with Unity (2024)
- Twitter - @srivello
- Git - Github.com/SamuelAsherRivello
- Resume & Portfolio - SamuelAsherRivello.com
- LinkedIn - Linkedin.com/in/SamuelAsherRivello <--- Say Hello! :)
License
Provided as-is under MIT License | Copyright ™ & © 2006 - 2024 Rivello Multimedia Consulting, LLC