NetworkProximityChecker.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Mirror
  5. {
  6. /// <summary>
  7. /// Component that controls visibility of networked objects for players.
  8. /// <para>Any object with this component on it will not be visible to players more than a (configurable) distance away.</para>
  9. /// </summary>
  10. // Deprecated 2021-02-17
  11. [Obsolete(NetworkVisibilityObsoleteMessage.Message)]
  12. [AddComponentMenu("Network/NetworkProximityChecker")]
  13. [RequireComponent(typeof(NetworkIdentity))]
  14. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-proximity-checker")]
  15. public class NetworkProximityChecker : NetworkVisibility
  16. {
  17. /// <summary>
  18. /// The maximum range that objects will be visible at.
  19. /// </summary>
  20. [Tooltip("The maximum range that objects will be visible at.")]
  21. public int visRange = 10;
  22. /// <summary>
  23. /// How often (in seconds) that this object should update the list of observers that can see it.
  24. /// </summary>
  25. [Tooltip("How often (in seconds) that this object should update the list of observers that can see it.")]
  26. public float visUpdateInterval = 1;
  27. /// <summary>
  28. /// Flag to force this object to be hidden for players.
  29. /// <para>If this object is a player object, it will not be hidden for that player.</para>
  30. /// </summary>
  31. // Deprecated 2021-02-17
  32. [Obsolete("Use NetworkIdentity.visible mode instead of forceHidden!")]
  33. public bool forceHidden
  34. {
  35. get => netIdentity.visible == Visibility.ForceHidden;
  36. set => netIdentity.visible = value ? Visibility.ForceHidden : Visibility.Default;
  37. }
  38. public override void OnStartServer()
  39. {
  40. InvokeRepeating(nameof(RebuildObservers), 0, visUpdateInterval);
  41. }
  42. public override void OnStopServer()
  43. {
  44. CancelInvoke(nameof(RebuildObservers));
  45. }
  46. void RebuildObservers()
  47. {
  48. netIdentity.RebuildObservers(false);
  49. }
  50. /// <summary>
  51. /// Callback used by the visibility system to determine if an observer (player) can see this object.
  52. /// <para>If this function returns true, the network connection will be added as an observer.</para>
  53. /// </summary>
  54. /// <param name="conn">Network connection of a player.</param>
  55. /// <returns>True if the player can see this object.</returns>
  56. public override bool OnCheckObserver(NetworkConnection conn)
  57. {
  58. if (forceHidden)
  59. return false;
  60. return Vector3.Distance(conn.identity.transform.position, transform.position) < visRange;
  61. }
  62. /// <summary>
  63. /// Callback used by the visibility system to (re)construct the set of observers that can see this object.
  64. /// <para>Implementations of this callback should add network connections of players that can see this object to the observers set.</para>
  65. /// </summary>
  66. /// <param name="observers">The new set of observers for this object.</param>
  67. /// <param name="initialize">True if the set of observers is being built for the first time.</param>
  68. public override void OnRebuildObservers(HashSet<NetworkConnection> observers, bool initialize)
  69. {
  70. // if force hidden then return without adding any observers.
  71. if (forceHidden)
  72. return;
  73. // 'transform.' calls GetComponent, only do it once
  74. Vector3 position = transform.position;
  75. // brute force distance check
  76. // -> only player connections can be observers, so it's enough if we
  77. // go through all connections instead of all spawned identities.
  78. // -> compared to UNET's sphere cast checking, this one is orders of
  79. // magnitude faster. if we have 10k monsters and run a sphere
  80. // cast 10k times, we will see a noticeable lag even with physics
  81. // layers. but checking to every connection is fast.
  82. foreach (NetworkConnectionToClient conn in NetworkServer.connections.Values)
  83. {
  84. if (conn != null && conn.identity != null)
  85. {
  86. // check distance
  87. if (Vector3.Distance(conn.identity.transform.position, position) < visRange)
  88. {
  89. observers.Add(conn);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }