public static int GetHostPort (int hostId);

参数

hostId主机 ID。

返回

int UDP 端口号,如果发生错误则为 -1。

描述

返回分配给主机的端口号。

注意:Xbox One 和 WebSocket 不支持此函数。

using UnityEngine;
using UnityEngine.Networking;

public class ExampleScript : MonoBehaviour { int channelId; int hostId; void Start() { // Init Transport using default values. NetworkTransport.Init();

// Create a connection_config and add a Channel. ConnectionConfig connection_config = new ConnectionConfig(); channelId = connection_config.AddChannel(QosType.Reliable);

// Create a topology based on the connection config. HostTopology topology = new HostTopology(connection_config, 10);

// Create a host based on the topology we just created, and bind the socket to port assigned buy OS. hostId = NetworkTransport.AddHost(topology, 0);

int assignedPort = NetworkTransport.GetHostPort(hostId); if (assignedPort == -1) { Debug.Log("GetHostPort returns error"); } } }