您的位置:首页 > 移动开发 > Unity3D

Unity NetworkManager整理160616

2016-06-16 22:16 519 查看
最近都在学习unity Network的知识,思绪凌乱,整理了些常用的方法和构造函数

一、添加NetworkManager组件

1、NetworkManager.StartClien():成为客户端

2、NetworkManager.StartServer():成为服务器

3、NetworkManager.StartHost():成为主机(既是服务器也是客户端)

4、ClientScene.RegisterPrefab():在客户端中注册预设体

5、NetworkManager.OnServerAddPlayer:添加玩家到服务器中,此方法也可以重载

例如:

public virtual void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
var player = (GameObject)GameObject.Instantiate(playerPrefab, playerSpawnPos,
Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId); }


6、NetworkManager.StartMatchmaker():开始Matchmaker

二、定制自己的函数

1、在服务器/主机可以调用的

// called when a client connects
public virtual void OnServerConnect(NetworkConnection conn);

// called when a client disconnects
public virtual void OnServerDisconnect(NetworkConnection conn)
{
NetworkServer.DestroyPlayersForConnection(conn);
}

// called when a client is ready
public virtual void OnServerReady(NetworkConnection conn)
{
NetworkServer.SetClientReady(conn);
}

// called when a new player is added for a client
public virtual void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
var player = (GameObject)GameObject.Instantiate(playerPrefab, playerSpawnPos,             Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}

// called when a player is removed for a client
public virtual void OnServerRemovePlayer(NetworkConnection conn, short playerControllerId)
{
PlayerController player;
if (conn.GetPlayer(playerControllerId, out player))
{
if (player.NetworkIdentity != null && player.NetworkIdentity.gameObject != null)
NetworkServer.Destroy(player.NetworkIdentity.gameObject);
}
}

// called when a network error occurs
public virtual void OnServerError(NetworkConnection conn, int errorCode);


2、在客户端可以调用的

// called when connected to a server
public virtual void OnClientConnect(NetworkConnection conn)
{
ClientScene.Ready(conn);
ClientScene.AddPlayer(0);
}

// called when disconnected from a server
public virtual void OnClientDisconnect(NetworkConnection conn)
{
StopClient();
}

// called when a network error occurs
public virtual void OnClientError(NetworkConnection conn, int errorCode);

// called when told to be not-ready by a server
public virtual void OnClientNotReady(NetworkConnection conn);


3、在Matchmaker可以调用的

// called when a match is created
public virtual void OnMatchCreate(CreateMatchResponse matchInfo)

// called when a list of matches is received
public virtual void OnMatchList(ListMatchResponse matchList)

// called when a match is joined
public void OnMatchJoined(JoinMatchResponse matchInfo)


这些在官方文档里都有

Unity新手,仅作记录作用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity Network