Utils.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // channels are const ints instead of an enum so people can add their own
  21. // channels (can't extend an enum otherwise).
  22. //
  23. // note that Mirror is slowly moving towards quake style networking which
  24. // will only require reliable for handshake, and unreliable for the rest.
  25. // so eventually we can change this to an Enum and transports shouldn't
  26. // add custom channels anymore.
  27. public static class Channels
  28. {
  29. public const int Reliable = 0; // ordered
  30. public const int Unreliable = 1; // unordered
  31. }
  32. // -- helpers for float conversion without allocations --
  33. [StructLayout(LayoutKind.Explicit)]
  34. internal struct UIntFloat
  35. {
  36. [FieldOffset(0)]
  37. public float floatValue;
  38. [FieldOffset(0)]
  39. public uint intValue;
  40. }
  41. [StructLayout(LayoutKind.Explicit)]
  42. internal struct UIntDouble
  43. {
  44. [FieldOffset(0)]
  45. public double doubleValue;
  46. [FieldOffset(0)]
  47. public ulong longValue;
  48. }
  49. [StructLayout(LayoutKind.Explicit)]
  50. internal struct UIntDecimal
  51. {
  52. [FieldOffset(0)]
  53. public ulong longValue1;
  54. [FieldOffset(8)]
  55. public ulong longValue2;
  56. [FieldOffset(0)]
  57. public decimal decimalValue;
  58. }
  59. public static class Utils
  60. {
  61. public static uint GetTrueRandomUInt()
  62. {
  63. // use Crypto RNG to avoid having time based duplicates
  64. using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
  65. {
  66. byte[] bytes = new byte[4];
  67. rng.GetBytes(bytes);
  68. return BitConverter.ToUInt32(bytes, 0);
  69. }
  70. }
  71. public static bool IsPrefab(GameObject obj)
  72. {
  73. #if UNITY_EDITOR
  74. return UnityEditor.PrefabUtility.IsPartOfPrefabAsset(obj);
  75. #else
  76. return false;
  77. #endif
  78. }
  79. public static bool IsSceneObjectWithPrefabParent(GameObject gameObject, out GameObject prefab)
  80. {
  81. prefab = null;
  82. #if UNITY_EDITOR
  83. if (!UnityEditor.PrefabUtility.IsPartOfPrefabInstance(gameObject))
  84. {
  85. return false;
  86. }
  87. prefab = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject);
  88. #endif
  89. if (prefab == null)
  90. {
  91. Debug.LogError($"Failed to find prefab parent for scene object [name:{gameObject.name}]");
  92. return false;
  93. }
  94. return true;
  95. }
  96. // is a 2D point in screen? (from ummorpg)
  97. // (if width = 1024, then indices from 0..1023 are valid (=1024 indices)
  98. public static bool IsPointInScreen(Vector2 point) =>
  99. 0 <= point.x && point.x < Screen.width &&
  100. 0 <= point.y && point.y < Screen.height;
  101. }
  102. }