Skip to content

Instantly share code, notes, and snippets.

@kirillrybin
Created September 8, 2013 20:56
Show Gist options
  • Save kirillrybin/6488334 to your computer and use it in GitHub Desktop.
Save kirillrybin/6488334 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class ClientNetwork : MonoBehaviour {
public string serverIP = "127.0.0.1";
public int port = 25000;
private string _messageLog = "";
string someInfo = "";
private NetworkPlayer _myNetworkPlayer;
void OnGUI() {
if (Network.peerType == NetworkPeerType.Disconnected) {
if (GUI.Button(new Rect(100, 100, 150, 25), "Connect")) {
Network.Connect(serverIP, port);
}
} else {
if (Network.peerType == NetworkPeerType.Client) {
GUI.Label(new Rect(100, 100, 150, 25), "client");
if (GUI.Button(new Rect(100, 125, 150, 25), "Logut"))
Network.Disconnect();
if (GUI.Button(new Rect(100, 150, 150, 25), "SendHello to server"))
SendInfoToServer();
}
}
GUI.TextArea(new Rect(250, 100, 300, 300), _messageLog);
}
[RPC]
void SendInfoToServer(){
someInfo = "Client " + _myNetworkPlayer.guid + ": hello server";
networkView.RPC("ReceiveInfoFromClient", RPCMode.Server, someInfo);
}
[RPC]
void SetPlayerInfo(NetworkPlayer player) {
_myNetworkPlayer = player;
someInfo = "Player setted";
networkView.RPC("ReceiveInfoFromClient", RPCMode.Server, someInfo);
}
[RPC]
void ReceiveInfoFromServer(string someInfo) {
_messageLog += someInfo + "\n";
}
void OnConnectedToServer() {
_messageLog += "Connected to server" + "\n";
}
void OnDisconnectedToServer() {
_messageLog += "Disco from server" + "\n";
}
// fix RPC errors
[RPC]
void ReceiveInfoFromClient(string someInfo) { }
[RPC]
void SendInfoToClient(string someInfo) { }
using UnityEngine;
using System.Collections;
public class ServerNetwork : MonoBehaviour {
private int port = 25000;
private int playerCount = 0;
private string _messageLog = "";
public void Awake() {
if (Network.peerType == NetworkPeerType.Disconnected)
Network.InitializeServer(10, port, false);
}
public void Update() {
}
public void OnGUI() {
if (Network.peerType == NetworkPeerType.Server) {
GUI.Label(new Rect(100, 100, 150, 25), "Server");
GUI.Label(new Rect(100, 125, 150, 25), "Clients attached: " + Network.connections.Length);
if (GUI.Button(new Rect(100, 150, 150, 25), "Quit server")) {
Network.Disconnect();
Application.Quit();
}
if (GUI.Button(new Rect(100, 175, 150, 25), "Send hi to client"))
SendInfoToClient();
}
GUI.TextArea(new Rect(275, 100, 300, 300), _messageLog);
}
void OnPlayerConnected(NetworkPlayer player) {
AskClientForInfo(player);
}
void AskClientForInfo(NetworkPlayer player) {
networkView.RPC("SetPlayerInfo", player, player);
}
[RPC]
void ReceiveInfoFromClient(string someInfo) {
_messageLog += someInfo + "\n";
}
string someInfo = "Server: hello client";
[RPC]
void SendInfoToClient() {
networkView.RPC("ReceiveInfoFromServer", RPCMode.Others, someInfo);
}
// fix RPC errors
[RPC]
void SendInfoToServer() { }
[RPC]
void SetPlayerInfo(NetworkPlayer player) { }
[RPC]
void ReceiveInfoFromServer(string someInfo) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment