您的位置:首页 > 理论基础 > 计算机网络

Unity&网络--HLAPI(2):Unity HLAPI NetworkManager与玩家NetworkBehaviour的各回调函数的调用时间序列

2017-04-27 16:14 309 查看
非原创的就不多说了,画了个回调函数的时间轴。在HLAPI这套框架里,player身上继承自Networkbehaviour的脚本与场景中唯一的NetworkManager组件是插入游戏逻辑的重要入口,同时NetworkManager也是Unity HLAPI的核心组件,吃透它们的回调函数的逻辑是快速掌握与使用这套API的关键。

时间轴简单说明:

第一层(2):左边是客户端,右边是Host主机(服务器+客户端)。第二层(6):每边都有客户端player,主机player,NetworkManager三个重要角色。第三层:各回调函数。为加强理解,也把Start,OnEnable等Monobehaviour的message调用函数放了进去。

点此看大图



点此看大图

各回调函数的说明:

官网上都可以查到,Host的函数最全,在这里罗列出来(没有退出阶段的):

NetworkBehaviour(Host)

顺序

OnStartServer

OnStartClient

OnRebuildObservers

OnStartAuthority

OnStartLocalPlayer

(Start() function is called)

OnSetLocalVisibility

说明

NetworkBehaviour.OnStartServer

This is invoked for NetworkBehaviour objects when they become active on the server.

This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.

This will be called for objects on a “host” as well as for object on a dedicated server.

NetworkBehaviour.OnStartClient

Called on every NetworkBehaviour when it is activated on a client.

Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.

NetworkBehaviour.OnRebuildObservers

Returns

bool Return true if this function did work.

Description

Callback used by the visibility system to (re)construct the set of observers that can see this object.

Implementations of this callback should add network connections of players that can see this object to the observers set.

NetworkBehaviour.OnStartAuthority

This is invoked on behaviours that have authority, based on context and NetworkIdentity.localPlayerAuthority.

This is called after OnStartServer and OnStartClient.

When NetworkIdentity.AssignClientAuthority is called on the server, this will be called on the client that owns the object. When an object is spawned with NetworkServer.SpawnWithClientAuthority, this will be called on the client that owns the object.

NetworkBehaviour.OnStartLocalPlayer

Called when the local player object has been set up.

This happens after OnStartClient(), as it is triggered by an ownership message from the server. This is an appropriate place to activate components or functionality that should only be active for the local player, such as cameras and input.

NetworkBehaviour.OnSetLocalVisibility

Callback used by the visibility system for objects on a host.

Objects on a host (with a local client) cannot be disabled or destroyed when they are not visibile to the local client. So this function is called to allow custom code to hide these objects. A typical implementation will disable renderer components on the object. This is only called on local clients on a host.

NetworkManager(Host)

顺序

(Start() function is called)

OnStartHost

OnStartServer

OnServerConnect

OnStartClient

OnMatchCreate

OnClientConnect

OnServerSceneChanged

OnServerReady

OnServerAddPlayer

OnClientSceneChanged

说明:

NetworkManager.OnStartHost

This hook is invoked when a host is started.

StartHost has multiple signatures, but they all cause this hook to be called.

NetworkManager.OnStartServer

This hook is invoked when a server is started - including when a host is started.

StartServer has multiple signatures, but they all cause this hook to be called.

NetworkManager.OnServerConnect

Called on the server when a new client connects.

NetworkManager.OnStartClient

This is a hook that is invoked when the client is started.

StartClient has multiple signatures, but they all cause this hook to be called.

NetworkManager.OnMatchCreate

Callback that happens when a NetworkMatch.CreateMatch request has been processed on the server.

NetworkManager.OnClientConnect

Called on the client when connected to a server.

The default implementation of this function sets the client as ready and adds a player.

NetworkManager.OnServerSceneChanged

Called on the server when a scene is completed loaded, when the scene load was initiated by the server with ServerChangeScene().

NetworkManager.OnServerReady

Called on the server when a client is ready.

The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process.

NetworkManager.OnServerAddPlayer

Called on the server when a client adds a new player with ClientScene.AddPlayer.

The default implementation for this function creates a new player object from the playerPrefab.

————————————————

维护日志:

2017-8-22:更新了标题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐