Mirror Networking
Utils.cs
1using System;
2using System.Runtime.CompilerServices;
3using System.Security.Cryptography;
4using UnityEngine;
5
6namespace Mirror
7{
8 // Handles network messages on client and server
9 public delegate void NetworkMessageDelegate(NetworkConnection conn, NetworkReader reader, int channelId);
10
11 // Handles requests to spawn objects on the client
12 public delegate GameObject SpawnDelegate(Vector3 position, Guid assetId);
13
14 public delegate GameObject SpawnHandlerDelegate(SpawnMessage msg);
15
16 // Handles requests to unspawn objects on the client
17 public delegate void UnSpawnDelegate(GameObject spawned);
18
19 // channels are const ints instead of an enum so people can add their own
20 // channels (can't extend an enum otherwise).
21 //
22 // note that Mirror is slowly moving towards quake style networking which
23 // will only require reliable for handshake, and unreliable for the rest.
24 // so eventually we can change this to an Enum and transports shouldn't
25 // add custom channels anymore.
26 public static class Channels
27 {
28 public const int Reliable = 0; // ordered
29 public const int Unreliable = 1; // unordered
30 }
31
32 public static class Utils
33 {
34 public static uint GetTrueRandomUInt()
35 {
36 // use Crypto RNG to avoid having time based duplicates
37 using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
38 {
39 byte[] bytes = new byte[4];
40 rng.GetBytes(bytes);
41 return BitConverter.ToUInt32(bytes, 0);
42 }
43 }
44
45 public static bool IsPrefab(GameObject obj)
46 {
47#if UNITY_EDITOR
48 return UnityEditor.PrefabUtility.IsPartOfPrefabAsset(obj);
49#else
50 return false;
51#endif
52 }
53
54 public static bool IsSceneObjectWithPrefabParent(GameObject gameObject, out GameObject prefab)
55 {
56 prefab = null;
57
58#if UNITY_EDITOR
59 if (!UnityEditor.PrefabUtility.IsPartOfPrefabInstance(gameObject))
60 {
61 return false;
62 }
63 prefab = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject);
64#endif
65
66 if (prefab == null)
67 {
68 Debug.LogError($"Failed to find prefab parent for scene object [name:{gameObject.name}]");
69 return false;
70 }
71 return true;
72 }
73
74 // is a 2D point in screen? (from ummorpg)
75 // (if width = 1024, then indices from 0..1023 are valid (=1024 indices)
76 [MethodImpl(MethodImplOptions.AggressiveInlining)]
77 public static bool IsPointInScreen(Vector2 point) =>
78 0 <= point.x && point.x < Screen.width &&
79 0 <= point.y && point.y < Screen.height;
80
81 // pretty print bytes as KB/MB/GB/etc. from DOTSNET
82 // long to support > 2GB
83 // divides by floats to return "2.5MB" etc.
84 public static string PrettyBytes(long bytes)
85 {
86 // bytes
87 if (bytes < 1024)
88 return $"{bytes} B";
89 // kilobytes
90 else if (bytes < 1024L * 1024L)
91 return $"{(bytes / 1024f):F2} KB";
92 // megabytes
93 else if (bytes < 1024 * 1024L * 1024L)
94 return $"{(bytes / (1024f * 1024f)):F2} MB";
95 // gigabytes
96 return $"{(bytes / (1024f * 1024f * 1024f)):F2} GB";
97 }
98
99 // universal .spawned function
100 [MethodImpl(MethodImplOptions.AggressiveInlining)]
101 public static NetworkIdentity GetSpawnedInServerOrClient(uint netId)
102 {
103 // server / host mode: use the one from server.
104 // host mode has access to all spawned.
106 {
107 NetworkServer.spawned.TryGetValue(netId, out NetworkIdentity entry);
108 return entry;
109 }
110
111 // client
113 {
114 NetworkClient.spawned.TryGetValue(netId, out NetworkIdentity entry);
115 return entry;
116 }
117
118 return null;
119 }
120 }
121}
NetworkClient with connection to server.
static bool active
active is true while a client is connecting/connected
static readonly Dictionary< uint, NetworkIdentity > spawned
All spawned NetworkIdentities by netId.
NetworkIdentity identifies objects across the network.
NetworkServer handles remote connections and has a local connection for a local client.
static bool active
active checks if the server has been started
static readonly Dictionary< uint, NetworkIdentity > spawned
All spawned NetworkIdentities by netId.