我正在尝试使用特定的 IP 地址和端口在 Unity Netcode 中加入/托管游戏。我的场景中有一个 NetworkManager,但我在使用它来托管/加入具有特定 IP 地址和端口的游戏时遇到问题。这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
using Unity.Netcode;
using UnityEngine.UI;
public class LobbyLogic : MonoBehaviour {
[SerializeField]private Button HostButtonStart;
[SerializeField]private Button JoinButtonStart;
[SerializeField] private InputField IPInputHost;
[SerializeField] private InputField PortInputHost;
[SerializeField] private InputField IPInputJoin;
[SerializeField] private InputField PortInputJoin;
private NetworkManager networkManager;
private void Awake() {
networkManager = FindObjectOfType<NetworkManager>();
HostButtonStart.onClick.AddListener(() => {
string ipAddress = IPInputHost.text;
int port = int.Parse(PortInputHost.text);
Debug.Log("Hosting a game");
// Use ipAdress and port vars to host a game trow networkManager.
});
JoinButtonStart.onClick.AddListener(() => {
string ipAddress = IPInputJoin.text;
int port = int.Parse(PortInputJoin.text);
Debug.Log("Joining a game");
// Use ipAdress and port vars to join a game trow networkManager.
});
}
private void Hide() {
gameObject.SetActive(false);
}
public void OpenMainMenu() {
Debug.Log("Loading up a scene! (MainMenu)");
SceneManager.LoadScene("MainMenu");
}
}
我尝试搜索网络、询问 ChatGPT 并阅读 Netcode 文档。没有任何帮助我。
https://docs-multiplayer.unity3d.com/netcode/current/components/networkmanager/index.html#connecting
示例:
NetworkManager.Singleton.GetComponent().SetConnectionData( "127.0.0.1", // IP地址是一个字符串 (ushort)12345 // 端口号是无符号短整型 );