Version: 2017.2
Object Visibility
NetworkBehaviour 回调

Network Manager 回调

This section covers the Network Manager callbacks received depending on the type of connection you have to your game’s Host server.

See documentation on NetworkBehaviour callbacks for more information on networking callbacks.

Local connection callbacks

Different callbacks are received depending on your game’s state. Three states are available. These can be set in the Network Manager HUD.

  • Host: To set your game up in Host mode, select LAN Host(H) or use the API function StartHost().

  • Client: To set your game up in Client mode, select LAN Client(C) and enter the server IP into the text field, or use the API function StartClient(). Note that your server IP must be in the same local network - or you can type “localhost” if you are connecting to the same machine.

  • Server: To set your game up in Server mode, select LAN Server Only(S) or use the API function StartServer().

During run time, the Network Manager HUD’s controls are also available in the Network Manager HUD component’s Inspector window. Click Runtime Controls to access these.

This page details the callbacks received for each mode. To learn more about setting up game states, see Using the Network Manager: Game state management.

Some Network Manager callbacks require you to have multiple instances of the game running (for example, two Standalone instances, or one Standalone and one in the Editor). These instances can be on the same machine or on different machines, as long as those machines are connected through a local network.

Host callbacks

To get these callbacks you need to have two instances of the game, one running in Host mode and the other running in Client mode. These callbacks are only called on instances that are running in Host mode.

Follow the steps below to get the callbacks. To stop an instance, press Stop or use the API function StopHost().

Step 1: Start Host instance

  • (Start() function is called)

  • OnStartHost

  • OnStartServer

  • OnServerConnect

  • OnStartClient

  • OnClientConnect

  • OnServerSceneChanged

  • OnServerReady

  • OnServerAddPlayer

  • OnClientSceneChanged

Step 2: Start Client instance

  • OnServerConnect

  • OnServerReady

  • OnServerAddPlayer

Step 3: Stop Client instance

  • OnServerDisconnect

Step 4: Stop Host instance

  • OnStopHost

  • OnStopServer

  • OnStopClient

Client callbacks

To get these callbacks you need to have two instances of the game, one running in Server mode and the other running in Client mode. These callbacks are only called on instances that are running in Client mode.

Launch the Server mode instance first, then follow the steps below to get the callbacks. To stop an instance, press Stop or use the API function StopHost()

Step 1: Start Client instance

  • (Start() function is called)

  • OnStartClient

  • OnClientConnect

  • OnClientSceneChanged

Step 2: Stop Server instance

  • OnStopClient

  • OnClientDisconnect

Server callbacks

To get these callbacks you need to have two instances of the game, one running in Server mode and the other running in Client mode. These callbacks are only called on instances that are running in Server mode.

Follow the steps below to get the callbacks. To stop an instance, press Stop or use the API function StopHost().

Step 1: Start Server instance

  • (Start() function is called)

  • OnStartServer

  • OnServerSceneChanged

Step 2: Start Client instance

  • OnServerConnect

  • OnServerReady

  • OnServerAddPlayer

Step 3: Stop Client instance

  • OnServerDisconnect

Step 4: Stop Server instance

  • OnStopServer

Internet connection callbacks

Internet connections to the Host server take place through the Unity MatchMaker system, which connects you via a relay server.

Different callbacks are received depending on your game’s state. Two states are available for MatchMaker: Host and Client. To use MatchMaker, run the game and select Enable Match Maker (M) from the Network Manager HUD menu:

This opens the MatchMaker menu:

  • Host: To set your game up in Host Mode, select Enable Match Maker (M) > Create Internet Match or use the API function CreateMatch()

  • Client: To set your game up in Client mode, select Enable Match Maker (M) > Find Internet Match > Join Internet Match or use the API function JoinMatch()

This page details the callbacks received for each mode. To learn more about setting up game states, see Using the Network Manager: Game state management.

Some Network Manager callbacks require you to have multiple instances of the game running (for example, two Standalone instances, or one Standalone and one in the Editor). These instances can be on the same machine or on different machines, and on the same network or different networks, but all machines must have an Internet connection. MatchMaker allows you to connect instances of your game via the Internet, so machines with instances of your game could be situated in different parts of the world.

Host MatchMaker callbacks

To get these callbacks you need to have two instances of the game, one running in Host mode and the other running in Client mode. These callbacks are only called on instances that are running in Host mode.

Follow the steps below to get the callbacks. To stop an instance, press Stop or use the API function StopHost().

Step 1: Start Host instance

  • (Start() function is called)

  • OnStartHost

  • OnStartServer

  • OnServerConnect

  • OnStartClient

  • OnMatchCreate

  • OnClientConnect

  • OnServerSceneChanged

  • OnServerReady

  • OnServerAddPlayer

  • OnClientSceneChanged

Step 2: Start Client instance and select Join Internet Match or use the API function JoinMatch()

  • OnServerConnect

  • OnServerReady

  • OnServerAddPlayer

Step 3: Stop Client instance

  • OnServerDisconnect

Client MatchMaker callbacks

To get these callbacks you need to have two instances of the game running, one in Host mode and the other running in Client mode. These callbacks are only called on instances that are running in Client mode.

Launch the Host mode instance first, then the Client instance. Follow the steps below to get the callbacks. To stop an instance, press Stop or use the API function StopHost().

Step 1: On the Client instance, press Find Internet Match or use the API function ListMatches() to find a list of online games

  • (Start() function is called)

  • OnMatchList

Step 2: On the Client instance, press Join Match or use the API function JoinMatch() to join an online game

  • OnStartClient

  • OnMatchJoined

  • OnClientConnect

  • OnClientSceneChanged

Step 3: Stop Host instance

  • OnStopClient

  • OnClientDisconnect

Object Visibility
NetworkBehaviour 回调
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961