This document is about: PUN 1
SWITCH TO

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

Offline Mode

Offline mode is a feature to be able to re-use your multiplayer code in singleplayer game modes as well.

The most common features that you'll want to be able to use in single player are sending RPCs and using PhotonNetwork.Instantiate.
The main goal of offline mode is to disable null references and other errors when using PhotonNetwork functionality while not connected.
You would still need to keep track of the fact that you're running a single player game, to set up the game etc.
However, while running the game, all code should be reusable.

You need to manually enable offline mode, as PhotonNetwork needs to be able to distinguish erroneous from intended behaviour.
Enabling this feature is very easy:

C#

PhotonNetwork.offlineMode = true; 

once set to true, Photon will callback with the call OnConnectedToMaster() and then you can create a room, this room will be of course Offline too.

You can now reuse certain multiplayer methods without generating any connections and errors.
Furthermore there is no noticeable overhead.
Below follows a list of PhotonNetwork functions and variables and their results during offline mode:

  • PhotonNetwork.player: The player ID is always -1.
  • PhotonNetwork.playerName: Works as expected.
  • PhotonNetwork.playerList: Contains only the local player.
  • PhotonNetwork.otherPlayers Always empty.
  • PhotonNetwork.time: returns Time.time.
  • PhotonNetwork.isMasterClient: Always true.
  • PhotonNetwork.AllocateViewID(): Works as expected.
  • PhotonNetwork.Instantiate: Works as expected.
  • PhotonNetwork.Destroy: Works as expected.
  • PhotonNetwork.RemoveRPCs/RemoveRPCsInGroup/SetReceivingEnabled/SetSendingEnabled/SetLevelPrefix: While these make no sense in single player, they will not hurt either.
  • PhotonView.RPC: works as expected.

Note that using properties or methods other than the ones above can yield unexpected results and some will simply do nothing.
e.g. PhotonNetwork.room will, obviously, return null.
If you intend on starting a game in single player, but move it to multiplayer at a later stage, you might want to consider hosting a 1 player game instead;
this will preserve buffered RPCs and instantiation calls, whereas offline mode instantiations will not automatically carry over after connecting.

Either set PhotonNetwork.offlineMode = false; or simply call Connect() to stop offline mode.

Back to top