MultiplayerTutorial.pdf

(2206 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Unity Multiplayer Tutorial
For using Networking in Unity.
Author: Andrius Kuznecovas
Last Revision: 24-MAR-2010
873142759.034.png 873142759.035.png 873142759.036.png 873142759.037.png
Contents
1. Introduction
3
2. Getting Started
3
3. Creating Your First Client / Server Application
4
3.1 Preparing Scene
4
3.2 Creating Scripts & Adding Components
5
3.2.1 Server & Client
5
3.2.2 Scene & Objects Instantiation On The Network
8
3.3 Quick Overview
10
4. Implementing Multiplayer in Startrooper
11
4.1 Changing Scene
11
4.2 Integration
11
4.2.1 Integrating Objects & Scene
11
4.2.2 Server & Client
21
4.3.3 Final Polishing
37
4.3 Quick Overview
38
873142759.001.png
Multiplayer Tutorial
Welcome, Unity User.
Here you will learn
advanced usage of Unity
Networking.
Have a great time and we
hope that this tutorial will
be helpful.
1. Introduction
The aim of this tutorial is to show the essence of Unity Networking. We will show
how to create basic and advanced network applications using Master Server/Client,
UDP Server/Client and Direct Connect. In this tutorial we will use Unity iPhone 1.6,
iPhone 3GS and the StarTrooper demo from the Unity web site.
2. Getting Started
There are a few things that you should know before you start.
You will learn how to:
Use basic and advanced components of networking.
Create a server and client.
Use the Master Server.
Use Direct Connect.
Use UDP Broadcast Server.
Create a simple scene on the network.
Convert the StarTrooper game to multiplayer.
873142759.002.png 873142759.003.png 873142759.004.png 873142759.005.png 873142759.006.png 873142759.007.png 873142759.008.png 873142759.009.png
 
873142759.010.png 873142759.011.png 873142759.012.png
Use other Unity components and more.
You should have:
Unity iPhone 1.6.
iPhone or iPod.
Knowledge about networks and how they work.
Advanced C# and JavaScript skills.
Basic Unity skills. You can find more information here .
Important notes:
Unity supports .NET 1.1 and 2.1.
You can disable or enable Networking in: Edit -> Project Settings -> Player
-> Enable Unity Networking.
Unity Networking supports wifi, 3G and GSM connections.
You can connect between diferent types of Unity targets. For example you
can connect from Unity on the desktop to Unity iPhone, from Unity Web
Player to Unity iPhone, etc.
3. Creating Your First Client / Server Application
In this chapter, we will cover the basics needed to create a simple multiplayer ap-
plication. We will create our first example featuring moving objects on the network
and basic handshaking between the client and server. The example uses all basic
multiplayer components such as: Network and NetworkView . We will use Direct
Connect for connection between client and server.
3.1 Preparing Scene
Now let’s start with a simple scene. Your first steps:
Create a new Project.
Create a new Prefab and name it Player: Assets -> Create -> Prefab.
Create a new Cube GameObject: GameObject -> Create other -> Cube.
Drag your Cube from the Hierarchy to your Player Prefab in the Project and
then delete your Cube from the scene.
4
873142759.013.png 873142759.014.png 873142759.015.png 873142759.016.png 873142759.017.png 873142759.018.png 873142759.019.png 873142759.020.png 873142759.021.png 873142759.022.png 873142759.023.png 873142759.024.png 873142759.025.png 873142759.026.png 873142759.027.png
Create a new Plane and name it Ground: GameObject -> Create other ->
Plane. Your Plane parameters should be: Position (0,0,0), Rotation (0,0,0),
Scale (5,5,5).
Create a Directional Light: GameObject -> Create other -> Directional Light.
Light parameters: Position (0,15,0), Rotation (25,0,0), Scale (1,1,1) Shadows
-> Type -> Soft Shadows.
Finally, save your scene and name it MainGame: File -> Save Scene.
Everything should look like this image:
3.2 Creating Scripts & Adding Components
Next you will learn how to create the server and client, instantiate the scene and
objects on the network, move objects, and connect everything together.
3.2.1 Server & Client
We will start with the most important — the creation of Server and Client:
Create a new JavaScript file and name it ConnectionGUI: Assets -> Create ->
JavaScript.
Add this file (by dragging) to the Main Camera object in the Hierarchy, then
open the file and create some variables:
var remoteIP = "127.0.0.1";
var remotePort = 25000;
var listenPort = 25000;
var useNAT = false;
var yourIP = "";
var yourPort = "";
5
873142759.028.png 873142759.029.png 873142759.030.png 873142759.031.png 873142759.032.png 873142759.033.png
Zgłoś jeśli naruszono regulamin