InterestManagement.cs 4.4 KB

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