【Unity】端末自分自身のipアドレスを取得する
2019-05-22 2019-07-18
OverView
Unity上で端末のipアドレスを取得する方法のメモ書き.本当にただのメモ書きなので,自分で欲しい機能があったら,適宜書き換えて下さい(o・ω・o)
今の状態だとゲッターも書いてないです.
Environment
- Unity2018.2.14f1
- Windows10
Method
GetIPAddress!なんてものは…
端末にはネットワークインターフェースがたくさんくっついている.
Ethernet,Wi-Fiをはじめ,Bluetooth,仮想マシンなどインストールしている端末には仮想ネットワークインターフェース,Wiresharkがインストールされている端末にはNpcap Loopback Adapterとか…
それらの中で知りたいインターフェース(だいたいはWi-Fi)を指定して現在のipアドレスを取得する必要があるため,ちょっと面倒くさいのだ…
以下サンプルソース
Sample.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
using System; using System.Collections.Generic; using System.Net.NetworkInformation; using UnityEngine; using UnityEngine.UI; public class Sample : MonoBehaviour { //obtained values [SerializeField, Space] private string _ipAddress; //This device ip //predefined values [SerializeField, Space(15f)] private string _priorityIntafaceName; //もし参照を優先したいインターフェースがあったらココに書く.なければ空白 //link components [SerializeField, Space(15f)] private Dropdown _selectNetworkInterfaceDropdown; private List<NetworkInterfaceData> _networkInterfaces = new List<NetworkInterfaceData>(); // Use this for initialization void Start () { GetIpAddress(); } /// <summary> /// Fetch the IP address of this terminal /// </summary> private void GetIpAddress() { _selectNetworkInterfaceDropdown.options.Clear(); _networkInterfaces.Clear(); //string hostname = Dns.GetHostName(); NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); bool isfoundPriorityInterface = false; foreach (var ni in nis) { IPInterfaceProperties ipip = ni.GetIPProperties(); UnicastIPAddressInformationCollection uipaic = ipip.UnicastAddresses; foreach (var uipai in uipaic) { if (ni.NetworkInterfaceType != NetworkInterfaceType.Loopback && ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel && uipai.Address.AddressFamily.ToString() == "InterNetwork" && ni.Name != "lo" && ni.Name != "lo0" && uipai.Address.ToString().IndexOf("169.254") == -1) { //Debug.Log("[" + ni.Name + "] " + uipai.Address); _networkInterfaces.Add(new NetworkInterfaceData(ni.Name, uipai.Address)); _selectNetworkInterfaceDropdown.options.Add (new Dropdown.OptionData(_networkInterfaces[_networkInterfaces.Count - 1].InterfaceName)); //priority interface if (ni.Name == _priorityIntafaceName) { //Debug.Log(uipai.Address); _ipAddress = uipai.Address.ToString(); _selectNetworkInterfaceDropdown.value = _selectNetworkInterfaceDropdown.options.Count - 1; isfoundPriorityInterface = true; } _selectNetworkInterfaceDropdown.RefreshShownValue(); } } } if (!isfoundPriorityInterface && _selectNetworkInterfaceDropdown.options.Count > 0 && _ipAddress == String.Empty) { _selectNetworkInterfaceDropdown.value = 0; _ipAddress = _networkInterfaces[0].IpAddress.ToString(); } } /// <summary> /// Select other network interface by drop down /// </summary> public void OnUpdatedSelectInterfaceDropdownValue() { Debug.Log("Select interface : " + _networkInterfaces[_selectNetworkInterfaceDropdown.value].InterfaceName); _ipAddress = _networkInterfaces[_selectNetworkInterfaceDropdown.value].IpAddress.ToString(); } } |
NetwordInterfaceData.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System.Net; public class NetworkInterfaceData { public string InterfaceName; public IPAddress IpAddress; public NetworkInterfaceData(string InterfaceName, IPAddress IpAddress) { this.InterfaceName = InterfaceName; this.IpAddress = IpAddress; } } |
使い方
1. サンプルソースを2つコピペ

2. 適当なゲームオブジェクトにSample.csをアタッチ

3. 適当なDropdownを作成

4. DropdownのOnValueChangedにメソッドを指定

DropdownのOnValueChangedにSample.csのOnUpdatedSelectIntarfaceDropdownValueを指定
5. DropdownをSample.csの変数にアタッチ

6. 再生
SampleクラスのGetIpAddress()を実行することで変数_ipAddressに現在のipAddressが格納される.
Dropdownにはインターフェース一覧が格納されるので,ipAddressを取得したいインターフェースをDropdownから選択することで指定したインターフェースのipAddressが取得される.


Sampleクラスの44行目が&&から始まってますがこれは正しいのでしょうか?
普通にリファクタミスです。記事見て頂きありがとうございます!修正しました。