このページは編集中です。更新が保留になっている可能性があります。

Network Object

Overview

The NetworkObject component assigns a network identity to a GameObject so it can be shared across the network. Any GameObject that is seen by all players and is a dynamic part of the scene should carry a NetworkObject script.

Fusion offers two classes for creating custom network behaviours on a NetworkObject:

  • SimulationBehaviour for simulation related behaviours; and,
  • NetworkBehaviour for behaviours keeping or tracking a [Networked] state.

トップに戻る

NetworkObject

A single NetworkObject script on the root node of a GameObject is sufficient to control the entire hierarchy. At runtime the NetworkObject will find all SimulationBehaviours and NetworkBehaviours in its hierarchy.

If needed, NetworkObjects can be nested. In this case, the NetworkObject will not search into nested children and let child NetworkObjects track all SimulationBehaviours and NetworkBehaviours below it. A typical usecase would be a two player controlled tank made of a chassis and a turret where the driver controls the chassis' direction (movement) and the gunner controls the turret (shooting).

トップに戻る

Prefab / Object Table

In order for the Prefab parameter to make sense to Fusion (and to be sure that all clients agree on exactly which prefab that is), the prefab itself must be registered with Fusion. Under normal circumstances the Fusion toolset will automatically detect these prefabs and register them, but you can also manually trigger this detection by selecting the NetworkProjectConfig and pressing Rebuild Object Table in the Unity Inspector.

トップに戻る

Instantiation / Spawn

The instantiation of NetworkObject prefabs requires two steps:

  1. At edit-time: ensure the clients have to have identical NetworkProjectConfig assets which agrees on the NetworkObject prefab identities. This is done by running Rebuild Object Table in the NetworkProjectConfig asset; and,
  2. At runtime: call Spawn() on the NetworkRunner with a NetworkObject prefab as the parameter.

IMPORTANT: Calling the generic Unity Instantiate() method will just create a broken local instance!

トップに戻る

Destruction / Despawn

To destroy a NetworkObject, call Runner.Despawn() and pass the object as a parameter. This will destroy the object as well as all nested objects; to despawn only the top object, the nested objects need to be de-parented manually first.

トップに戻る

Finding NetworkObject

A NetworkObject can be found using NetworkRunner.TryFindObject() by passing the NetworkId that references the instance of that NetworkObject.

NetworkIds and NetworkObjects can be networked with the [Networked] attribute, but networking the NetworkObject will only add the NetworkId on the Game State (No data from the actual NetworkObject will be copied to the buffer again, only the reference for it).

When accessing a [Networked] NetworkObject, the NetworkRunner.TryFindObject() is executed beforehand, so it is the developer choice to network the ID and do it manually or the Object itself.

トップに戻る

State Authority

State Authority indicates who has the right to change and decide what is the correct value of a [Networked] property.

Although the concept itself is identical for all topologies in Fusion, there are functional differences.

トップに戻る

ServerMode And HostMode

In ServerMode and HostMode, the simulation on the server / host is single state authority. It is therefore possible to encapsulate code paths which should only be executed there by using an if-statement with Object.HasStateAuthority. Object.HasStateAuthority will only ever return true on the server / host.

In Server/Client Mode this value will always be PlayerRef.None, as the Server/Host peer will always act as State Authority.

トップに戻る

SharedMode

In SharedMode, the state authority is shared between players. Every player can have state authority over a certain number of objects in the simulation. Therefore Object.HasStateAuthority will return true for the player who has authority over it. The Photon Shared Server manages State Authority of NetworkObjects, but does not HAVE State Authority over those objects.

The State Authority indicates which PlayerRef's peer is the final authority on the NetworkObject's state (Networked Properties), and its Networked Property values are replicated to other clients as Tick Snapshots.

NOTE: The StateAuthority property is only applicable to Shared Mode.

Although the State Authority of an NetworkObject may change in SharedMode, there is always only one player who has it for a given object. It is not possible for multiple players to have state authority over the same object!

  • DestroyWhenStateAuthorityLeaves: Indicates if this NetworkObject will automatically be despawned when the Player with StateAuthority over it leaves the game.
  • AllowStateAuthorityOverride: Indicates if the State Authority can be acquired by a Player when another player already has the State Authority. N.B.: This property must be set before spawning, as it cannot be changed after spawn. In most use cases, leave AllowStateAuthorityOverride enabled for Prefabs and Scene Objects.
  • ReleaseStateAuthority(): The current State Authority releases control of the object.
  • RequestStateAuthority(): Can be called on a client to assign its PlayerRef as the State Authority for the NetworkObject. The Photon Shared Server will only grant the request if:
    • The Object has AllowStateAuthorityOverride enabled; or,
    • The Object has no current State Authority. This can be because the State Authority left the game session or because it previously relinquished control by call ReleaseStateAuthority() on the object.
  • StateAuthorityChanged(): Callback called on any implementation of IStateAuthorityChanged interface when the state authority over the object changes.

トップに戻る

Input Authority

The InputAuthority property indicates which PlayerRef's input data (INetworkInput) is returned when GetInput() is called in any FixedUpdateNetwork on this NetworkObject. Player Input is only available to the client with Input Authority, and to the State Authority. All other clients (Proxies) will return false when GetInput() is called.

Input Authority and Fusion's INetworkInput handling are primarily meant for ServerMode and HostMode, as they are fundamental to client prediction and resimulation. In SharedMode however, user inputs are consumed immediately by the State Authority - making the INetworkStruct input system non-essential. It may still be desirable to use the Fusion input system in SharedMode for convenience as well as leaving the doors open to switching to ServerMode or HostMode in the future, and want to avoid refactoring later.

  • HasInputAuthority: will return true if the Runner.LocalPlayer == Object.InputAuthority. Use this in scripts to test if the NetworkRunner.LocalPlayer is the Input Authority for this NetworkObject.
  • AssignInputAuthority(): Assigns input authority to the PlayerRef passed to the method. This can only be changed by the State Authority of the Object.
  • RemoveInputAuthority(): Removes input authority from the NetworkObject by setting it to PlayerRef.None. This can only be changed by the State Authority of the Object.


ドキュメントのトップへ戻る