InterestManagementBase.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // interest management component for custom solutions like
  2. // distance based, spatial hashing, raycast based, etc.
  3. // low level base class allows for low level spatial hashing etc., which is 3-5x faster.
  4. using UnityEngine;
  5. namespace Mirror
  6. {
  7. [DisallowMultipleComponent]
  8. [HelpURL("https://mirror-networking.gitbook.io/docs/guides/interest-management")]
  9. public abstract class InterestManagementBase : MonoBehaviour
  10. {
  11. // Configures InterestManagementBase in NetworkServer/Client
  12. // Do NOT check for active server or client here.
  13. // OnEnable must always set the static aoi references.
  14. // make sure to call base.OnEnable when overwriting!
  15. // Previously used Awake()
  16. protected virtual void OnEnable()
  17. {
  18. if (NetworkServer.aoi == null)
  19. {
  20. NetworkServer.aoi = this;
  21. }
  22. else Debug.LogError($"Only one InterestManagement component allowed. {NetworkServer.aoi.GetType()} has been set up already.");
  23. if (NetworkClient.aoi == null)
  24. {
  25. NetworkClient.aoi = this;
  26. }
  27. else Debug.LogError($"Only one InterestManagement component allowed. {NetworkClient.aoi.GetType()} has been set up already.");
  28. }
  29. [ServerCallback]
  30. public virtual void Reset() {}
  31. // Callback used by the visibility system to determine if an observer
  32. // (player) can see the NetworkIdentity. If this function returns true,
  33. // the network connection will be added as an observer.
  34. // conn: Network connection of a player.
  35. // returns True if the player can see this object.
  36. public abstract bool OnCheckObserver(NetworkIdentity identity, NetworkConnectionToClient newObserver);
  37. // Callback used by the visibility system for objects on a host.
  38. // Objects on a host (with a local client) cannot be disabled or
  39. // destroyed when they are not visible to the local client. So this
  40. // function is called to allow custom code to hide these objects. A
  41. // typical implementation will disable renderer components on the
  42. // object. This is only called on local clients on a host.
  43. // => need the function in here and virtual so people can overwrite!
  44. // => not everyone wants to hide renderers!
  45. [ServerCallback]
  46. public virtual void SetHostVisibility(NetworkIdentity identity, bool visible)
  47. {
  48. foreach (Renderer rend in identity.GetComponentsInChildren<Renderer>())
  49. rend.enabled = visible;
  50. }
  51. /// <summary>Called on the server when a new networked object is spawned.</summary>
  52. // (useful for 'only rebuild if changed' interest management algorithms)
  53. [ServerCallback]
  54. public virtual void OnSpawned(NetworkIdentity identity) {}
  55. /// <summary>Called on the server when a networked object is destroyed.</summary>
  56. // (useful for 'only rebuild if changed' interest management algorithms)
  57. [ServerCallback]
  58. public virtual void OnDestroyed(NetworkIdentity identity) {}
  59. public abstract void Rebuild(NetworkIdentity identity, bool initialize);
  60. /// <summary>Adds the specified connection to the observers of identity</summary>
  61. protected void AddObserver(NetworkConnectionToClient connection, NetworkIdentity identity)
  62. {
  63. connection.AddToObserving(identity);
  64. identity.observers.Add(connection.connectionId, connection);
  65. }
  66. /// <summary>Removes the specified connection from the observers of identity</summary>
  67. protected void RemoveObserver(NetworkConnectionToClient connection, NetworkIdentity identity)
  68. {
  69. connection.RemoveFromObserving(identity, false);
  70. identity.observers.Remove(connection.connectionId);
  71. }
  72. }
  73. }