You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.9 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.Runtime.Serialization;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.NetworkInformation;
  8. using System.Net.Sockets;
  9. using UnityEngine;
  10. namespace Networking
  11. {
  12. public static class Utility
  13. {
  14. public static byte[] ObjectToByteArray<T>(T obj)
  15. {
  16. if (obj == null)
  17. return null;
  18. BinaryFormatter bf = new BinaryFormatter();
  19. SurrogateSelector surrogateSelector = new SurrogateSelector();
  20. Vector3SerializationSurrogate vector3SS = new Vector3SerializationSurrogate();
  21. ColorSerializationSurrogate colorSS = new ColorSerializationSurrogate();
  22. surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS);
  23. surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSS);
  24. bf.SurrogateSelector = surrogateSelector;
  25. MemoryStream ms = new MemoryStream();
  26. bf.Serialize(ms, obj);
  27. return ms.ToArray();
  28. }
  29. public static T ByteArrayToObject<T>(byte[] arrBytes)
  30. {
  31. MemoryStream memStream = new MemoryStream();
  32. BinaryFormatter bf = new BinaryFormatter();
  33. SurrogateSelector surrogateSelector = new SurrogateSelector();
  34. Vector3SerializationSurrogate vector3SS = new Vector3SerializationSurrogate();
  35. ColorSerializationSurrogate colorSS = new ColorSerializationSurrogate();
  36. surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS);
  37. surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSS);
  38. bf.SurrogateSelector = surrogateSelector;
  39. memStream.Write(arrBytes, 0, arrBytes.Length);
  40. memStream.Seek(0, SeekOrigin.Begin);
  41. T obj = (T)bf.Deserialize(memStream);
  42. return obj;
  43. }
  44. public enum ADDRESSFAM
  45. {
  46. IPv4, IPv6
  47. }
  48. //https://stackoverflow.com/questions/51975799/how-to-get-ip-address-of-device-in-unity-2018
  49. public static string GetIP(ADDRESSFAM Addfam)
  50. {
  51. //Return null if ADDRESSFAM is Ipv6 but Os does not support it
  52. if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
  53. {
  54. return null;
  55. }
  56. string output = "";
  57. foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
  58. {
  59. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  60. NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
  61. NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;
  62. if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
  63. #endif
  64. {
  65. foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
  66. {
  67. //IPv4
  68. if (Addfam == ADDRESSFAM.IPv4)
  69. {
  70. if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
  71. {
  72. output = ip.Address.ToString();
  73. }
  74. }
  75. //IPv6
  76. else if (Addfam == ADDRESSFAM.IPv6)
  77. {
  78. if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
  79. {
  80. output = ip.Address.ToString();
  81. }
  82. }
  83. }
  84. }
  85. }
  86. return output;
  87. }
  88. }
  89. }