M2H_Networking_Tutorial.pdf
(
735 KB
)
Pobierz
203814176 UNPDF
By Mike Hergaarden - Last edit: 29-10-2009
Content
About this tutorial.................................................................................................................................2
About the author...................................................................................................................................2
How to use this tutorial?.......................................................................................................................3
Tutorial 1: Connect & Disconnect........................................................................................................4
Tutorial 2: Sending messages...............................................................................................................6
Our very first multiplayer scene..just one player though!...............................................................6
Tutorial 2A: Server plays, client observes, no instantiating............................................................6
Tutorial 2B: Server and client(s) play, with instantiating................................................................9
Tutorial 3: Authoritative servers........................................................................................................10
Further network subjects explained....................................................................................................12
Real life examples..............................................................................................................................15
Example 1: Chatscript....................................................................................................................15
Example 2: Masterserver example.................................................................................................15
Example 3: Lobby system .............................................................................................................16
Example 4: FPS game....................................................................................................................17
Tips.....................................................................................................................................................18
M2H unity networking tutorial
1
About this tutorial
I always thought Unity needed a proper networking tutorial. When I started unity networking the
networking example was a bit too confusing; A proper networking example should have
independent examples so you know where to look for something within seconds. With that in mind
I decided to join the UniKnowledge contest and finally give the community a networking tutorial,
that I hope, is all you need to create a networked game.
This tutorial features many examples; from small (but important) techniques to a real FPS game.
You are advised to read this document from start to end, but if you are picking things up quickly
you can have a look at the examples yourself and fall back to this document whenever you need
more details.
About the author
This tutorial is written by Mike Hergaarden (“Leepo”) from M2H
(
www.m2h.nl
)
. We've been using Unity for over two years now, though
most of our unity development time has been spent in the past few months.
We've been concentrating on unity multiplayer since the very beginning. In
fact the first unity game we made featured multiplayer; it's that easy to add! Our multiplayer games
include: Crashdrive 3D, Cratemania, Surrounded by Death, Verdun Online and our current
upcoming multiplayer challenge is Hyberon.
Have fun going trough this tutorial. If you made something exciting thanks to this tutorial, let me
know!
M2H unity networking tutorial
2
How to use this tutorial?
Combined with this document is a zip file containing the unity project which is used in this tutorial.
It is assumed you have already made yourself familiar with the unity editor and basic scripting, if
not: check out the unity (video) tutorials.
Multiplayer isn't much fun to debug since you need to have two instances of your game running
(server and client). During this tutorial you're advised to run the server in the editor game view, and
a client in a webplayer.
In case you want to use the tutorial assets in a project of your own, do mind that one projects setting
has been modified in this tutorial project. For your own projects ensure the option “Run in
background” has been turned on to be able to run a server in the background without it going to
sleep. This will keep the servers game running in the background. Otherwise you would not be able
to connect to your server when running the client in the foreground. You can find this option at
“Edit → Project settings → Player” .
M2H unity networking tutorial
3
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
this later.
OnSerializeNetworkView
is one of the two methods for us to send messages across the
server and clients.
RPC
calls are network messages/functions you define yourself. In the next
tutorial we'll have a look at serialization and RPC calls.
To conclude this tutorial have a look at the Network.* “Messages Sent”, “Class Variables” and
“Class Functions“ here:
http://unity3d.com/support/documentation/ScriptReference/Network.html
Now you know where to find all this information in the manual as a reference. We've already
briefly discussed about 75% of the information over there!
M2H unity networking tutorial
5
Plik z chomika:
ElKoziRRo
Inne pliki z tego folderu:
UnityTerrainBump.rar
(1 KB)
TerrainReplacement(additive).shader
(6 KB)
CustomTerrainScript.cs
(2 KB)
FloatingX_v1.2.zip
(979 KB)
3D Tower Defense Kit.unitypackage
(68950 KB)
Inne foldery tego chomika:
- bajki słuchowiska
Pliki dostępne do 01.06.2025
Pliki dostępne do 19.01.2025
Pliki dostępne do 21.01.2024
!! AutoMapa 6.19 Europa 1602 EU FINAL (INSTALATOR CRACKED)
Zgłoś jeśli
naruszono regulamin