※ Unity 5.0 版本以上,network 參數被捨棄,請改寫成 GetComponent<Network>().RPC(......) 或者 gameObjcet.AddComponent<Network>().RPC(......)。
C# Server(Network Unity C# ) :
using UnityEngine;
using System.Collections;
public class TestServer : MonoBehaviour {
public int port;
string Message="";
Vector2 scrollPostition;
Vector2 scrollPostition;
void OnGUI(){
switch(Network.peerType){
case NetworkPeerType.Disconnected:
StartServer();
break;
case NetworkPeerType.Server:
OnServer();
break;
case NetworkPeerType.Client:
Debug.Log("Client Line");
break;
case NetworkPeerType.Connecting:
Debug.Log("Test Line");
break;
}
}
void StartServer(){
if(GUILayout.Button("Start Server")){
//Create Server, but Client ten Clinet line;
NetworkConnectionError error = Network.InitializeServer(10, port, false);
//line Error Message;
Debug.Log("Error" + error);
}
}
void OnServer(){
GUILayout.Label ("Create server, Wait client line......");
int lenght = Network.connections.Length;
//Client quantity;
for(int i=0; i < lenght; i++){
GUILayout.Label("Lineing client ID: "+i);
GUILayout.Label("Lineing client IP: "+Network.connections[i].ipAddress);
GUILayout.Label("Lineing client Port: "+Network.connections[i].port);
}
if(GUILayout.Button("Over server")){
Network.Disconnect();
//Return Message;
Message = "";
}
//Create scroll view, Message;
scrollPostition = GUILayout.BeginScrollView (scrollPostition, GUILayout.Width(200), GUILayout.Height(Screen.height));
//Message view
GUILayout.Box (Message);
//Client Posistion Message;
GUILayout.EndScrollView ();
}
[RPC]
// Client talk message;
void RequestMessage(string message, NetworkMessageInfo info){
Message += "\n" + "Clinet: " + info.sender + " message: " +message;
}
}
----------------------------------------
C# Client(Network Unity C# ) :
using UnityEngine;
using System.Collections;
public class TestClient : MonoBehaviour {
public string IP;
public float speed = 10f, rosistionSpeed = 10f;
int port = 10000;
string InputMessage = "Talk View", Message="";
Vector2 scrollPosistion;
GameObject mainPlay, clientPlay;
void Start(){
mainPlay = GameObject.Find ("Cube");
clientPlay = GameObject.Find ("Cube1");
}
void OnGUI(){
switch (Network.peerType) {
case NetworkPeerType.Disconnected:
StartConnect ();
break;
case NetworkPeerType.Server:
Debug.Log ("Client to server line");
break;
case NetworkPeerType.Client:
OnClient ();
break;
case NetworkPeerType.Connecting:
Debug.Log ("Test connecting");
break;
}
}
void StartConnect(){
if(GUILayout.Button("Start Game")){
NetworkConnectionError error = Network.Connect(IP,port);
Debug.Log("Line Message: "+error);
}
}
void OnClient(){
scrollPosistion = GUILayout.BeginScrollView (scrollPosistion, GUILayout.Width (200), GUILayout.Height (Screen.height));
GUILayout.Box (Message);
GUILayout.BeginHorizontal ();
InputMessage = GUILayout.TextArea (InputMessage);
if (GUILayout.Button ("Input Talk")) {
networkView.RPC("RequestMessage", RPCMode.AllBuffered, InputMessage);
}
GUILayout.EndHorizontal ();
if (GUILayout.Button ("Exit Game")) {
Network.Disconnect ();
Message = "";
}
GUILayout.EndScrollView ();
}
[RPC]
void RequestMessage(string message, NetworkMessageInfo info){
Message += "\n" + "Client: " + info.sender + ":" + message;
}
}
結果圖:
沒有留言:
張貼留言