纳金网

标题: Unity3D网络连接 — Make Connection! [打印本页]

作者: yeu1233    时间: 2012-8-29 10:11
标题: Unity3D网络连接 — Make Connection!
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("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("ing 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("layer connected from: " + player.ipAddress + ":" + player.port);

}
function OnServerInitialized() {

Debug.Log("Server initialized and ready");

}
function OnPlayerDisconnected(player: NetworkPlayer) {

Debug.Log("layer 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("ing 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);

}

---------------------------------------------------------------------------------------------------------
把 ClientScene 放到 webplayer 上,然后 ServerScene 在 unity editor 里面直接跑。然后就可以本机测试了。

在 ServerScene 跑之前,设置 Edit => Project Settings => Player 中的 Run In Background。因为默认处于 inactive window 状态的 Unity程序会去 sleep,如果不设置 "Run In Background",则在操作 ClientScene 时,你会发现连接不上服务器,但点一下 ServerScene,然后再看 ClientScene 就连接上了。
Unity 对网络事件的封装还是很到位的。

不过目前看起来,其不允许你用 Unity 的客户端连接自己的服务器。这个还要继续研究。




欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5