Unity Network 網絡基礎

  • Network類的變量及函數
  • NetworkPeerType網絡連接類型
  • 網絡存取方式

1. Network類的變量及函數

Network類的變量

屬性名 描述
connections 返回連接的玩家數組,數組類型是NetworkPlayer
isClient 判斷是否為客戶端
isServer 判讀是否為服務端
peerType 連接類型。包含 disconnected, connecting, server or client.
player 返回本地的NetworkPlayer實例

Network類的函數

函數名 描述
Connect 連接(ip 或 domain 名字)和服務器端口
Destroy Destroy the object associated with this view ID across the network.
Disconnect 斷開所有連接并且關閉所有網絡接口
HavePublicAddress 檢查當前機器是否有公網地址
InitializeServer 初始化服務器

Messages

方法名 描述
OnConnectedToServer Called on the client when you have successfully connected to a server.
OnDisconnectedFromServer Called on client during disconnection from server, but also on the server when the connection has disconnected.
OnFailedToConnect Called on the client when a connection attempt fails for some reason.
OnNetworkInstantiate Called on objects which have been network instantiated with Network.Instantiate.
OnPlayerConnected Called on the server whenever a new player has successfully connected.
OnPlayerDisconnected Called on the server whenever a player is disconnected from the server.
OnSerializeNetworkView Used to customize synchronization of variables in a script watched by a network view.
OnServerInitialized Called on the server whenever a Network.InitializeServer was invoked and has completed.

2. NetworkPeerType網絡連接類型

屬性名 描述
Disconnected No client connection running. Server not initialized.
Server Running as server.
Client Running as client.
Connecting Attempting to connect to a server.

3. 網絡存取方式

  • IP/Port
  • NAT
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容