Adds NAT punch-through and automatic port-forwarding to any peer-to-peer networking system, including Unity's high-level networking API. Reduces latency and saves you money by connecting players directly whenever possible, even when they are behind a router.
Supports Windows, Linux, and OSX.
Does not support any mobile platforms. Does not support web builds or windows store.
Direct connect whenever possible: Why waste your hard earned money using bandwidth on the relay servers if you don't have to? A direct connection means less lag and less dependence on infrastructure that you don't control.
Seamless connection replacing: Your players will always use the best connection possible, seamlessly switching from relay to direct connections as they become available.
Faster connection: Gets your players in the game faster by not waiting for a UNET match to be created or joined before connecting.
Host Migration: Fully supports UNet's built in Host Migration. No more dropped games, even if the host leaves.
Note: Punch-through requires an externally hosted server to run the included Facilitator on.
Note: Web builds are not supported. Unfortunately this plugin does not work with WebSockets and there is no plan to add WebSocket support is the future. Sorry!
If you wish to use Unity's matchmaking and you haven't already signed up for Unity's Online Services you should do that now. Also be sure to create a project and set up the multiplayer service in Unity.
NAT punch-through requires an external server, often referred to as a Facilitator, in order to broker the connections between peers. The first thing you'll want to do is get the Facilitator up and running.
*Note: If you want to skip the first two steps you can temporarily use my Facilitator for testing. Just leave grabblesgame.com
as your facilitatorIP on the NATHelper component.*
Assets/Plugins/NAT Traversal for UNET/
./Facilitator
Facilitator.exe
Note: If you are connecting to a Linux server via putty and want to be able to logout and keep the Facilitator running, look into screen. *Note: If you are using AWS and having trouble connecting to the Facilitator please see the FAQ
Now that the Facilitator is up and running, you can start connecting some players.
Example
scene that comes with the pluginThat's it! You're done. Ok, well, not quite...let's make sure it's working before we pour the champagne.
Example
scene that is included with the plugin.If your mind is not yet sufficiently blown, take a look at the usage stats for your game on Unity's online services portal. You should notice that the only CCU being taken up is by the hosts. You can connect as many players as you want all day without eating into your CCU or bandwidth at all, as long as they connect directly and not over the relays.
You can use the provided NetworkManager on its own or as the base class for your own game's NetworkManager. For the most part you will only ever need to call StartHostAll() and StartClientAll() but almost all of the internals are exposed so you can do lots of advanced things that I haven't even thought of. You can use the NATHelper class to forward ports and punch holes yourself if you need lower level control but for most uses extending from the included NetworkManager and overriding a couple of methods is the most you will need. You can override OnHolePunchedServer() and OnHolePunchedClient() to be informed when a hole is successfully punched. For most methods that you override in NetworkManager you will want to make sure to call the base method to avoid causing unexpected behaviour. If you get stuck make sure to check out the API documentation.
Punch-through is accomplished with the help of RakNet. Automatic port forwarding is accomplished using Open.NAT. Both upnp and nat pmp are supported. Once the connection is established, all the HLAPI stuff works as normal. RakNet has some good documentation on how punch-through works. The RakNet libraries are included for Windows, Mac, and Linux. The included ExampleNetworkManager requires Unity's matchmaking to work but if you have another way to pass around connection data (like steam lobbies) and you don't care about the relays, then you can still use this plugin without Unity's online services.
The primary motivation for creating this was so that small time developers like myself wouldn't be so dependent on Unity's relay servers. I also needed the fastest connections possible for my studio's own fast-paced game Grabbles and I was not satisfied with the latency introduced by the relay servers when they were first introduced. I think this issue has been mostly resolved but a direct connection is always going to be faster.