一直在阅读一些教程和统一文档来学习如何将网络实例作为“主机”启动,但我所能找到的只是如何将系统初始化为客户端或服务器。我在下面附加了将在服务器或客户端模式下启动的代码,但是我该如何为主机模式进行初始化呢?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkManager : MonoBehaviour {
public static NetworkManager Instance = null; // create singleton
public bool host;
private int hostPort = 7777;
private string hostIP = "192.168.1.160";
string networkConnections;
private void Awake()
{
InitNetworkManager();
}
private void Update()
{
if (Network.isServer)
{
networkConnections = NetworkServer.connections.Count.ToString();
}
}
void InitNetworkManager()
{
if (Instance == null)
Instance = this;
else if (Instance != null)
Destroy(gameObject);
if (host && !Network.isServer)
{
// setup server
LaunchServer();
return;
}
else if (!host && !Network.isClient)
{
// setup client
LaunchClient();
return;
}
}
void LaunchServer()
{
Network.InitializeServer(32, hostPort, true);
Debug.Log("Server Launched");
}
void LaunchClient()
{
Network.Connect(hostIP, hostPort);
Debug.Log("Client Launched");
}
}