Fusion Introduction

Overview

Fusion is a new high performance state synchronization networking library for Unity. With a single API, it supports many network architectures such as dedicated server, listen server, relay and more.

It is built with simplicity in mind, so you can expect it to integrate seamlessly into the common Unity workflow. At the same time, it brings advanced features to cover all possible use cases.

메인 화면으로

State Of The Art Features

This is a summary with some of the important features Fusion has built-in:

  • Snapshot Delta Compression: Fusion transfers either full states or AoI (area of interest) managed state chunks with a new delta-compression technique that greatly reduces bandwidth while requiring very low CPU load.
  • Made for Unity MonoBehaviours: define the game state in your own Unity components, no need to write serialization or any other complicated code. Also works seamlessly with all of Unity's most recent prefab features like nesting and variants.
  • Client side prediction + server reconciliation: the transparent use of internal data buffers mean the Unity-held data is always butter-smooth, with reconciliation and interpolation automatically (or custom) handled.
  • RPCs: simple and yet high performance RPCs are also included for API convenience.

메인 화면으로

One Library Many Architectures

Fusion was developed to replace both existing Photon state-transfer products (Bolt and PUN), so you can expect it to include all architectures supported by them and extra features on top:

  • Single player: locally with the same code, no connection required.
  • Dedicated Server: deploy Unity headless instances with full server authority.
  • Listen Server: player hosted server+client with built-in punch-through, relay as a fallback (including full host migration support provided by Photon Cloud).
  • Relay: client authority with data-driven server-managed snapshots and interest management (AoI: Area of Interest). Fusion scales to high player counts even on relay-only mode.
  • Smart Relay with custom code: the Fusion Photon Server plugin has full access to the game-state even without Unity. Writing light server game logic is achievable with little effort.

메인 화면으로

Code Snippets

Here we included a few preliminary code examples of how you use Fusion with Unity. This document will be updated as the API matures and more examples will be included with time.

메인 화면으로

Networked State

To create a MonoBehavior with properties to be synced over the network, simply extend the base NetworkedBehavior class. Fusion will internally handle the property with its own high performance data buffers and delta compression:

public class PlayerBehaviour : NetworkedBehaviour {
  public float Health { get; set; }
}

You can control how much accuracy you need for the appropriate numeric types:

public class PlayerBehaviour : NetworkedBehaviour {
  [Accuracy(0.001)]
  public float Health { get; set; }
}

메인 화면으로

Pre-built Components

Fusion comes with turnkey NetworkedBehavior sub-classes for the most common use cases in Unity:

  • RigidBody
  • Transform
  • Animation
  • NavmeshAgent
  • more

Simply add the corresponding component to your GameObject and let Fusion keep everything in sync.

메인 화면으로

Prefabs

Fusion supports all of Unity's most recent Prefab features out of the box:

  • Prefab Variants
  • Nested Prefabs
  • Nested Networked Objects (non prefabs)

You can directly instantiate from Unity and Fusion will detect them as networked objects automatically (assuming the prefab has one or more NetworkedBehavior component).

메인 화면으로

Player Input And Server Reconciliation

Fusion has built-in client side prediction and server reconciliation. To use that to the full extent setup your own input structs within the simulation API.

Full example:

A - Define an input struct:

public struct MyGameInput : INetworkedInput {
  public const uint BUTTON_FORWARD  = 1 << 3;
  public const uint BUTTON_BACKWARD = 1 << 4;
  public const uint BUTTON_LEFT     = 1 << 5;
  public const uint BUTTON_RIGHT    = 1 << 6;

  public uint Buttons;

  public bool IsUp(uint button) {
    return IsDown(button) == false;
  }

  public bool IsDown(uint button) {
    return (Buttons & button) == button;
  }
}

B - Use that when implementing tick-based game logic:

To write logic that affects networked state, override FixedUpdateNetwork (similar to Unity's FixedUpdate):

[OrderBefore(typeof(NetworkedPhysicsSimulation))]
public class PlayerBehaviour : SimulationBehaviour {
  public Rigidbody Rigidbody;
  public float     Speed = 5;

  public override void FixedUpdateNetwork() {
    if (GetInput(out MyGameInput input)) {
      if (input.IsDown(MyGameInput.BUTTON_FORWARD)) {
        Rigidbody.velocity += Vector3.forward * Speed * Time.fixedDeltaTime;
      }

      if (input.IsDown(MyGameInput.BUTTON_BACKWARD)) {
        Rigidbody.velocity += Vector3.back * Speed * Time.fixedDeltaTime;
      }

      if (input.IsDown(MyGameInput.BUTTON_LEFT)) {
        Rigidbody.velocity += Vector3.left * Speed * Time.fixedDeltaTime;
      }

      if (input.IsDown(MyGameInput.BUTTON_RIGHT)) {
        Rigidbody.velocity += Vector3.right * Speed * Time.fixedDeltaTime;
      }
    }
  }
}

C - Poll input:

Use the polling callback to feed input into Fusion:

public class ClientInputBehaviour : MonoBehaviour {
  public void OnInput(NetworkRunner runner, NetworkedInput inputContainer) {
    var myInput = new MyGameInput();

    if (Input.GetKey(KeyCode.W)) {
      myInput.Buttons |= MyGameInput.BUTTON_FORWARD;
    }

    if (Input.GetKey(KeyCode.S)) {
      myInput.Buttons |= MyGameInput.BUTTON_BACKWARD;
    }

    if (Input.GetKey(KeyCode.A)) {
      myInput.Buttons |= MyGameInput.BUTTON_LEFT;
    }

    if (Input.GetKey(KeyCode.D)) {
      myInput.Buttons |= MyGameInput.BUTTON_RIGHT;
    } 

    inputContainer.Set(myInput);
  }
}

기술문서 TOP으로 돌아가기