Unity Tutorials
Basic Multi-player
///// Networking script /////
@script ExecuteInEditMode() // makes GUI show in edit mode
var gameName : String = "AnAuthenticTestName"; // make the name authentic to reduce chance of error
var refreshing = false;
var hostData : HostData[]; // a list of all current hosts
var playerPrefab : GameObject; // your player
var create = false;
var joining = false;
var serverName = "";
var serverInfo = "";
var serverPass = "";
var playerName = "";
var clientPass = "";
var scrollPosition : Vector2 = Vector2.zero;
function Start(){
playerName = PlayerPrefs.GetString("Player Name"); // loads your previosuly used player names
}
function OnGUI () {
if(!Network.isClient && !Network.isServer) { // if you arent the server or a client
if(!create && !joining){
if (GUI.Button(Rect(Screen.width/2 - 50,Screen.height/2,100,20),"Create Game")) {
create = true;
}
if (GUI.Button(Rect(Screen.width/2 - 50,Screen.height/2 + 30,100,20),"Find Game")) {
joining = true;
refreshHostList();
}
}
if (create){
if (GUI.Button(Rect(Screen.width/2 - 50,Screen.height/3 + 110,100,50),"Create")) {
startServer();
}
GUI.Label(Rect (Screen.width/2 - 110,Screen.height/3,100,20),"Server Name:");
GUI.Label(Rect (Screen.width/2 + 40,Screen.height/3,100,20),"Password:");
GUI.Label(Rect (Screen.width/2 - 30,Screen.height/2 + 90,100,20),"Server Info:");
serverName = GUI.TextField (Rect (Screen.width/2 - 120,Screen.height/3 + 30,100,20), serverName, 12);
serverPass = GUI.PasswordField (Rect (Screen.width/2 + 20,Screen.height/3 + 30,100,20), serverPass, "*"[0], 12);
serverInfo = GUI.TextArea (Rect (Screen.width/2 - 70,Screen.height/2 + 120,150,40), serverInfo, 35);
if (GUI.Button(Rect(Screen.width/1.2,Screen.height/20,100,20),"Back")) {
create = false;
}
}
if (joining){
if(hostData) {
scrollPosition = GUI.BeginScrollView (Rect (Screen.width/4,Screen.height/6,Screen.width/1.5,Screen.height/2),scrollPosition, Rect (0, 0, 300, 1000/hostData.length * 30/));
GUI.Label(Rect(30,0,100,20),"Game Name");
GUI.Label(Rect(350,0,100,20),"Server Info");
GUI.Label(Rect(590,0,100,20),"Player Count");
GUI.Label(Rect(700,0,100,20),"Password");
for(var i:int = 0; i < hostData.length; i++) {
GUI.Label(Rect(0,30 + i * 30,200,22),hostData[i].gameName);
GUI.Label(Rect(160,30 + i * 30,500,22),hostData[i].comment);
GUI.Label(Rect(610,30 + i * 30,100,20),hostData[i].connectedPlayers + " / " + hostData[i].playerLimit);
if (hostData[i].passwordProtected){
clientPass = GUI.PasswordField (Rect (680,30 + i * 30,100,25), clientPass, "*"[0], 12);
}
if (GUI.Button(Rect(800,30 + i * 30,100,25),"Join")) {
Network.Connect(hostData[i], clientPass);
}
}
GUI.EndScrollView ();
}
if(!hostData){
GUI.Label(Rect(Screen.width/2 - 50,Screen.height/3,200,25),"No Games Found");
if (GUI.Button(Rect(Screen.width/2 - 50,Screen.height/3 + 30,105,25),"Refresh")) {
refreshHostList();
}
}
if (GUI.Button(Rect(Screen.width/1.2,Screen.height/20,100,20),"Back")) {
joining = false;
}
}
if (GUI.Button(Rect(Screen.width/20,Screen.height/20,100,20),"Quit")) {
Application.Quit();
}
GUI.Label(Rect(Screen.width/2 - 35,Screen.height/1.2 - 30,100,20),"Your Name:");
playerName = GUI.TextField (Rect (Screen.width/2 - 50,Screen.height/1.2,100,20), playerName, 12);
}
}
function Update () {
if(refreshing) {
if(MasterServer.PollHostList().Length > 0) {
refreshing = false;
hostData = MasterServer.PollHostList();
}
}
}
function startServer () {
if (serverPass != ""){
Network.incomingPassword = serverPass;
}
Network.InitializeServer(15,25001, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, serverName, serverInfo);
}
function OnServerInitialized () {
DontDestroyOnLoad (transform.gameObject);
Application.LoadLevel ("Lobby");
lobbySpawn();
}
function OnConnectedToServer () {
lobbySpawn();
}
function lobbySpawn(){
yield WaitForSeconds(0.1);
var made = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
made.GetComponent(playerMove).playerName = playerName;
PlayerPrefs.SetString("Player Name", playerName);
if(Network.isClient){
Destroy(this);
}
}
function refreshHostList () {
MasterServer.ClearHostList();
MasterServer.RequestHostList(gameName);
refreshing = true;
}
///// NetworkView script /////
function Awake() {
if (!networkView.isMine){
GetComponentInChildren(Camera).enabled = false;
GetComponent(MouseLook).enabled = false;
GetComponent(playerMove).enabled = false;
}
}
///// Basic movement script (with RPC's)/////
#pragma strict
var playerName : String;
function Update () {
if(Input.GetButton("w")){
networkView.RPC("forward",RPCMode.AllBuffered);
}
if(Input.GetButton("a")){
networkView.RPC("left",RPCMode.AllBuffered);
}
if(Input.GetButton("s")){
networkView.RPC("back",RPCMode.AllBuffered);
}
if(Input.GetButton("d")){
networkView.RPC("right",RPCMode.AllBuffered);
}
}
function OnGUI () {
GUI.Label(Rect(Screen.width/2,Screen.height/2,100,20),"X");
}
@RPC
function forward(){
transform.Translate(Vector3.forward / 10);
}
@RPC
function left(){
transform.Translate(Vector3.left / 10);
}
@RPC
function back(){
transform.Translate(Vector3.back / 10);
}
@RPC
function right(){
transform.Translate(Vector3.right / 10);
}
Authoritative Networking JS
////////NetworkManager Script:
var player : Transform;
var registeredName : string = "somekindofuniquename";
var refreshRequestLength : float = 3.0f;
var hostData : HostData[];
var chosenGameName : string = "";
var myPlayer : NetworkPlayer;
function StartServer(){
Network.InitializeServer (16, Random.Range(2000,2500),!Network.HavePublicAddress());
MasterServer.RegisterHost(registeredName,chosenGameName);
}
function OnServerInitialized(){
if(Network.isServer){
myPlayer = Network.player;
makePlayer(myPlayer);
}
}
function OnConnectedToServer() {
myPlayer = Network.player;
networkView.RPC("makePlayer", RPCMode.Server, myPlayer);
}
@RPC
function makePlayer(NetworkPlayer : thisPlayer){
var newPlayer : Transform = Network.Instantiate (player, transform.position, transform.rotation, 0) as Transform;
if (thisPlayer != myPlayer) {
networkView.RPC ("enableCamera", thisPlayer, newPlayer.networkView.viewID);
}
else{
enableCamera(newPlayer.networkView.viewID);
}
}
@RPC
function enableCamera(NetworkViewID : playerID){
var players : GameObject[];
players = GameObject.FindGameObjectsWithTag ("Player");
for(var thisPlayer : GameObject in players){
if(thisPlayer.networkView.viewID == playerID){
thisPlayer.GetComponent(Movement).haveControl = true;
var myCamera : Transform = thisPlayer.transform.Find("Camera");
myCamera.camera.enabled = true;
myCamera.camera.GetComponent(AudioListener).enabled = true;
break;
}
}
}
function RefreshHostList (){
MasterServer.RequestHostList (registeredName);
var timeEnd : float = Time.time + refreshRequestLength;
while (Time.time < timeEnd) {
hostData = MasterServer.PollHostList();
yield WaitForEndOfFrame();
}
}
function OnGUI(){
if (Network.isClient || Network.isServer) {
return;
}
if(chosenGameName == ""){
GUI.Label(new Rect(Screen.width/2 - Screen.width/10, Screen.height/2 - Screen.height/20, Screen.width/5,Screen.height/20), "Game Name");
}
chosenGameName = GUI.TextField(new Rect(Screen.width/2 - Screen.width/10, Screen.height/2 - Screen.height/20, Screen.width/5,Screen.height/20), chosenGameName, 25);
if (GUI.Button (new Rect (Screen.width/2 - Screen.width/10, Screen.height/2, Screen.width/5,Screen.height/10), "Start New Server")) {
StartServer();
}
if (GUI.Button (new Rect (Screen.width/2 - Screen.width/10, Screen.height/2 + Screen.height/10, Screen.width/5,Screen.height/10), "Find Servers")) {
RefreshHostList();
}
if (hostData != null) {
for(var i = 0; i < hostData.Length; i++){
if(GUI.Button (new Rect (Screen.width/2 - Screen.width/10, Screen.height/2 + ((Screen.height/20)*(i+4)), Screen.width/5,Screen.height/20), hostData[i].gameName)) {
Network.Connect(hostData[i]);
}
}
}
}
//////////// Movement Script
var moveSpeed : int = 8;
var horiz : float = 0;
var vert : float = 0;
var haveControl : boolean = false;
function FixedUpdate(){
if(haveControl){
vert = Input.GetAxis("Vertical");
horiz = Input.GetAxis("Horizontal");
var newVelocity : Vector3 = (transform.right * horiz * moveSpeed) + (transform.forward * vert * moveSpeed);
var myVelocity : Vector3 = rigidbody.velocity;
myVelocity.x = newVelocity.x;
myVelocity.z = newVelocity.z;
if(myVelocity != rigidbody.velocity){
if(Network.isServer){
movePlayer(myVelocity);
}
else{
networkView.RPC("movePlayer", RPCMode.Server, myVelocity);
}
}
}
}
@RPC
function movePlayer(Vector3 : playerVelocity){
rigidbody.velocity = playerVelocity;
networkView.RPC("updatePlayer", RPCMode.OthersBuffered, transform.position);
}
@RPC
function updatePlayer(Vector3 : playerPos){
transform.position = playerPos;
}
}
var player : Transform;
var registeredName : string = "somekindofuniquename";
var refreshRequestLength : float = 3.0f;
var hostData : HostData[];
var chosenGameName : string = "";
var myPlayer : NetworkPlayer;
function StartServer(){
Network.InitializeServer (16, Random.Range(2000,2500),!Network.HavePublicAddress());
MasterServer.RegisterHost(registeredName,chosenGameName);
}
function OnServerInitialized(){
if(Network.isServer){
myPlayer = Network.player;
makePlayer(myPlayer);
}
}
function OnConnectedToServer() {
myPlayer = Network.player;
networkView.RPC("makePlayer", RPCMode.Server, myPlayer);
}
@RPC
function makePlayer(NetworkPlayer : thisPlayer){
var newPlayer : Transform = Network.Instantiate (player, transform.position, transform.rotation, 0) as Transform;
if (thisPlayer != myPlayer) {
networkView.RPC ("enableCamera", thisPlayer, newPlayer.networkView.viewID);
}
else{
enableCamera(newPlayer.networkView.viewID);
}
}
@RPC
function enableCamera(NetworkViewID : playerID){
var players : GameObject[];
players = GameObject.FindGameObjectsWithTag ("Player");
for(var thisPlayer : GameObject in players){
if(thisPlayer.networkView.viewID == playerID){
thisPlayer.GetComponent(Movement).haveControl = true;
var myCamera : Transform = thisPlayer.transform.Find("Camera");
myCamera.camera.enabled = true;
myCamera.camera.GetComponent(AudioListener).enabled = true;
break;
}
}
}
function RefreshHostList (){
MasterServer.RequestHostList (registeredName);
var timeEnd : float = Time.time + refreshRequestLength;
while (Time.time < timeEnd) {
hostData = MasterServer.PollHostList();
yield WaitForEndOfFrame();
}
}
function OnGUI(){
if (Network.isClient || Network.isServer) {
return;
}
if(chosenGameName == ""){
GUI.Label(new Rect(Screen.width/2 - Screen.width/10, Screen.height/2 - Screen.height/20, Screen.width/5,Screen.height/20), "Game Name");
}
chosenGameName = GUI.TextField(new Rect(Screen.width/2 - Screen.width/10, Screen.height/2 - Screen.height/20, Screen.width/5,Screen.height/20), chosenGameName, 25);
if (GUI.Button (new Rect (Screen.width/2 - Screen.width/10, Screen.height/2, Screen.width/5,Screen.height/10), "Start New Server")) {
StartServer();
}
if (GUI.Button (new Rect (Screen.width/2 - Screen.width/10, Screen.height/2 + Screen.height/10, Screen.width/5,Screen.height/10), "Find Servers")) {
RefreshHostList();
}
if (hostData != null) {
for(var i = 0; i < hostData.Length; i++){
if(GUI.Button (new Rect (Screen.width/2 - Screen.width/10, Screen.height/2 + ((Screen.height/20)*(i+4)), Screen.width/5,Screen.height/20), hostData[i].gameName)) {
Network.Connect(hostData[i]);
}
}
}
}
//////////// Movement Script
var moveSpeed : int = 8;
var horiz : float = 0;
var vert : float = 0;
var haveControl : boolean = false;
function FixedUpdate(){
if(haveControl){
vert = Input.GetAxis("Vertical");
horiz = Input.GetAxis("Horizontal");
var newVelocity : Vector3 = (transform.right * horiz * moveSpeed) + (transform.forward * vert * moveSpeed);
var myVelocity : Vector3 = rigidbody.velocity;
myVelocity.x = newVelocity.x;
myVelocity.z = newVelocity.z;
if(myVelocity != rigidbody.velocity){
if(Network.isServer){
movePlayer(myVelocity);
}
else{
networkView.RPC("movePlayer", RPCMode.Server, myVelocity);
}
}
}
}
@RPC
function movePlayer(Vector3 : playerVelocity){
rigidbody.velocity = playerVelocity;
networkView.RPC("updatePlayer", RPCMode.OthersBuffered, transform.position);
}
@RPC
function updatePlayer(Vector3 : playerPos){
transform.position = playerPos;
}
}
Subscribe to:
Posts (Atom)