赞
踩
- /***
- *
- * Title: MXFramework
- * 主题: 获取本机IP
- * Description:
- * 功能:获取局域网和广域网IP
- *
- * Date: 2020
- * Version: v5.0版本
- * Modify Recoder:
- *
- */
-
- using System;
- using System.Collections;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- using UnityEngine;
- using UnityEngine.Networking;
-
- namespace Mx.Net
- {
- /// <summary>本机IP</summary>
- public class LocalIP :MonoBehaviour
- {
- /// <summary>获取局域网IP</summary>
- public string GetLANIP(ADDRESSFAM Addfam)
- {
- if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
- {
- return null;
- }
-
- string output = string.Empty;
-
- foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
- {
- #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
- NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
- NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
- if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
- #endif
- {
- foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
- {
- //IPv4
- if (Addfam == ADDRESSFAM.IPv4)
- {
- if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
- {
- output = ip.Address.ToString();
- }
- }
-
- //IPv6
- else if (Addfam == ADDRESSFAM.IPv6)
- {
- if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
- {
- output = ip.Address.ToString();
- }
- }
- }
- }
- }
-
- return output;
- }
-
- /// <summary>获取广域网IP</summary>
- public void GetWANIp(Action<string> finish)
- {
- string url = "http://icanhazip.com/";
-
- StartCoroutine(GetWANIp(url, finish));
- }
-
- public IEnumerator GetWANIp(string url, Action<string> finish)
- {
- using (UnityWebRequest uwr = UnityWebRequest.Get(url))
- {
- yield return uwr.SendWebRequest();
- if (finish != null) finish(uwr.downloadHandler.text);
- }
- }
- }
-
- public enum ADDRESSFAM
- {
- IPv4, IPv6
- }
- }
Unity QQ交流群:299412191 欢迎对Unity感兴趣的同学加入.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。