Unity得到本机IP地址
直接上代码
using System.Collections; using System.Collections.Generic; using System.Net.NetworkInformation; using System.Net.Sockets; using UnityEngine; public class IPLocalManager : MonoBehaviour { // Use this for initialization void Start () { Debug.LogError(GetIP()); } // Update is called once per frame void Update () { } private string GetIP() { NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adater in adapters) { if (adater.Supports(NetworkInterfaceComponent.IPv4)) { UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses; if (UniCast.Count > 0) { foreach (UnicastIPAddressInformation uni in UniCast) { if (uni.Address.AddressFamily == AddressFamily.InterNetwork) { Debug.Log(uni.Address.ToString()); return uni.Address.ToString(); } } } } } return null; } }
using System.Collections;using System.Collections.Generic;using System.Net.NetworkInformation;using System.Net.Sockets;using UnityEngine;
public class IPLocalManager : MonoBehaviour {
// Use this for initializationvoid Start () { Debug.LogError(GetIP());}// Update is called once per framevoid Update () {} private string GetIP() { NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adater in adapters) { if (adater.Supports(NetworkInterfaceComponent.IPv4)) { UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses; if (UniCast.Count > 0) { foreach (UnicastIPAddressInformation uni in UniCast) { if (uni.Address.AddressFamily == AddressFamily.InterNetwork) { Debug.Log(uni.Address.ToString()); return uni.Address.ToString(); } } } } } return null; }}

浙公网安备 33010602011771号