This document is about: FUSION 2
SWITCH TO

ネットワークプロパティ

概要

ネットワークプロパティは、NetworkBehaviour派生クラスのプロパティで、ネットワークオブジェクトの状態を定義します。

ネットワークプロパティを定義するには、NetworkBehaviour派生クラスの自動実装プロパティに[Networked]属性を追加します。Fusionは自動的にILコードを生成して、ネットワークオブジェクトの状態のメモリバッファにプロパティ(getter/setter)を接続します。

ネットワークプロパティは独自実装しないで、自動実装プロパティのままにしてください。もし、プロパティに特別な処理をする必要があるなら、別のプロパティでラップしてください。

C#

public class PlayerBehaviour : NetworkBehaviour
{
  [Networked] public float Health { get; set; }
}

通常、状態の変更はシミュレーションの一部としてFixedUpdateNetwork()内で行います。これによって、クライアントサイド予測が正確になり、変更が正確なティックで複製されることが保証されます。

備考:共有モードでは、他のタイミング(Update()FixedUpdate()など)で、ネットワークプロパティを変更することも可能です。

C#

public override void FixedUpdateNetwork()
{
    Health += Runner.DeltaTime * HealthRegen;
}

有効な型

Fusionが対応している([Networked]属性を追加できる)型は以下の通りです。

  • Primitives
    • byte, sbyte
    • short, int, long
    • ushort, uint, ulong
    • float, double
    • bool (converted to int)
  • Strings with a maximum Length set using the [Capacity] attribute (defaults to 16)
  • Unity struct types (defined in ILWeaver.cs)
    • Vector2, Vector3, Vector4
    • Quaternion
    • Matrix4x4
    • Vector2Int, Vector3Int
    • BoundingSphere
    • Bounds
    • Rect
    • BoundsInt
    • RectInt
    • Color, Color32
  • Enums
  • System.Guid
  • User Defined INetworkStructs
  • Fusion Defined INetworkStructs
    • NetworkString<>
    • IFixedStorage<>
    • NetworkBool
    • Ptr
    • Angle
    • BitSet64, BitSet128, BitSet192, BitSet256
    • PlayerRefSet
    • NetworkId
    • NetworkButtons
    • NetworkRNG
    • NetworkObjectGuid
    • NetworkPrefabRef
    • NetworkObjectHeader
    • NetworkPrefabId
    • SceneRef
    • TickTimer
    • IFixedStorage (_2, _4, _8, _16, _32, _64, _128, _256, _512)
  • Fusion Types
    • NetworkObject (serialized as NetworkId)
    • NetworkBehaviour (serialized as NetworkId and the NetworkBehaviour index)
    • PlayerRef (serialized as PlayerRef.PlayerId)
  • Network Collections
    • NetworkArray<T> with a maximum Length set using the [Capacity] attribute (defaults to 1)
    • NetworkDictionary<K, V> with a maximum Count set using the [Capacity]
    • NetworkLinkedList<T> with a maximum Count set using the [Capacity] attribute.
    • NetworkString<_size> with a maximum Size set using the any of the predefined IFixedStorage types, which are named _X, where X is the size of storage struct _32 for example

Fusionのオブジェクト型のネットワーク化

NetworkObjectNetworkBehaviourの参照は、ネットワークプロパティにすることができます。(注意:参照はINetworkStructでは無効です)

内部的にNetworkObjectNetworkBehaviourの参照は、NetworkIdNetworkBehaviourIDの値に変換されます。

Fusionのオブジェクト型のネットワーク化をご覧ください。

デフォルト値の設定

ネットワークプロパティは、デフォルト値を設定することが可能です。

共通型

ほとんどの共通型は、単純な代入で十分です。

C#

public class PlayerBehaviour : NetworkBehaviour
{
  [Networked] public float Health { get; set; } = 100;
  [Networked] public NetworkObject DefaultNetworkObject { get; set; } = GameObject.FindGameObjectWithTag("Foo").GetComponent<NetworkObject>();
}

コレクション

ネットワークコレクション(NetworkArray<T>NetworkDictionary<K, V>NetworkLinkedList<T>NetworkString<_size>)には、特別な構文が必要です。Network Collectionsをご覧ください。

Capacity

[Capacity]属性で、NetworkArrayNetworkDictionaryNetworkLinkedListNetworkStringstringの最大サイズを定義できます。

C#

public class MyNetworkBehaviour : NetworkBehaviour
{
  [Networked, Capacity(14)]
  string MyString { get; set; }

  [Networked, Capacity(8)]
  NetworkArray<byte> MyArray { get; }
}

RefとPtr

適切なコンストラクタを使用すると、参照やポインタのデフォルト値を設定できます。

C#

[Networked] public ref Byte Prop => ref MakeRef<Byte>(123);
[Networked] public Byte* Prop => MakePtr<Byte>(123);

詳細はNetwork Collectionsをご覧ください。

変更検知

変更検知をご覧ください。

実践的なブールの処理

ネットワークゲームでは、boolよりintの使用が推奨されることがあります。そのケースとは、値の変更が意図通りに検知されない場合(例えば、シリアライズされる前に値を切り替えてすぐ戻す、カリングでトグルが失われる、データ損失、共有モードのデータ圧縮、途中参加したプレイヤーなど)です。

解決策の一つは、符号付き整数値でブール値の状態をエンコードし、値の絶対値を追加情報(値が何度変更されたか、値が最後に更新されたのはいつか)として保持することです。

C#

public class MyNetworkBehaviour : NetworkBehaviour
{
  // This is the backing value for our virtual bool.
  [Networked] int _intToggle { get; set; }


  // This property will automatically encode the change count into the backing value,
  // while still acting like a normal bool.
  private bool CountToggle
  {
    // This assumes 0 to mean false, <= can be used to make 0 indicate true.
    get => _intToggle > 0;
    // Every call to set increments the absolute value
    // and sets the sign to negative for false, positive for true.
    set =>
      _intToggle = _intToggle >= 0 ?
        value ?   _intToggle + 1 : -(_intToggle + 1) :
        value ? -(_intToggle - 1):   _intToggle - 1;
  }

  private int toggleCount => _intToggle >= 0 ? _intToggle : -_intToggle;
}

C#

public class MyNetworkBehaviour : NetworkBehaviour
{
  // This is the backing value for our virtual bool.
  [Networked] int _intToggle { get; set; }

  // This property will automatically encode the current tick into the backing value,
  // while still acting like a normal bool.
  private bool TickToggle
  {
    get => _intToggle > 0;
    set => _intToggle = value ? Runner.Tick : -Runner.Tick;
  }

  private Tick toggleLastChangedTick => _intToggle >= 0 ? _intToggle : -_intToggle;

}

補間

スナップショットと補間をご覧ください。

ジェネリクス

NetworkBehaviourを継承したテンプレートクラスが作成可能で、ネットワークプロパティを持つこともできます。

C#

// This is VALID
class ValidGenericClass_With_NonGenericProperty<T> : NetworkBehaviour {
    [Networked] public int Prop { get; set; }
}

ただし、ジェネリック型<T>のネットワークプロパティを持つことはできません。

C#

// This is INVALID
class InValidGenericClass_With_GenericProperty<T> : NetworkBehaviour {
    [Networked] public T Prop { get; set; }
}
Back to top