IpAddressDisplay.cs 879 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using UnityEngine;
  7. using UnityEngine.Networking.Types;
  8. using Valve.Newtonsoft.Json.Utilities;
  9. public class IpAddressDisplay : MonoBehaviour
  10. {
  11. private string ipAddress;
  12. private void OnGUI()
  13. {
  14. GUI.TextField(new Rect(Screen.width - 130f, Screen.height - 30f, 120f, 20f), ipAddress);
  15. }
  16. void Start()
  17. {
  18. string usedIp = null;
  19. foreach (var ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
  20. {
  21. if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  22. {
  23. if (usedIp == null || ip.ToString().StartsWith("192.168.1."))
  24. {
  25. usedIp = ip.ToString();
  26. }
  27. }
  28. }
  29. ipAddress = usedIp;
  30. }
  31. }