PUN Classic (v1)、PUN 2 和 Bolt 處於維護模式。 PUN 2 將支援 Unity 2019 至 2022,但不會添加新功能。 當然,您所有的 PUN & Bolt 專案可以用已知性能繼續運行使用。 對於任何即將開始或新的專案:請切換到 Photon Fusion 或 Quantum。

Global Callbacks

Photon Bolt allows you to hook into various global callback methods that are triggered when different actions occur inside of Bolt.
To implement these callbacks create a new C# script in Unity and instead of inheriting from MonoBehaviour use Bolt.GlobalEventListener.
Below is an example of this.

C#

using UnityEngine;
using System.Collections;

public class NetworkCallbacks : Bolt.GlobalEventListener {

}

We can for example implement the Connected method and have Bolt notify us every time we are connected to a new peer.
You can find a list of all the callback methods you can implement in the API Documentation.

C#

using UnityEngine;
using System.Collections;

public class NetworkCallbacks : Bolt.GlobalEventListener {
  public override void Connected(BoltConnection connection) {
    Debug.Log(connection.ToString() + " connected to us");
  }
}

Important:
Bolt uses the C# standard public override ... and not the way Unity does it where you don't have to specify the override keyword.

There are two ways to get Bolt to detect your callback script and invoke the methods on it, the most basic one is to just do the default 'Unity way' and attach it to a game object somewhere in a scene like you would with any script.

The other way is to use the [BoltGlobalBehaviour] attribute, if you specify this attribute on your callback class, like below, Bolt will automatically find your class and create an instance of it that lives with together with Bolt and is destroyed when Bolt is shutdown.

C#

using UnityEngine;
using System.Collections;

[BoltGlobalBehaviour]
public class NetworkCallbacks : Bolt.GlobalEventListener {
  public override void Connected(BoltConnection connection) {
    Debug.Log(connection.ToString() + " connected to us");
  }
}

Using this attribute means that you don't have to manually deal with keeping a game object around, etc.
You can also specify a couple of parameters to the attribute which lets you customize a few options, here are a few example of this.

If you want callbacks that only get called on the server you can pass in BoltNetworkModes.Serveras the first parameter:
[BoltGlobalBehaviour(BoltNetworkModes.Server)], the same goes if you only want to have callbacks on the clients: BoltNetworkModes.Client.

You can also specify that you only want the callbacks to be active in certain scenes by passing in a scene name [BoltGlobalBehaviour("Level1")].
You can also pass in several scene names after one another: [BoltGlobalBehaviour("Level1", "Level2")].
Or you can specify a regular expression that can match several scenes names: [BoltGlobalBehaviour(@"Level\d")].

And as a last option you can combine both the client/server mode option and the scene name filter: [BoltGlobalBehaviour(BoltNetworkModes.Server, @"Level\d")]

Global Event Callbacks Cheat Sheet

As detailed above, a GlobalEventListener inherits from MonoBehaviour, so every callback that already exists on the Unity API can be used inside it (Start, Update, and so on).
The callbacks exposed by Bolt are intended to inform progress during gameplay or to invoke events that need to be processed by the game logic.

Here it is described all callbacks available for override through the Bolt.GlobalEventListener class, that is the main entry point for the Bolt workflow. Also, it's assigned where the callback can be invoked.

  Server Client Description
Initialization Callbacks
BoltStartBegin Callback triggered before the Bolt simulation starts.
BoltStartDone Callback triggered after the Bolt simulation starts.
BoltStartFailed Callback triggered when Bolt was not able to initialize completely.
BoltShutdownBegin Callback triggered when the Bolt simulation is shutting down.
Connection Callbacks
Connected Callback triggered when the local instance connects to another remote peer.
ConnectFailed Callback triggered when a connection to remote server has failed.
ConnectRequest Callback triggered when this instance receives an incoming client connection.
ConnectAttempt Callback triggered when trying to connect to a remote endpoint.
ConnectRefused Callback triggered when the connection to a remote server has been refused.
Disconnected Callback triggered when disconnected from remote endpoint.
Entity Callbacks
EntityReceived Callback triggered when a Bolt Entity is recieved from the network.
EntityAttached Callback triggered when a new Entity is attached to the bolt simulation.
EntityDetached Callback triggered when a new Entity is detached from the bolt simulation.
EntityFrozen Callback triggered when a bolt entity is frozen.
EntityThawed Callback triggered when a bolt entity is thawed.
ControlOfEntityGained Callback triggered when this instance of Bolt receieves control of a Bolt Entity.
ControlOfEntityLost Callback triggered when this instance of Bolt loses control of a Bolt Entity.
Scene Callbacks
SceneLoadLocalBegin Callback triggered before the new local scene is loaded.
SceneLoadLocalDone Callback triggered after the new local scene has been completely loaded.
SceneLoadRemoteDone Callback triggered when a remote connection has completely loaded the current scene.
Session Callbacks
SessionCreatedOrUpdated Callback triggered when the requested session creation or update was successful.
SessionConnected Callback triggered when the client joins the session.
SessionConnectFailed Callback triggered when the client can't join the session.
SessionListUpdated Callback triggered when the session list is updated.
Stream Callbacks
StreamDataReceived Callback triggered when binary stream data is received.
StreamDataStarted Callback triggered when a binary stream data starts to be received.
StreamDataProgress Callback triggered when a new block of data from a binary stream data is received.
StreamDataAborted Callback triggered when a binary stream is aborted.
Back to top