A local multiplayer networking library for making games like Spaceteam in Unity (5.1+). Tested on iOS, Android, and Mac but probably runs on other Unity platforms. Hopefully it will help other developers make more local multiplayer games!
The library is designed for local multiplayer (LAN only) games where you play with other people in the same room. The main idea is to allow "one button" connections where you just hit Play and the game automatically connects with other games around it. No IP addresses, connection dialogs, or decisions about Hosting or Joining.
It's currently built on top of the Unity Networking HLAPI (High Level API) introduced in Unity 5.1, specifically the NetworkDiscovery
and NetworkLobbyManager NetworkManager
classes. CaptainsMess only handles the initial discovery and connection. After that you should use the built-in Unity Networking features to communicate between devices like SyncVars, Commands, and ClientRpcs.
It's not finished yet. Please help if you can!
So far it only supports Wifi. I may have to drop down to the Low Level API to add Bluetooth support.
(For more background, see my blog post: http://www.sleepingbeastgames.com/blog/the-spaceteam-networking-post/)
System.deviceUniqueIdentifier
. I now use a different system which is not technically unique to the device. It will be regenerated if the app is uninstalled/reinstalled. This is fine for normal connections but won't work if you actually want to track unique devices (eg. Spaceteam has achievements for number of players met)FinishGame
API call. Call it when the game is over so you can go back to the lobby with the same players.serverScore
. This may be used in the future to pick the server based on device performance, eg. to prefer the "fastest" device as the server.OnServerCreated
callback (eg. for spawning initial network objects)OnLeftLobby
callback is called properlyOnStopConnecting
callbackserverScore
, ensuring that it will be picked first)AutoConnect()
starts joining or hosting as appropriate. This is what Spaceteam does when you dial "Play".StartHosting()
starts hosting a game as the serverStartJoining()
starts joining any servers it finds, but will not hostCancel()
aborts the connection or disconnect from a lobbyPlayers()
returns a list of all the connected playersLocalPlayer()
returns the player associated with the local deviceAreAllPlayersReady()
returns true if all players have marked themselves readyCountdownTimer()
returns the remaining time in the countdown (or 0 if the countdown is not running)IsConnected()
returns true if connected to other devicesIsHost()
returns true if this device is the host/serverStartLocalGameForDebugging()
is the same as StartHosting but temporarily sets minPlayers to 1 so that you can start a single-player gameFinishGame()
call this when your game is over if you want to return to the lobby with the same playersForceServer(bool forceServer)
give the device a high 'serverScore' so that it will be preferred when choosing a serverSendReadyToBeginMessage()
Tell the server that this player is readySendNotReadyToBeginMessage()
Tell the server that this player is NOT readyOnClientReady(bool readyState)
Called when the player's ready state changesOnClientEnterLobby()
Called when the player enters a lobbyOnClientExitLobby()
Called when the player exits a lobbyOnServerCreated()
(Server only) Called after the server has started so you can do additional setup (eg. spawn network objects)OnStartGame(List<CaptainsMessPlayer> aStartingPlayers)
(Server only) Called when all players are ready and the countdown has finished. This is when your game should take over do whatever it needs to.OnAbortGame()
(Server only) Called if any player disconnects after the game has started. You should end your game at this point. At the moment there is no support for disconnecting/reconnecting while a game is in progress.OnCountdownStarted()
(Server only) Called when all players have marked themselves readyOnCountdownCancelled()
(Server only) Called if any player stops being ready during the countdownOnJoinedLobby()
Called when the player joins a lobbyOnLeftLobby()
Called when the player leaves a lobby or is disconnectedOnStartConnecting()
You can override this if you want to show feedback that the game has started the connection processOnStopConnecting()
You can override this if you want to show feedback that the game has stopped the connection processOnReceivedBroadcast()
You can override this if you want to show feedback about the network trafficOnDiscoveredServer()
You can override this if you want to show feedback about the network traffic.Please check it out and let me know if you have any problems or if you can help improve it!
I've created a discussion thread on my forum here: http://spaceteamadmiralsclub.com/forum/discussion/218/captainsmess-first-release#latest
And if you want to support me please consider joining the Spaceteam Admiral's Club.
Thanks,