You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB

2 years ago
using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class NetworkCommandLine : MonoBehaviour {
private NetworkManager _netManager;
private void Start() {
_netManager = GetComponentInParent<NetworkManager>();
if (Application.isEditor)
2 years ago
return;
var args = GetCommandlineArgs();
if (args.TryGetValue("-mode", out var mode))
switch (mode) {
case "server":
_netManager.StartServer();
break;
case "host":
_netManager.StartHost();
break;
case "client":
_netManager.StartClient();
break;
}
}
private static Dictionary<string, string> GetCommandlineArgs() {
var argDictionary = new Dictionary<string, string>();
var args = Environment.GetCommandLineArgs();
for (var i = 0; i < args.Length; ++i) {
var arg = args[i].ToLower();
if (arg.StartsWith("-")) {
var value = i < args.Length - 1 ? args[i + 1].ToLower() : null;
value = value?.StartsWith("-") ?? false ? null : value;
argDictionary.Add(arg, value);
}
}
return argDictionary;
}
}