From 55decc95f712ceb272fb1bb4dc26411b539e8335 Mon Sep 17 00:00:00 2001 From: Chris Langsenkamp Date: Mon, 29 Jul 2019 16:22:22 -0400 Subject: [PATCH] NetworkProximityChecker --- .../Components/NetworkProximityChecker.cs | 108 ++++++++++++------ 1 file changed, 71 insertions(+), 37 deletions(-) diff --git a/Assets/Mirror/Components/NetworkProximityChecker.cs b/Assets/Mirror/Components/NetworkProximityChecker.cs index 1ff3e9ea2..43d30ea3a 100644 --- a/Assets/Mirror/Components/NetworkProximityChecker.cs +++ b/Assets/Mirror/Components/NetworkProximityChecker.cs @@ -8,26 +8,47 @@ namespace Mirror [HelpURL("https://vis2k.github.io/Mirror/Components/NetworkProximityChecker")] public class NetworkProximityChecker : NetworkBehaviour { + /// + /// Enumeration of methods to use to check proximity. + /// public enum CheckMethod { Physics3D, Physics2D } + /// + /// The maximim range that objects will be visible at. + /// [Tooltip("The maximum range that objects will be visible at.")] public int visRange = 10; - // how often to refresh the list of observers, in seconds - [Tooltip("How often (in seconds) that this object should update the set of players that can see it.")] + /// + /// How often (in seconds) that this object should update the list of observers that can see it. + /// + [Tooltip("How often (in seconds) that this object should update the list of observers that can see it.")] public float visUpdateInterval = 1; - [Tooltip("Which method to use for checking proximity of players.\n\nPhysics3D uses 3D physics to determine proximity.\n\nPhysics2D uses 2D physics to determine proximity.")] + /// + /// Which method to use for checking proximity of players. + /// Physics3D uses 3D physics to determine proximity. + /// Physics2D uses 2D physics to determine proximity. + /// + [Tooltip("Which method to use for checking proximity of players.\n\nPhysics3D uses 3D physics to determine proximity.\nPhysics2D uses 2D physics to determine proximity.")] public CheckMethod checkMethod = CheckMethod.Physics3D; + /// + /// Flag to force this object to be hidden for players. + /// If this object is a player object, it will not be hidden for that player. + /// [Tooltip("Enable to force this object to be hidden from players.")] public bool forceHidden; - // ~0 means 'Everything'. layers are used anyway, might as well expose them to the user. + // Layers are used anyway, might as well expose them to the user. + /// + /// Select only the Player's layer to avoid unnecessary SphereCasts against the Terrain, etc. + /// ~0 means 'Everything'. + /// [Tooltip("Select only the Player's layer to avoid unnecessary SphereCasts against the Terrain, etc.")] public LayerMask castLayers = ~0; @@ -53,7 +74,11 @@ void Update() } } - // called when a new player enters + /// + /// Called when a new player enters + /// + /// + /// public override bool OnCheckObserver(NetworkConnection newObserver) { if (forceHidden) @@ -62,6 +87,12 @@ public override bool OnCheckObserver(NetworkConnection newObserver) return Vector3.Distance(newObserver.playerController.transform.position, transform.position) < visRange; } + /// + /// Called when a new player enters, and when scene changes occur + /// + /// + /// + /// public override bool OnRebuildObservers(HashSet observers, bool initial) { // if force hidden then return without adding any observers. @@ -69,51 +100,51 @@ public override bool OnRebuildObservers(HashSet observers, bo // always return true when overwriting OnRebuildObservers so that // Mirror knows not to use the built in rebuild method. return true; - + // find players within range switch (checkMethod) { case CheckMethod.Physics3D: - { - // cast without allocating GC for maximum performance - int hitCount = Physics.OverlapSphereNonAlloc(transform.position, visRange, hitsBuffer3D, castLayers); - if (hitCount == hitsBuffer3D.Length) Debug.LogWarning("NetworkProximityChecker's OverlapSphere test for " + name + " has filled the whole buffer(" + hitsBuffer3D.Length + "). Some results might have been omitted. Consider increasing buffer size."); - - for (int i = 0; i < hitCount; i++) { - Collider hit = hitsBuffer3D[i]; - // collider might be on pelvis, often the NetworkIdentity is in a parent - // (looks in the object itself and then parents) - NetworkIdentity identity = hit.GetComponentInParent(); - // (if an object has a connectionToClient, it is a player) - if (identity != null && identity.connectionToClient != null) + // cast without allocating GC for maximum performance + int hitCount = Physics.OverlapSphereNonAlloc(transform.position, visRange, hitsBuffer3D, castLayers); + if (hitCount == hitsBuffer3D.Length) Debug.LogWarning("NetworkProximityChecker's OverlapSphere test for " + name + " has filled the whole buffer(" + hitsBuffer3D.Length + "). Some results might have been omitted. Consider increasing buffer size."); + + for (int i = 0; i < hitCount; i++) { - observers.Add(identity.connectionToClient); + Collider hit = hitsBuffer3D[i]; + // collider might be on pelvis, often the NetworkIdentity is in a parent + // (looks in the object itself and then parents) + NetworkIdentity identity = hit.GetComponentInParent(); + // (if an object has a connectionToClient, it is a player) + if (identity != null && identity.connectionToClient != null) + { + observers.Add(identity.connectionToClient); + } } + break; } - break; - } case CheckMethod.Physics2D: - { - // cast without allocating GC for maximum performance - int hitCount = Physics2D.OverlapCircleNonAlloc(transform.position, visRange, hitsBuffer2D, castLayers); - if (hitCount == hitsBuffer2D.Length) Debug.LogWarning("NetworkProximityChecker's OverlapCircle test for " + name + " has filled the whole buffer(" + hitsBuffer2D.Length + "). Some results might have been omitted. Consider increasing buffer size."); - - for (int i = 0; i < hitCount; i++) { - Collider2D hit = hitsBuffer2D[i]; - // collider might be on pelvis, often the NetworkIdentity is in a parent - // (looks in the object itself and then parents) - NetworkIdentity identity = hit.GetComponentInParent(); - // (if an object has a connectionToClient, it is a player) - if (identity != null && identity.connectionToClient != null) + // cast without allocating GC for maximum performance + int hitCount = Physics2D.OverlapCircleNonAlloc(transform.position, visRange, hitsBuffer2D, castLayers); + if (hitCount == hitsBuffer2D.Length) Debug.LogWarning("NetworkProximityChecker's OverlapCircle test for " + name + " has filled the whole buffer(" + hitsBuffer2D.Length + "). Some results might have been omitted. Consider increasing buffer size."); + + for (int i = 0; i < hitCount; i++) { - observers.Add(identity.connectionToClient); + Collider2D hit = hitsBuffer2D[i]; + // collider might be on pelvis, often the NetworkIdentity is in a parent + // (looks in the object itself and then parents) + NetworkIdentity identity = hit.GetComponentInParent(); + // (if an object has a connectionToClient, it is a player) + if (identity != null && identity.connectionToClient != null) + { + observers.Add(identity.connectionToClient); + } } + break; } - break; - } } // always return true when overwriting OnRebuildObservers so that @@ -121,7 +152,10 @@ public override bool OnRebuildObservers(HashSet observers, bo return true; } - // called hiding and showing objects on the host + /// + /// Called when hiding and showing objects on the host + /// + /// public override void OnSetLocalVisibility(bool visible) { foreach (Renderer rend in GetComponentsInChildren())