InterestManagement.cs 4.1 KB

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