2015年4月6日 星期一

Unity Server / Client


Unity Server Script (c#) :

using UnityEngine;
using System.Collections;

public class Server : MonoBehaviour {

public string IP = "Server IP";
public int Port = Server Port;

void OnGUI(){
if(Network.peerType == NetworkPeerType.Disconnected){
if(GUI.Button(new Rect(Screen.width/Screen.width, Screen.height/Screen.height+120f,100, 50), "Start Server")){
Network.InitializeServer(10, Port);
}
}else{
if(Network.peerType == NetworkPeerType.Server){

GUI.Label(new Rect(Screen.width/Screen.width,Screen.height/Screen.height, 100, 50), "Server");
GUI.Label(new Rect(Screen.width/Screen.width, Screen.height/Screen.height+50f, 100, 50), "Connection Clinet Number: "+ Network.connections.Length);

if(GUI.Button(new Rect(Screen.width/Screen.width, Screen.height/Screen.height+100f,100, 50), "Logout")){
Network.Disconnect(250);
}
}
}
}
}

---------------------------

Unity Client Script (c#) :

using UnityEngine;
using System.Collections;

public class Client : MonoBehaviour {

public string IP = "Server IP";
public int Port = Server Port;
void OnGUI(){
if(Network.peerType == NetworkPeerType.Disconnected){
if(GUI.Button(new Rect(Screen.width/Screen.width, Screen.height/Screen.height,100, 50), "Start Clinet")){
Network.Connect(IP, Port);
}
}else{
if(Network.peerType == NetworkPeerType.Client){
GUI.Label(new Rect(Screen.width/Screen.width,Screen.height/Screen.height, 100, 50), "Clinet");
if(GUI.Button(new Rect(Screen.width/Screen.width, Screen.height/Screen.height + 50f,100, 50), "Logout")){
Network.Disconnect(250);
}
}
}
}
}

---------------------

結果圖 :

    Server Start:


Client Login :







沒有留言:

張貼留言