如何获得设备';团结的门户

本文关键字:何获得 | 更新日期: 2023-09-27 18:23:51

我正试图在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());
                }
            }
        }
    }

如何获得设备';团结的门户

适用于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();

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 PhoneVR Headset上。我在Oculus Quest上测试了它,它运行得很好。