- Home /
Offline match in a multiplayer game
Hello, please look at the following question by marcelop:
http://answers.unity3d.com/questions/1090412/unet-is-it-possible-to-use-the-same-scene-for-onli.html
For my game, I have done what seanr suggested, and it works very nicely! My only concern is that the network server is still listening to a port, which is unnecessary. Is it possible to do what seanr said, but without occupying any ports?
Thank you for reading :)
Answer by Wolfrik_Creations · May 27, 2016 at 09:23 PM
I have my multiplayer game's singleplayer setting set to where it hosts a server with a max of 1 player and a host ID that is different so other people can't see it.
That's the only workaround I can think of.
Thank you for your answer! I am still new to many concepts of networking, and I have to ask: What is host ID exactly?
A host ID is how the game knows what game you're playing, or else all Unity games would be reading each other's servers.
For instance if you had one game coded to InitializeServer with the host ID of "$$anonymous$$yGame" and then another game that initialized "$$anonymous$$yGame2" then they wouldn't see each other's servers, but if they both had "$$anonymous$$yGame" then they'd see each other's servers; I'm not sure if you could connect or not, though.
Here's something you could do for starting a server and starting a singleplayer match, just make the buttons run these functions. var serverPort : int; var hostID : String = "UniqueGameID"; var myServerName : String = "$$anonymous$$ake a GUI box to change this."
function StartServer() {
Network.InitializeServer(maxPlayers, serverPort, !Network.HavePublicAddress);
$$anonymous$$asterServer.RegisterHost(hostID, myServerName, System.String.Empty);
}
function StartServer_Singeplayer() {
Network.InitializeServer(1, serverPort, !Network.HavePublicAddress);
$$anonymous$$asterServer.RegisterHost(hostID+" sp", myServerName, System.String.Empty);
}
And my probably sad attempt at C#, this may not work:
public integur serverPort;
public string hostID = "UniqueGameID";
public string myServerName = "$$anonymous$$ake a GUI box to change this."
void StartServer() {
Network.InitializeServer(maxPlayers, serverPort, !Network.HavePublicAddress);
$$anonymous$$asterServer.RegisterHost(hostID, myServerName, System.String.Empty);
}
void StartServer_Singeplayer() {
Network.InitializeServer(1, serverPort, !Network.HavePublicAddress);
$$anonymous$$asterServer.RegisterHost(hostID+" sp", myServerName, System.String.Empty);
}
These are both untested but the UnityScript one is an edited version of $$anonymous$$e so it should work.
Answer by SweatyChair · Oct 31, 2016 at 08:57 AM
My solution similar to @Wolfrik_Creations, but in the new UNet.
I have a MatchGameManager class overriding UNet's NetworkManager:
public class MatchGameManager : NetworkManager
{
public void StartSingle ()
{
maxConnections = -1;
// Just make sure match making is stopped
if (matchMaker != null)
StopMatchMaker ();
// Just make sure no server is running
if (!IsClientConnected () && !NetworkServer.active && matchMaker == null)
StartHost ();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612081730im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Multiplayer on Android? 0 Answers
[Unity Multiplayer] Unet matchmaker stopped finding matches suddenly 2 Answers
Simulating a loss of internet connection 0 Answers