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