using System.Collections.Generic; using UnityEngine; using Mirror; /* Documentation: https://mirror-networking.gitbook.io/docs/guides/interest-management API Reference: https://mirror-networking.com/docs/api/Mirror.InterestManagement.html */ // NOTE: Attach this component to the same object as your Network Manager. public class #SCRIPTNAME# : InterestManagement { /// /// Callback used by the visibility system to determine if an observer (client) can see the NetworkIdentity. /// If this function returns true, the network connection will be added as an observer. /// /// Object to be observed (or not) by a client /// Network Connection of a client. /// True if the client can see this object. [ServerCallback] public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnectionToClient newObserver) { // Default behaviour of making the identity object visible to all clients. // Replace this code with your own logic as appropriate. return true; } /// /// Callback used by the visibility system to determine if an observer (client) can see the NetworkIdentity. /// Add connections to newObservers that should see the identity object. /// /// Object to be observed (or not) by clients /// cached hashset to put the result into /// true if being rebuilt for the first time [ServerCallback] public override void OnRebuildObservers(NetworkIdentity identity, HashSet newObservers, bool initialize) { // Default behaviour of making the identity object visible to all clients. // Replace this code with your own logic as appropriate. foreach (NetworkConnectionToClient conn in NetworkServer.connections.Values) newObservers.Add(conn); } /// /// Called on the server when a new networked object is spawned. /// /// NetworkIdentity of the object being spawned [ServerCallback] public override void OnSpawned(NetworkIdentity identity) { } /// /// Called on the server when a networked object is destroyed. /// /// NetworkIdentity of the object being destroyed [ServerCallback] public override void OnDestroyed(NetworkIdentity identity) { } /// /// Callback used by the visibility system for objects on a host. /// Objects on a host (with a local client) cannot be disabled or destroyed when /// they are not visible to the local client, so this function is called to allow /// custom code to hide these objects. /// A typical implementation will disable renderer components on the object. /// This is only called on local clients on a host. /// /// NetworkIdentity of the object being considered for visibility /// True if the identity object should be visible to the host client [ServerCallback] public override void SetHostVisibility(NetworkIdentity identity, bool visible) { base.SetHostVisibility(identity, visible); } /// /// Called by NetworkServer in Initialize and Shutdown /// [ServerCallback] public override void Reset() { } [ServerCallback] void Update() { // Here is where you'd need to evaluate if observers need to be rebuilt, // either for a specific object, a subset of objects, or all objects. // Review the code in the various Interest Management components // included with Mirror for inspiration: // - Distance Interest Management // - Spatial Hash Interest Management // - Scene Interest Management // - Match Interest Management // - Team Interest Management } }