[英]How do I get a device's gateway in Unity?
我正在嘗試在 Android 智能手機上接收 Unity 和 C# 中設備的標准網關,但我無法修復它。
這就是為什么我問是否有可能使用 Unity 和 C# 在 Android 智能手機上找到網關。
我已經嘗試過這個,它在 Unity 編輯器中確實有效:
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
if (addresses.Count > 0)
{
foreach (GatewayIPAddressInformation address in addresses)
{
if (address.Address.ToString() != "0.0.0.0")
{
Debug.Log("Gateway: " + address.Address.ToString());
}
}
}
}
在電腦上工作:
IPAddress GatewayAddress = NetworkInterface
.GetAllNetworkInterfaces()
.Where(n => n.OperationalStatus == OperationalStatus.Up)
.Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.SelectMany(n => n.GetIPProperties()?.GatewayAddresses)
.Select(g => g?.Address)
.Where(a => a != null)
.Where(a => a.AddressFamily == AddressFamily.InterNetwork)
.FirstOrDefault();
在Assets/Plugins/Android
目錄中創建一個Network.java
文件。
資產/插件/Android/Network.java
package com.mynet;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Network{
//Match the IP of class C address
public static final String regexCIp = "^192\\.168\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)$";
//Match a class A address
public static final String regexAIp = "^10\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)$";
//Match B address
public static final String regexBIp = "^172\\.(1[6-9]|2\\d|3[0-1])\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)$";
public String getHostIp() {
String hostIp;
Pattern ip = Pattern.compile("(" + regexAIp + ")|" + "(" + regexBIp + ")|" + "(" + regexCIp + ")");
Enumeration<NetworkInterface> networkInterfaces = null;
try {
networkInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress address;
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
address = inetAddresses.nextElement();
String hostAddress = address.getHostAddress();
Matcher matcher = ip.matcher(hostAddress);
if (matcher.matches()) {
hostIp = hostAddress;
return hostIp;
}
}
}
return null;
}
}
在
C#
代碼中使用此腳本,如下所示:
using UnityEngine;
using UnityEngine.UI;
public class MyIPScript: MonoBehaviour
{
public Text ipText;
public void Start()
{
string ip = GetIP();
ipText.text = ip;
}
private string GetIP() {
AndroidJavaObject jo = new AndroidJavaObject("com.mynet.Network");
AndroidJavaClass act = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
return jo.Call<string>("getHostIp");
}
}
注意:這僅適用於Android
設備。 如果在PC
上測試,您將看不到任何 IP 地址。 因此,通過從Build Settings
選擇Android
並生成apk
文件來構建您的 unity 項目,然后將其安裝在您的Android Phone
或VR Headset
。 我在Oculus Quest
上對其進行了測試,效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.