查看: 1524|回复: 0
打印 上一主题 下一主题

Unity3D网络连接 — Make Connection!

[复制链接]

435

主题

2

听众

6371

积分

高级设计师

Rank: 6Rank: 6

纳金币
6372
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-8-29 10:11:51 |只看该作者 |倒序浏览
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 的客户端连接自己的服务器。这个还要继续研究。
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-13 19:53 , Processed in 0.085028 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部