This document is about: PUN 2

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:


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.LocalPlayer: The actor number is always -1.
  • PhotonNetwork.NickName: Works as expected.
  • PhotonNetwork.PlayerList: Contains only the local player.
  • PhotonNetwork.PlayerListOthers 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/SetInterestGroups/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. 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