InterestManagement.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // interest management component for custom solutions like
  2. // distance based, spatial hashing, raycast based, etc.
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Mirror
  6. {
  7. [DisallowMultipleComponent]
  8. [HelpURL("https://mirror-networking.gitbook.io/docs/guides/interest-management")]
  9. public abstract class InterestManagement : MonoBehaviour
  10. {
  11. // Awake configures InterestManagement in NetworkServer/Client
  12. // Do NOT check for active server or client here.
  13. // Awake must always set the static aoi references.
  14. // make sure to call base.Awake when overwriting!
  15. protected virtual void Awake()
  16. {
  17. if (NetworkServer.aoi == null)
  18. {
  19. NetworkServer.aoi = this;
  20. }
  21. else Debug.LogError($"Only one InterestManagement component allowed. {NetworkServer.aoi.GetType()} has been set up already.");
  22. if (NetworkClient.aoi == null)
  23. {
  24. NetworkClient.aoi = this;
  25. }
  26. else Debug.LogError($"Only one InterestManagement component allowed. {NetworkClient.aoi.GetType()} has been set up already.");
  27. }
  28. [ServerCallback]
  29. public virtual void Reset() {}
  30. // Callback used by the visibility system to determine if an observer
  31. // (player) can see the NetworkIdentity. If this function returns true,
  32. // the network connection will be added as an observer.
  33. // conn: Network connection of a player.
  34. // returns True if the player can see this object.
  35. public abstract bool OnCheckObserver(NetworkIdentity identity, NetworkConnectionToClient newObserver);
  36. // rebuild observers for the given NetworkIdentity.
  37. // Server will automatically spawn/despawn added/removed ones.
  38. // newObservers: cached hashset to put the result into
  39. // initialize: true if being rebuilt for the first time
  40. //
  41. // IMPORTANT:
  42. // => global rebuild would be more simple, BUT
  43. // => local rebuild is way faster for spawn/despawn because we can
  44. // simply rebuild a select NetworkIdentity only
  45. // => having both .observers and .observing is necessary for local
  46. // rebuilds
  47. //
  48. // in other words, this is the perfect solution even though it's not
  49. // completely simple (due to .observers & .observing).
  50. //
  51. // Mirror maintains .observing automatically in the background. best of
  52. // both worlds without any worrying now!
  53. public abstract void OnRebuildObservers(NetworkIdentity identity, HashSet<NetworkConnectionToClient> newObservers);
  54. // helper function to trigger a full rebuild.
  55. // most implementations should call this in a certain interval.
  56. // some might call this all the time, or only on team changes or
  57. // scene changes and so on.
  58. //
  59. // IMPORTANT: check if NetworkServer.active when using Update()!
  60. [ServerCallback]
  61. protected void RebuildAll()
  62. {
  63. foreach (NetworkIdentity identity in NetworkServer.spawned.Values)
  64. {
  65. NetworkServer.RebuildObservers(identity, false);
  66. }
  67. }
  68. // Callback used by the visibility system for objects on a host.
  69. // Objects on a host (with a local client) cannot be disabled or
  70. // destroyed when they are not visible to the local client. So this
  71. // function is called to allow custom code to hide these objects. A
  72. // typical implementation will disable renderer components on the
  73. // object. This is only called on local clients on a host.
  74. // => need the function in here and virtual so people can overwrite!
  75. // => not everyone wants to hide renderers!
  76. [ServerCallback]
  77. public virtual void SetHostVisibility(NetworkIdentity identity, bool visible)
  78. {
  79. foreach (Renderer rend in identity.GetComponentsInChildren<Renderer>())
  80. rend.enabled = visible;
  81. }
  82. /// <summary>Called on the server when a new networked object is spawned.</summary>
  83. // (useful for 'only rebuild if changed' interest management algorithms)
  84. [ServerCallback]
  85. public virtual void OnSpawned(NetworkIdentity identity) {}
  86. /// <summary>Called on the server when a networked object is destroyed.</summary>
  87. // (useful for 'only rebuild if changed' interest management algorithms)
  88. [ServerCallback]
  89. public virtual void OnDestroyed(NetworkIdentity identity) {}
  90. }
  91. }