====== Lab 8: Networking ====== {{:public:t-vien-14-1:lab8-screen.png?300|}} ===== Goal ===== The goal of this lab is to create a very simple shared virtual environment using Unity's networking support. ===== Preparation ===== * Nothing ===== Documentation ===== * Unity's [[https://docs.unity3d.com/Documentation/Components/NetworkReferenceGuide.html|Networking Reference Guide]] * This lab is based on the [[http://cgcookie.com/unity/2011/12/20/introduction-to-networking-in-unity/|CG Cookie Introduction to Networking]] video tutorial ===== Procedure ===== - **Empty Scene** Make an empty scene with a ground and a directional light. Position the camera such that you have a good view of the ground plane. - **Create Network Manager** Create a new ''GameObject'' (**GameObject->CreateEmpty**) and name it "NetworkManager". Create a new C# script called "Networking.cs" and attach it to this new game object. Paste the following code into this script:using UnityEngine; using System.Collections; public class Networking : MonoBehaviour { public GameObject playerPrefab; public Transform spawnObject; bool initialized = false; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnGUI() { if(!initialized) { if(GUI.Button (new Rect(10,10,100,30),"Start Server")) { Network.InitializeServer(32,2500,true); } if(GUI.Button (new Rect(10,100,100,30),"Start Client")) { Network.Connect ("127.0.0.1",2500); } } } void OnServerInitialized() { initialized = true; Debug.Log ("Server Initialized"); spawnPlayer(); } void OnConnectedToServer() { initialized = true; spawnPlayer(); } void spawnPlayer() { Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity,0); } } - **Create Player** Create a regular cube (**GameObject->CreateOther->Cube**). Change its name to "NetworkPlayer". Add a ''CharacterMotor'' component to it (**Component->Character->CharacterMotor**). Add a ''NetworkView'' component to it (**Component->Miscellaneous->NetworkView**). Create a new C# script called "CharacterMovement.cs" and add the following code to it:using UnityEngine; using System.Collections; public class CharacterMovement : MonoBehaviour { public float speed = 5.0f; public float gravity = 5.0f; private CharacterController cc; // Use this for initialization void Start () { cc = GetComponent(); } // Update is called once per frame void Update () { if(networkView.isMine) { cc.Move (new Vector3(Input.GetAxis ("Horizontal")*speed*Time.deltaTime, -gravity*Time.deltaTime, Input.GetAxis("Vertical")*speed*Time.deltaTime)); } } } - **Turn Player into Prefab** Drag your ''NetworkedPlayer'' object into the project panel to create a Prefab and **delete** the original instance in the Hierarchy view. - **Create a Spawn Point** Create a new empty game object and call it "SpawnPoint". Place it well **above** the plane. - **Finalize Networking** Choose the ''NetworkManager'' and drag the ''NetworkPlayer'' **PreFab** into the ''Player Prefab'' property and drag the ''SpawnObject'' game object into the ''Spawn Object'' property. - **Build Stand Alone** Go to **File->Build Settings...** and press **Player Settings...**. Under **Resolution** check the box marked **Run in Background**. Build a stand alone application and run it. Press **Start Server** in the application. Make sure you can move the box with the arrow keys. Start a second instance of the application but chose **Start Client** this time. Make sure you can move the cube, and see it move in both applications!