unity网络编程

Server.js:
  
var connectPort : int = 2224;

function OnGUI ()
{
    if (Network.peerType == NetworkPeerType.Disconnected)
    {
        GUILayout.Label("Connection status: Disconnected");
  
        connectToIP = GUILayout.TextField(connectToIP, GUILayout.MinWidth(100));
        connectPort = parseInt(GUILayout.TextField(connectPort.ToString()));
  
        if (GUILayout.Button("Start Server"))
        {
            Network.InitializeServer(32, connectPort, false);//********
        }
    }
    else
    {
        if (Network.peerType == NetworkPeerType.Server)
        {
            GUILayout.Label("Connection status: Server!");
            GUILayout.Label("Connection: " + Network.connections.length);
            if (Network.connections.length >= 1)
            {
                GUILayout.Label("Ping to first player: " + Network.GetAveragePing(Network.connections[0]));
            }
        }
  
        if (GUILayout.Button("Disconnect"))
        {
            Network.Disconnect(200);
        }
    }
}


// Server functions called by Unity
function OnPlayerConnected(player: NetworkPlayer) {
    Debug.Log("Player connected from: " + player.ipAddress + ":" + player.port);
}


function OnServerInitialized() {
    Debug.Log("Server initialized and ready");
}


function OnPlayerDisconnected(player: NetworkPlayer) {
    Debug.Log("Player disconnected from: " + player.ipAddress + ":" + player.port);
}




Client.js:

var connectToIP : String = "127.0.0.1";
var connectPort : int = 2224;


function OnGUI ()
{
    if (Network.peerType == NetworkPeerType.Disconnected)
    {
        GUILayout.Label("Connection status: Disconnected");
  
        connectToIP = GUILayout.TextField(connectToIP, GUILayout.MinWidth(100));
        connectPort = parseInt(GUILayout.TextField(connectPort.ToString()));
  
        if (GUILayout.Button("Connect as client"))
        {
            Network.Connect(connectToIP, connectPort);//******
        }
    }
    else
    {
        if (Network.peerType == NetworkPeerType.Connecting)
        {
            GUILayout.Label("Connection status: Connecting");
        }
        else if (Network.peerType == NetworkPeerType.Client)
        {
            GUILayout.Label("Connection status: Client!");
            GUILayout.Label("Ping to server: " + Network.GetAveragePing(Network.connections[0]));
        }
  
        if (GUILayout.Button("Disconnect"))
        {
            Network.Disconnect(200);
        }
    }
}


// Client functions called by Unity
function OnConnectedToServer()
{
    Debug.Log("This CLIENT has connected to a server"); 
}


function OnDisconnectedFromServer(info : NetworkDisconnection)
{
    Debug.Log("This SERVER OR CLIENT has disconnected from a server");
}


function OnFailedToConnect(error: NetworkConnectionError)
{
    Debug.Log("Could not connect to server: " + error);
}










   

你可能感兴趣的:(unity网络编程)