Tutorial 1: Connect & Disconnect
Let's get you going!
• Open the very first tutorial scene: The scene can be found at “Tutorial 1/Tutorial_1”. This
scene consists of one camera, one gameobject with the Connect script attached to it and one
gameobject to display the scenes title.
• Build a webplayer and run it.
• Start the scene in the editor as well and click on “Start a server”
(using the default values for IP and port)
• Click on “Connect as client” in the webplayer.
• You should now see “Connection status: Client!” and
“Connection status: Server!” on your two instances.
Congratulations, you have connected!
This was all very easy; luckily the code is not much harder. Have a look at the file “Tutorial
1/Connect.js” in your favorite editor. All code that has been used in this tutorial can be found in the
OnGUI(); function, have a look at this function and ensure you understand how it works. The code
should be pretty much self explanatory, however, we'll deal with the most important parts briefly.
The two variables at the top of the script (connectToIP and connectPort) are used for capturing the
input from the GUI field, these values are used when pressing the button to connect. The GUI
function is divided in four parts; For servers, connected clients, connecting clients and for
disconnected clients. We use the provided networking status “Network.peerType” to easily check
our current connection status. We call the Network.Connect function to connect clients to servers,
this function takes IP, port and optionally a password as arguments. TO start a server a similarly
easy function is called: Network.InitializeServer. This takes a port and maximum allowed number
of connections as argument. Do note that you can always lower the maximum numbers connection
on a running server, but you can never set it higher than the value you used when initializing. You
need to keep one more setting in mind before connecting to a server or when initializing a server,
that is the “Network.useNat” bit you see just above the corresponding connection/initializing code.
NAT connection (Network.useNat)
We have set Network.useNat to false since we do not want to use Network Adress
Translation. NAT is useful for clients behind a router (inside a LAN). This networking
demo should only be run inside a LAN; you won't be able to connect to your friends
house (unless you or he/she has a very unrestrictive firewall/router). For more
information about NAT see: http://unity3d.com/support/documentation/Components/net-MasterServer.html
Now, on to the last bit of code in the file; The +/- 10 functions that are automatically called by
Unity. It is important to note that you don't need any of these functions anywhere in your code if
you don't want to use them, you can remove them all and this demo will still work. The first 6 client
and server functions should be very self explanatory; They are called on only the client(s) or only
the servers. If you want to use the parameter passed by the functions, checkout the unity manual
entries for these functions.
The last three functions are different. OnFailedToConnectToMasterServer is called on a client
when you somehow can't connect to the masterserver, more details about the masterserver will
follow later on. OnNetworkInstantiate is called on instantiated objects, we'll also have a look at
M2H unity networking tutorial 4