Unity&网络--HLAPI(2):Unity HLAPI NetworkManager与NetworkBehaviour的回调函数

虽然当前的Unity游戏服务器模块即将被砍,不过作为当前最主流游戏引擎的一般性通用服务器架构,研究一下还是可以学习学习它的一些思想。

Unity HLAPI既High Level API是相对于Low Level API,它隐藏了网络传输层的细节,更专注于将一些功能性的API暴露给使用者,其中NetworkManager为抽象出的最核心的管理类对象之一,而NetworkBehaviour可看做是MonoBehaviour的网络化版本。就如同我们习惯从MonoBehaviour的OnEnable,Start,Update中写代码,NetworkManager与NetworkBehaviour的回调函数是插入游戏逻辑的重要入口。例如,作为一个玩家的gameObject在一个网络游戏总有可能同时存在于三种不同的进程中,1自己的Client进程,2Host进程,3其他Client的进程。这样我们在设计一个玩家类的时候,根据功能的需求,可以简单的将逻辑分别插入到合适的回调函数中,所以吃透它们的调用顺序与时机是快速掌握与使用这套Unity网络框架的关键。

玩家对象初始化过程分析

下面分析一个在Host/Client结构中两个玩家的初始化生成场景。下面画了一个大致的各相关回调函数调用的时间轴:
在这里插入图片描述
粗略的总结上图中的初始化顺序大致是:
Host上的NetworkManager对象初始化==>Host上的Host NetworkBehaviour初始化==>Client上的NetworkManager被同步==>Client上的Host NetworkBehaviour启动==>Client上的Client 玩家对象初始化+通知Host上的NetworkManager==>Host上的NetworkManger对Client玩家对象进行初始化

以上回调函数都是围绕着几个概念。首先是最基础的Host与Client,Client既是客户端,既是一般玩家,Host是主机可以理解为服务器+客户端,就如同以前局域网CS建主机一样,Host进程不光处理服务器相关事务也负责一个游戏玩家客户端的事务处理。因此,Host与Client玩家相对于服务器会有一些不同,例如通讯,Host玩家与服务器是同进程通讯,Client玩家是网络通讯。(也可以创建Dedicated Server的项目,既是Server只负责服务器事务例如MMORPG类型的服务器)

另外GameObject会分为一般GameObject与玩家GameObject。GameObject还有一个Authority的概念,例如在MMORPG或FPS游戏中,可以将自己的玩家GameObject设为拥有Authority,拥有该GO Authority的客户端可以反向向服务器同步状态,控制自己玩家移动之类的操作可以不必等待服务器通讯延迟时间,增加了玩家体验。没有Authority的GO则是一律由服务端负责向所有客户端同步状态。

为了减少服务端向客户端同步的工作量,每个GameObject还有一个Observers列表,既是可见它的所有客户端,当一个GameObject状态发生变化,服务器只会向能见到它的客户端同步该对象状态,这样在大世界之类的游戏系统中可以大幅降低网络通讯压力。相关的回调有OnRebuildObservers和OnSetLocalVisibility。

Cilent与Host间的同步有一个网络通讯的过程,在帧同步的架构下,HLAPI可以配置Message Send Interval既是帧同步的时间粒度,同步方式有将服务器的对象状态同步到客户端的对象对象,客户端的自己玩家对象对服务器的对应玩家对象进行RPC调用操作等等。

Host的玩家对象的NetworkBehaviour各回调函数

调用顺序 :

OnStartServer
OnStartClient
OnRebuildObservers
OnStartAuthority
OnStartLocalPlayer
(Start() function is called)
OnSetLocalVisibility

所有GO在client和server上的调用请见官网

函数调用时机说明:

OnStartServer

  • called when a GameObject spawns on the server, or when the server is started for GameObjects in the Scene
  • 当GO在服务器中生成时调用

OnStartClient

  • called when the GameObject spawns on the client, or when the client connects to a server for GameObjects in the Scene
  • 当GO在客户端生成时调用

OnSerialize

  • called to gather state to send from the server to clients
  • 当服务器向客户端同步状态时调用

OnDeSerialize

  • called to apply state to GameObjects on clients
  • 客户端同步状态时调用

OnNetworkDestroy

  • called on clients when the server destroys the GameObject
  • 服务器销毁GO时调用

OnStartLocalPlayer

  • called on clients for player GameObjects on the local client (only)
  • 客户端自己玩家对象生成时调用

OnRebuildObservers

  • called on the server when the set of observers for a GameObjects is rebuilt
  • 服务器端GO的observers重建时点欧阳

OnSetLocalVisibility

  • called on the client and/or server when the visibility of a GameObject changes for the local client
  • 客户端或服务器上的GO可视范围发生变化时调用

OnCheckObserver

  • called on the server to check visibility state for a new client
  • 服务器检查新客户端可视范围时调用

函数说明:

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的对象在server上active开启后触发,有可能触发自NetworkServer.Listen()或NetworkServer.Spawn() 。

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的对象在client或host上active开启后触发,该对象触发此方法时,它的所有标注SyncVars特性的成员的状态一定是和server上的对应对象一致的。

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.
  • 每个对象都有一个能看到它的client列表既是observers。当它的observers发生变化时会触发此方法。只在服务器端调用。

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.
  • 只会在具有Authority的对象上触发,在OnStartServer和OnStartClient之后调用。当NetworkIdentity.AssignClientAuthority或NetworkServer.SpawnWithClientAuthority在服务器端调用后,只会在拥有该对象Authority的client上触发该对象方法。

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.
  • 本地玩家对象生成时收到服务器一个拥有权确认的消息后调用,在OnStartClient()之后,这里可以进行自己玩家对象的一些初始化逻辑。

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.
  • 每个对象都有一个能看到它的client列表既是observers。当它的observers发生变化时会触发此方法。在客户端和服务器端调用。

Host的NetworkManager各回调函数

调用顺序:

当host启动时:
(Start() function is called)
OnStartHost
OnStartServer
OnServerConnect
OnStartClient
OnMatchCreate
OnClientConnect
OnServerSceneChanged
OnServerReady
OnServerAddPlayer
OnClientSceneChanged

当有client连接时:
OnServerConnect
OnServerReady
OnServerAddPlayer

当有client断开连接时:
OnServerDisconnect

当host停止时:
OnStopHost
OnStopServer
OnStopClient

在client和server上的调用请见官网

函数说明:

NetworkManager.OnStartHost

  • This hook is invoked when a host is started.
    StartHost has multiple signatures, but they all cause this hook to be called.
  • Host启动时调用

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.
  • Host或Dedicated Server启动时调用

NetworkManager.OnServerConnect

  • Called on the server when a new client connects.
  • 当有新client连接server时调用

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.
  • 当client启动时调用

NetworkManager.OnMatchCreate

  • Callback that happens when a NetworkMatch.CreateMatch request has been processed on the server.
  • 当服务端有处理NetworkMatch.CreateMatch请求后调用。

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.
  • 当client连接server后调用。client可以利用该函数初始化自己玩家对象。

NetworkManager.OnServerSceneChanged

  • Called on the server when a scene is completed loaded, when the scene load was initiated by the server with ServerChangeScene().
  • 在服务器调用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.
  • 当有client变为了ready状态,会在服务端触发该方法。

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.
  • 当有client利用ClientScene.AddPlayer实例化一个玩家对象后在服务端调用。服务端可利用该函数也实例化一个对应的玩家对象。

————————————————
维护日志:
2017-8-22:更新了标题
2020-8-12:增删查改

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值