关于Unity获取本机IP地址以及设备信息,你必须要知道的事。

Unity获取本机IP地址以及设备信息

1.代码示例

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using UnityEngine;
using static UnityEngine.EventSystems.EventTrigger;

public class IpTool : MonoBehaviour
{
    void Start()
    {
        Debug.Log("获取本机IPv4:" + GetLocalIPv4());
        Debug.Log("获取本机IPv6:" + GetLocalIPv6());
        Debug.Log("获取外网Ip:" + GetExternalNetIp());

        Debug.Log("获取指定域名www.baidu.com的Ip信息:");
        GetOtherNetIP("www.baidu.com");

        Debug.Log("获取设备名称:" + GetDeviceName());
        Debug.Log("获取设备模型:" + GetDeviceModel());
        Debug.Log("获取设备唯一标识码:" + GetDeviceUniqueIdentifier());


    }

    // 获取本机IPv4
    public string GetLocalIPv4()
    {
        string ipAddress = "";
        try
        {
            IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
            //Debug.Log("GetHostName:" + Dns.GetHostName());
            
            //返回IP列表
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    ipAddress = ip.ToString();
                    break;
                }
            }

            //获取主机别名列表
            for (int i = 0; i < host.Aliases.Length; i++)
            {
                //Debug.Log("主机别名:" + host.Aliases[i]);
            }


            //获取DNS名称
            //Debug.Log("DNS服务器名称" + host.HostName);



        }
        catch (Exception e)
        {
            Debug.LogError("IP 获取失败" +e.Message);
        }
        return ipAddress;
    }

    // 获取本机IPv6
    public string GetLocalIPv6()
    {
        string ipAddress = "";
        try
        {
            IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    ipAddress = ip.ToString();
                    break;
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError("IP 获取失败" + e.Message);
        }
        return ipAddress;
    }

    //获取指定域名的Ip信息
    public string GetOtherNetIP(string ip)
    {
        string ipAddress = "";
        try
        {

            //获取域名的地址
            IPHostEntry entry = Dns.GetHostEntry(ip);

            for (int i = 0; i < entry.AddressList.Length; i++)
            {
                Debug.Log("IP地址:" + entry.AddressList[i]);
            }

            for (int i = 0; i < entry.Aliases.Length; i++)
            {
                Debug.Log("主机别名:" + entry.Aliases[i]);
            }


            Debug.Log("DNS服务器名称" + entry.HostName);





        }
        catch (Exception e)
        {
            Debug.LogError("IP 获取失败" + e.Message);
        }
        return ipAddress;
    }

    //获取外网Ip
    public string GetExternalNetIp()
    {
        string IP = string.Empty;
        try
        {
            //从网址中获取本机ip数据
            WebClient client = new WebClient();
            client.Encoding = System.Text.Encoding.Default;
            IP = client.DownloadString("http://checkip.amazonaws.com/");
            client.Dispose();
            IP = Regex.Replace(IP, @"[\r\n]", "");
        }
        catch (Exception e)
        {
            Debug.LogError("获取外网Ip" + e.Message);
        }

        return IP;
    }

    //获取设备名称
    public string GetDeviceName()
    {
        return SystemInfo.deviceName;
    }

    //获取设备模型
    public string GetDeviceModel()
    {
        return SystemInfo.deviceModel;
    }

    // 获取设备唯一标识码
    public string GetDeviceUniqueIdentifier()
    {
        return SystemInfo.deviceUniqueIdentifier;
    }



}


2.运行结果

请添加图片描述

这里是井队,天高任鸟飞,海阔凭鱼跃,点个关注不迷路,我们下期再见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

井队Tell

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值