我试图在Android智能手机上接收Unity和C#的设备的标准网关,但我就是解决不了它。
这就是为什么我在问是否有可能在安卓智能手机上找到一个使用Unity和C#的网关。
我已经尝试过这个,它在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());
}
}
}
}
发布于 2022-02-25 20:50:13
在PC中工作:
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();
发布于 2021-05-25 02:28:45
在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
来构建unity项目,并生成apk
文件,然后将其安装到您的Android Phone
或VR Headset
上。我在Oculus Quest
上测试了它,它工作得很好。
https://stackoverflow.com/questions/33848341
复制相似问题