Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by agentsmith · Dec 10, 2010 at 10:14 AM · masterserver

How do I connect to the MasterServer using Unity 3.x?

Does anyone have a simple script/test demo that I can use to see if I can register a game to the MasterServer? I've ran demos from Noesis tutorials, M2H tutorials, and Unity's Network Tutorial which I was able to get working in Unity 2.6 but I can't get any of them to work in Unity 3.x.

Any help would be apprciated!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image agentsmith · Dec 10, 2010 at 10:42 AM 0
Share

If a demo isn't available can someone please provide the IP and port? Thanks!

avatar image Anton Petrov · Dec 10, 2010 at 03:09 PM 0
Share

I have run Network Tutorial under Unity 3 without any problems. Just have to fix some compiling warnings.

avatar image agentsmith · Dec 11, 2010 at 06:15 AM 0
Share

Thanks Anton! I was able to get it to work using the other Network Tutorial Unity has (I tried the StarTrooper Networking one under "Tutorials" which I couldn't get to work, but the Network one under "Example section did!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by agentsmith · Dec 11, 2010 at 06:30 AM

Just in case anyone else is having an issue connecting/testing with Unity's Master Server here's my code (not sure how to get it to format correctly):

INSTRUCTIONS:

1) Copy and paste all this code in to a javascript file and attach to a empty gameobject.

2) Change the name of the game

3) Confirm your project "Runs in Background" is checked; Edit->Project Settings->Player->"Runs in Background"

4) Build the game (it should only be the empty game object with this script attached and the "Main Camera")

5) Run multiple instances of the game (one can be the server and the other can be the client)

DontDestroyOnLoad(this);

var gameName = "You must change this";

var serverName : String = "Joe Blow's Game";

var serverTagline : String = "l33t game for all!";

var serverPort = 25002;

var numberOfPlayers : int = 32;

private var playerCount: int = 0;

private var timeoutHostList = 0.0;

private var lastHostListRequest = -1000.0;

private var hostListRefreshTimeout = 10.0;

private var natCapable : ConnectionTesterStatus = ConnectionTesterStatus.Undetermined;

private var filterNATHosts = false;

private var probingPublicIP = false;

private var doneTesting = false;

private var timer : float = 0.0;

private var windowRect = Rect (Screen.width-300,0,300,100);

private var hideTest = false;

private var testMessage = "Detetcting NAT capabilities...";

// Enable this if not running a client on the server machine

//MasterServer.dedicatedServer = true;

function OnFailedToConnectToMasterServer(info: NetworkConnectionError)

{

Debug.Log(info);

}

function OnFailedToConnect(info: NetworkConnectionError)

{

Debug.Log(info);

}

function OnGUI ()

{

windowRect = GUILayout.Window (0, windowRect, MakeWindow, "Server Controls");

}

function Awake ()

{

// Start connection test

natCapable = Network.TestConnection();

// What kind of IP does this machine have? TestConnection also indicates this in the

// test results

if (Network.HavePublicAddress())

 Debug.Log("This machine has a public IP address");

else

 Debug.Log("This machine has a private IP address");

}

function MakeWindow (id : int)

{

var hideNumberOfPlayers = !Network.isServer;

if (!hideNumberOfPlayers)

{

 GUILayout.Label("Number of player's connected: " + playerCount);

}

hideTest = GUILayout.Toggle(hideTest, "Hide test info");

if (!hideTest)

{

 GUILayout.Label(testMessage);

 if (GUILayout.Button ("Retest connection"))

 {

     Debug.Log("Redoing connection test");

     probingPublicIP = false;

     doneTesting = false;

     natCapable = Network.TestConnection(true);

 }

}

if (Network.peerType == NetworkPeerType.Disconnected)

{

 GUILayout.BeginHorizontal();

 GUILayout.Space(10);

 // Start a new server

 if (GUILayout.Button ("Start Server"))

 {

     // Use NAT punchthrough if no public IP present

     var useNat = !Network.HavePublicAddress();



     Network.InitializeServer(numberOfPlayers, serverPort, useNat);

     MasterServer.RegisterHost(gameName, serverName, serverTagline);

 }



 // Refresh hosts

 if (GUILayout.Button ("Refresh available Servers") || Time.realtimeSinceStartup > lastHostListRequest + hostListRefreshTimeout)

 {

     MasterServer.RequestHostList (gameName);

     lastHostListRequest = Time.realtimeSinceStartup;

 }



 GUILayout.FlexibleSpace();



 GUILayout.EndHorizontal();



 GUILayout.Space(5);



 var data : HostData[] = MasterServer.PollHostList();

 for (var element in data)

 {

     GUILayout.BeginHorizontal();



     // Do not display NAT enabled games if we cannot do NAT punchthrough

     if ( !(filterNATHosts))//&& element.useNat) )

     {

         var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;

         GUILayout.Label(name);  

         GUILayout.Space(5);



         var hostInfo = "[";

         // Here we display all IP addresses, there can be multiple in cases where

         // internal LAN connections are being attempted. In the GUI we could just display

         // the first one in order not confuse the end user, but internally Unity will

         // do a connection check on all IP addresses in the element.ip list, and connect to the

         // first valid one.



         for (var host in element.ip)

         {

             hostInfo = hostInfo + host + ":" + element.port + " ";

         }

         hostInfo = hostInfo + "]";

         //GUILayout.Label("[" + element.ip + ":" + element.port + "]"); 

         GUILayout.Label(hostInfo);  

         GUILayout.Space(5);

         GUILayout.Label(element.comment);

         GUILayout.Space(5);

         GUILayout.FlexibleSpace();

         if (GUILayout.Button("Connect"))

         {

             Network.Connect(element.ip, element.port);          

         }

     }



     GUILayout.EndHorizontal();  

 }

}

else

{

 if (GUILayout.Button ("Disconnect"))

 {

     Network.Disconnect();

     MasterServer.UnregisterHost();

 }

 GUILayout.FlexibleSpace();

}

GUI.DragWindow (Rect (0,0,1000,1000));

}

function Update()

{

// If test is undetermined, keep running

if (!doneTesting)

{

 TestConnection();

}

}

function TestConnection()

{

// Start/Poll the connection test, report the results in a label and

// react to the results accordingly

connectionTestResult = Network.TestConnection();

switch (connectionTestResult) {

 case ConnectionTesterStatus.Error: 

     testMessage = "Problem determining NAT capabilities";

     doneTesting = true;

     break;



 case ConnectionTesterStatus.Undetermined: 

     testMessage = "Undetermined NAT capabilities";

     doneTesting = false;

     break;



 case ConnectionTesterStatus.PublicIPIsConnectable:

     testMessage = "Directly connectable public IP address.";

     useNat = false;

     doneTesting = true;

     break;



 // This case is a bit special as we now need to check if we can 

 // circumvent the blocking by using NAT punchthrough

 case ConnectionTesterStatus.PublicIPPortBlocked:

     testMessage = "Non-connectble public IP address (port " + 

         serverPort +" blocked), running a server is impossible.";

     useNat = false;

     // If no NAT punchthrough test has been performed on this public 

     // IP, force a test

     if (!probingPublicIP) {

         connectionTestResult = Network.TestConnectionNAT();

         probingPublicIP = true;

         testStatus = "Testing if blocked public IP can be circumvented";

         timer = Time.time + 10;

     }

     // NAT punchthrough test was performed but we still get blocked

     else if (Time.time > timer) {

         probingPublicIP = false;         // reset

         useNat = true;

         doneTesting = true;

     }

     break;

 case ConnectionTesterStatus.PublicIPNoServerStarted:

     testMessage = "Public IP address but server not initialized, "+

         "it must be started to check server accessibility. Restart "+

         "connection test when ready.";

     break;



 case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:

     testMessage = "Limited NAT punchthrough capabilities. Cannot "+

         "connect to all types of NAT servers.";

     useNat = true;

     doneTesting = true;

     break;



 case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:

     testMessage = "Limited NAT punchthrough capabilities. Cannot "+

         "connect to all types of NAT servers. Running a server "+

         "is ill advised as not everyone can connect.";

     useNat = true;

     doneTesting = true;

     break;



 case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:

     testMessage = "Limited NAT punchthrough capabilities. Cannot "+

         "connect to all types of NAT servers. Running a server "+

         "is ill advised as not everyone can connect.";

     useNat = true;

     doneTesting = true;

     break;



 case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:

 case ConnectionTesterStatus.NATpunchthroughFullCone:

     testMessage = "NAT punchthrough capable. Can connect to all "+

         "servers and receive connections from all clients. Enabling "+

         "NAT punchthrough functionality.";

     useNat = true;

     doneTesting = true;

     break;



 default: 

     testMessage = "Error in test routine, got " + connectionTestResult;

}

if (doneTesting)

{

 if (useNat)

     shouldEnableNatMessage = "When starting a server the NAT "+

         "punchthrough feature should be enabled (useNat parameter)";

 else

     shouldEnableNatMessage = "NAT punchthrough not needed";

 testStatus = "Done testing";

}

}

function OnPlayerConnected(player: NetworkPlayer)

{

playerCount += 1;

Debug.Log("Player " + playerCount +

       " connected from " + player.ipAddress + 

       ":" + player.port);

// Populate a data structure with player information ...

}

function OnPlayerDisconnected(player: NetworkPlayer)

{

playerCount -= 1;

Debug.Log("Clean up after player " + player);

Network.RemoveRPCs(player);

Network.DestroyPlayerObjects(player);

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image agentsmith · Dec 11, 2010 at 06:33 AM 0
Share

Again, sorry about the formatting; this is code from the Network Example and from Unity's documentation. I cleared up all the deprecation warnings and it tested successfully, enjoy!

avatar image mrwhite69 · Jan 20, 2011 at 09:34 PM 0
Share

I don't think It will run correctly, even if the server is hosted on internet, it will work "Locally" , but not correctly between players behind a Nat or a Router...

You are using Network.Connect(element.ip, element.port);

which does not initiate Nat PunchThrough if necessary...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

MasterServer connection in Unity 3 fails 2 Answers

How do I get the MasterServer to work properly? 0 Answers

MasterServer Is Public? 1 Answer

Network instantiate problem 1 Answer

Server Appears in MasterServer for me, but not friend 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges