This page is a work in progress and could be pending updates.

Fusion Headless Mode

本文介紹了一組實用腳本,可用於從無標頭的Unity應用程序中啟動Photon Fusion。 在沒有圖形介面的情況下啟動Fusion並沒有什麼特別之處,這個腳本只是其中一種方法。

HeadlessController 腳本

下面的腳本負責在Server遊戲模式下實例化和啟動一個新的Fusion Runner。 它將使用HeadlessUtils腳本(見下文),以檢查二進制文件是否以"無標頭模式”啟動,並驗証應該下載哪個場景。 如果在初始化Game Server時出現任何故障,應用程式將以非零的代碼退出,這對於從外部應用程式進行檢查非常有用。

using UnityEngine;
using Fusion;
using System.Collections.Generic;
using Fusion.Sockets;
using UnityEngine.SceneManagement;

public class HeadlessController : MonoBehaviour, INetworkRunnerCallbacks {

  [SerializeField]
  private NetworkRunner _runnerPrefab;

  void Awake() {
    Application.targetFrameRate = 30;
  }

  async void Start() {

    // Quit if not in Headless mode
    if (HeadlessUtils.IsHeadlessMode() == false) {
      Application.Quit(1);
    }

    // Get game scene to be loaded
    var sceneToLoad = HeadlessUtils.GetArg("-scene") ?? SceneManager.GetActiveScene().name;

    Debug.Log($"Starting Server");

    // Create a new Fusion Runner  
    var runner = Instantiate(_runnerPrefab);

    // Basic Setup
    runner.name = $"DedicatedServer";
    runner.AddCallbacks(this); // register callbacks

    // Start the Server
    var result = await runner.StartGame(new StartGameArgs() {
      GameMode = GameMode.Server, // for dedicated servers,
      Scene = SceneManager.GetSceneByName(sceneToLoad).buildIndex,
      SceneObjectProvider = gameObject.AddComponent<NetworkSceneManagerDefault>()
    });

    // Check if all went fine
    if (result.Ok) {
      Debug.Log($"Runner Start DONE");
    } else {
      // Quit the application if startup fails

      Debug.LogError($"Error while starting Server: {result.ShutdownReason}");

      // it can be used any error code that can be read by an external application
      // using 0 means all went fine
      Application.Quit(1);
    }
  }

  // Fusion INetworkRunnerCallbacks implementation

  public void OnShutdown(NetworkRunner runner, ShutdownReason shutdownReason) {
    Debug.LogWarning($"{nameof(OnShutdown)}: {nameof(shutdownReason)}: {shutdownReason}");

    // Quit normally
    Application.Quit(0);
  }

  public void OnPlayerJoined(NetworkRunner runner, PlayerRef player) => Debug.LogWarning($"{nameof(OnPlayerJoined)}: {nameof(player)}: {player}");
  public void OnPlayerLeft(NetworkRunner runner, PlayerRef player) => Debug.LogWarning($"{nameof(OnPlayerLeft)}: {nameof(player)}: {player}");
  public void OnConnectRequest(NetworkRunner runner, NetworkRunnerCallbackArgs.ConnectRequest request, byte[] token) => Debug.LogWarning($"{nameof(OnConnectRequest)}: {nameof(NetworkRunnerCallbackArgs.ConnectRequest)}: {request.RemoteAddress}");
  public void OnSceneLoadDone(NetworkRunner runner) => Debug.LogWarning($"{nameof(OnSceneLoadDone)}: {nameof(runner.CurrentScene)}: {runner.CurrentScene}");
  public void OnSceneLoadStart(NetworkRunner runner) => Debug.LogWarning($"{nameof(OnSceneLoadStart)}: {nameof(runner.CurrentScene)}: {runner.CurrentScene}");
  public void OnInput(NetworkRunner runner, NetworkInput input) { }
  public void OnInputMissing(NetworkRunner runner, PlayerRef player, NetworkInput input) { }
  public void OnConnectedToServer(NetworkRunner runner) { }
  public void OnDisconnectedFromServer(NetworkRunner runner) { }
  public void OnConnectFailed(NetworkRunner runner, NetAddress remoteAddress, NetConnectFailedReason reason) { }
  public void OnUserSimulationMessage(NetworkRunner runner, SimulationMessagePtr message) { }
  public void OnSessionListUpdated(NetworkRunner runner, List<SessionInfo> sessionList) { }
  public void OnCustomAuthenticationResponse(NetworkRunner runner, Dictionary<string, object> data) { }
  public void OnReliableDataReceived(NetworkRunner runner, PlayerRef player, System.ArraySegment<byte> data) { }
}

Back To Top

HeadlessUtils 腳本

這個腳本只包含兩個方法,可以用來檢查Unity二進制文件是否以”無標頭模式”啟動(IsHeadlessMode),以及獲取特定命令行參數的值(GetArg),對於您的無標頭遊戲伺服器傳遞自定義啟動設置非常有用。

using System;

public class HeadlessUtils {

  /// <summary>
  /// Signal if the executable was started in Headless mode by using the "-batchmode -nographics" command-line arguments
  /// <see cref="https://docs.unity3d.com/Manual/PlayerCommandLineArguments.html"/>
  /// </summary>
  /// <returns>True if in "Headless Mode", false otherwise</returns>
  public static bool IsHeadlessMode() {
    return Environment.CommandLine.Contains("-batchmode") && Environment.CommandLine.Contains("-nographics");
  }

  /// <summary>
  /// Get the value of a specific command-line argument passed when starting the executable
  /// </summary>
  /// <example>
  /// Starting the binary with: "./my-game.exe -map street -type hide-and-seek"
  /// and calling `var mapValue = HeadlessUtils.GetArg("-map", "-m")` will return the string "street"
  /// </example>
  /// <param name="keys">List of possible keys for the argument</param>
  /// <returns>The string value of the argument if the at least 1 key was found, null otherwise</returns>
  public static string GetArg(params string[] keys) {
    var args = Environment.GetCommandLineArgs();

    for (int i = 0; i < args.Length; i++) {
      foreach (var name in keys) {
        if (args[i] == name && args.Length > i + 1) {
          return args[i + 1];
        }
      }
    }

    return null;
  }
}


To Document Top