Utils.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security.Cryptography;
  4. using UnityEngine;
  5. namespace Mirror
  6. {
  7. // Handles network messages on client and server
  8. public delegate void NetworkMessageDelegate(NetworkConnection conn, NetworkReader reader, int channelId);
  9. // Handles requests to spawn objects on the client
  10. public delegate GameObject SpawnDelegate(Vector3 position, Guid assetId);
  11. public delegate GameObject SpawnHandlerDelegate(SpawnMessage msg);
  12. // Handles requests to unspawn objects on the client
  13. public delegate void UnSpawnDelegate(GameObject spawned);
  14. // invoke type for Cmd/Rpc
  15. public enum MirrorInvokeType
  16. {
  17. Command,
  18. ClientRpc
  19. }
  20. // Deprecated 2021-03-15
  21. [Obsolete("Version has never been used, neither by UNET nor by Mirror.")]
  22. public enum Version
  23. {
  24. Current = 1
  25. }
  26. // channels are const ints instead of an enum so people can add their own
  27. // channels (can't extend an enum otherwise).
  28. //
  29. // note that Mirror is slowly moving towards quake style networking which
  30. // will only require reliable for handshake, and unreliable for the rest.
  31. // so eventually we can change this to an Enum and transports shouldn't
  32. // add custom channels anymore.
  33. public static class Channels
  34. {
  35. public const int Reliable = 0; // ordered
  36. public const int Unreliable = 1; // unordered
  37. // Deprecated 2021-03-15
  38. [Obsolete("Use Channels.Reliable instead")]
  39. public const int DefaultReliable = Reliable;
  40. // Deprecated 2021-03-15
  41. [Obsolete("Use Channels.Unreliable instead")]
  42. public const int DefaultUnreliable = Unreliable;
  43. }
  44. // -- helpers for float conversion without allocations --
  45. [StructLayout(LayoutKind.Explicit)]
  46. internal struct UIntFloat
  47. {
  48. [FieldOffset(0)]
  49. public float floatValue;
  50. [FieldOffset(0)]
  51. public uint intValue;
  52. }
  53. [StructLayout(LayoutKind.Explicit)]
  54. internal struct UIntDouble
  55. {
  56. [FieldOffset(0)]
  57. public double doubleValue;
  58. [FieldOffset(0)]
  59. public ulong longValue;
  60. }
  61. [StructLayout(LayoutKind.Explicit)]
  62. internal struct UIntDecimal
  63. {
  64. [FieldOffset(0)]
  65. public ulong longValue1;
  66. [FieldOffset(8)]
  67. public ulong longValue2;
  68. [FieldOffset(0)]
  69. public decimal decimalValue;
  70. }
  71. public static class Utils
  72. {
  73. public static uint GetTrueRandomUInt()
  74. {
  75. // use Crypto RNG to avoid having time based duplicates
  76. using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
  77. {
  78. byte[] bytes = new byte[4];
  79. rng.GetBytes(bytes);
  80. return BitConverter.ToUInt32(bytes, 0);
  81. }
  82. }
  83. public static bool IsPrefab(GameObject obj)
  84. {
  85. #if UNITY_EDITOR
  86. return UnityEditor.PrefabUtility.IsPartOfPrefabAsset(obj);
  87. #else
  88. return false;
  89. #endif
  90. }
  91. public static bool IsSceneObjectWithPrefabParent(GameObject gameObject, out GameObject prefab)
  92. {
  93. prefab = null;
  94. #if UNITY_EDITOR
  95. if (!UnityEditor.PrefabUtility.IsPartOfPrefabInstance(gameObject))
  96. {
  97. return false;
  98. }
  99. prefab = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject);
  100. #endif
  101. if (prefab == null)
  102. {
  103. Debug.LogError("Failed to find prefab parent for scene object [name:" + gameObject.name + "]");
  104. return false;
  105. }
  106. return true;
  107. }
  108. // is a 2D point in screen? (from ummorpg)
  109. // (if width = 1024, then indices from 0..1023 are valid (=1024 indices)
  110. public static bool IsPointInScreen(Vector2 point) =>
  111. 0 <= point.x && point.x < Screen.width &&
  112. 0 <= point.y && point.y < Screen.height;
  113. }
  114. }