53-Mirror__Custom Interest Management-CustomInterestManagement.cs.txt 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Mirror;
  4. /*
  5. Documentation: https://mirror-networking.gitbook.io/docs/guides/interest-management
  6. API Reference: https://mirror-networking.com/docs/api/Mirror.InterestManagement.html
  7. */
  8. // NOTE: Attach this component to the same object as your Network Manager.
  9. public class #SCRIPTNAME# : InterestManagement
  10. {
  11. /// <summary>
  12. /// Callback used by the visibility system to determine if an observer (client) can see the NetworkIdentity.
  13. /// If this function returns true, the network connection will be added as an observer.
  14. /// </summary>
  15. /// <param name="identity">Object to be observed (or not) by a client</param>
  16. /// <param name="newObserver">Network Connection of a client.</param>
  17. /// <returns>True if the client can see this object.</returns>
  18. [ServerCallback]
  19. public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnectionToClient newObserver)
  20. {
  21. // Default behaviour of making the identity object visible to all clients.
  22. // Replace this code with your own logic as appropriate.
  23. return true;
  24. }
  25. /// <summary>
  26. /// Callback used by the visibility system to determine if an observer (client) can see the NetworkIdentity.
  27. /// Add connections to newObservers that should see the identity object.
  28. /// </summary>
  29. /// <param name="identity">Object to be observed (or not) by clients</param>
  30. /// <param name="newObservers">cached hashset to put the result into</param>
  31. /// <param name="initialize">true if being rebuilt for the first time</param>
  32. [ServerCallback]
  33. public override void OnRebuildObservers(NetworkIdentity identity, HashSet<NetworkConnectionToClient> newObservers, bool initialize)
  34. {
  35. // Default behaviour of making the identity object visible to all clients.
  36. // Replace this code with your own logic as appropriate.
  37. foreach (NetworkConnectionToClient conn in NetworkServer.connections.Values)
  38. newObservers.Add(conn);
  39. }
  40. /// <summary>
  41. /// Called on the server when a new networked object is spawned.
  42. /// </summary>
  43. /// <param name="identity">NetworkIdentity of the object being spawned</param>
  44. [ServerCallback]
  45. public override void OnSpawned(NetworkIdentity identity) { }
  46. /// <summary>
  47. /// Called on the server when a networked object is destroyed.
  48. /// </summary>
  49. /// <param name="identity">NetworkIdentity of the object being destroyed</param>
  50. [ServerCallback]
  51. public override void OnDestroyed(NetworkIdentity identity) { }
  52. /// <summary>
  53. /// Callback used by the visibility system for objects on a host.
  54. /// Objects on a host (with a local client) cannot be disabled or destroyed when
  55. /// they are not visible to the local client, so this function is called to allow
  56. /// custom code to hide these objects.
  57. /// A typical implementation will disable renderer components on the object.
  58. /// This is only called on local clients on a host.
  59. /// </summary>
  60. /// <param name="identity">NetworkIdentity of the object being considered for visibility</param>
  61. /// <param name="visible">True if the identity object should be visible to the host client</param>
  62. [ServerCallback]
  63. public override void SetHostVisibility(NetworkIdentity identity, bool visible)
  64. {
  65. base.SetHostVisibility(identity, visible);
  66. }
  67. /// <summary>
  68. /// Called by NetworkServer in Initialize and Shutdown
  69. /// </summary>
  70. [ServerCallback]
  71. public override void Reset() { }
  72. [ServerCallback]
  73. void Update()
  74. {
  75. // Here is where you'd need to evaluate if observers need to be rebuilt,
  76. // either for a specific object, a subset of objects, or all objects.
  77. // Review the code in the various Interest Management components
  78. // included with Mirror for inspiration:
  79. // - Distance Interest Management
  80. // - Spatial Hash Interest Management
  81. // - Scene Interest Management
  82. // - Match Interest Management
  83. // - Team Interest Management
  84. }
  85. }