Skip to content

Instantly share code, notes, and snippets.

@st0326s
Created August 6, 2018 01:47
Show Gist options
  • Save st0326s/d63c02ec37123616976f8f23a55fb08f to your computer and use it in GitHub Desktop.
Save st0326s/d63c02ec37123616976f8f23a55fb08f to your computer and use it in GitHub Desktop.
Unity 通信状態の取得 ref: https://qiita.com/satotin/items/e26787254eaec1fa3be4
/// <summary>
/// Pingによる応答時間の取得
/// </summary>
public async void OnClickGetPingTime()
{
int pingTime = await PingStatus.GetPingTime();
}
/// <summary>
/// 通信状態の取得クラス
/// </summary>
public static class PingStatus
{
/// <summary>
/// PING応答時間の取得
/// </summary>
/// <returns>PING応答時間(msec)</returns>
public static async Task<int> GetPingTime()
{
// "/"を省いたホスト名にする事
string host = "";
// ホスト名からIPアドレスを非同期で取得
IPAddress[] serverIPs = await Dns.GetHostAddressesAsync(host);
// コンストラクタが走った時点でPING送信される
Ping ping = new Ping(serverIPs[0].ToString());
await new WaitUntil(() => ping.isDone == true);
return ping.time;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment