Mirror Networking
InterestManagement.cs
1// interest management component for custom solutions like
2// distance based, spatial hashing, raycast based, etc.
3using System.Collections.Generic;
4using UnityEngine;
5
6namespace Mirror
7{
8 [DisallowMultipleComponent]
9 [HelpURL("https://mirror-networking.gitbook.io/docs/guides/interest-management")]
10 public abstract class InterestManagement : MonoBehaviour
11 {
12 // Awake configures InterestManagement in NetworkServer/Client
13 // Do NOT check for active server or client here.
14 // Awake must always set the static aoi references.
15 void Awake()
16 {
17 if (NetworkServer.aoi == null)
18 {
19 NetworkServer.aoi = this;
20 }
21 else Debug.LogError($"Only one InterestManagement component allowed. {NetworkServer.aoi.GetType()} has been set up already.");
22
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
30 [ServerCallback]
31 public virtual void Reset() {}
32
33 // Callback used by the visibility system to determine if an observer
34 // (player) can see the NetworkIdentity. If this function returns true,
35 // the network connection will be added as an observer.
36 // conn: Network connection of a player.
37 // returns True if the player can see this object.
38 public abstract bool OnCheckObserver(NetworkIdentity identity, NetworkConnectionToClient newObserver);
39
40 // rebuild observers for the given NetworkIdentity.
41 // Server will automatically spawn/despawn added/removed ones.
42 // newObservers: cached hashset to put the result into
43 // initialize: true if being rebuilt for the first time
44 //
45 // IMPORTANT:
46 // => global rebuild would be more simple, BUT
47 // => local rebuild is way faster for spawn/despawn because we can
48 // simply rebuild a select NetworkIdentity only
49 // => having both .observers and .observing is necessary for local
50 // rebuilds
51 //
52 // in other words, this is the perfect solution even though it's not
53 // completely simple (due to .observers & .observing).
54 //
55 // Mirror maintains .observing automatically in the background. best of
56 // both worlds without any worrying now!
57 public abstract void OnRebuildObservers(NetworkIdentity identity, HashSet<NetworkConnectionToClient> newObservers);
58
59 // helper function to trigger a full rebuild.
60 // most implementations should call this in a certain interval.
61 // some might call this all the time, or only on team changes or
62 // scene changes and so on.
63 //
64 // IMPORTANT: check if NetworkServer.active when using Update()!
65 [ServerCallback]
66 protected void RebuildAll()
67 {
68 foreach (NetworkIdentity identity in NetworkServer.spawned.Values)
69 {
70 NetworkServer.RebuildObservers(identity, false);
71 }
72 }
73
74 // Callback used by the visibility system for objects on a host.
75 // Objects on a host (with a local client) cannot be disabled or
76 // destroyed when they are not visible to the local client. So this
77 // function is called to allow custom code to hide these objects. A
78 // typical implementation will disable renderer components on the
79 // object. This is only called on local clients on a host.
80 // => need the function in here and virtual so people can overwrite!
81 // => not everyone wants to hide renderers!
82 [ServerCallback]
83 public virtual void SetHostVisibility(NetworkIdentity identity, bool visible)
84 {
85 foreach (Renderer rend in identity.GetComponentsInChildren<Renderer>())
86 rend.enabled = visible;
87 }
88
90 // (useful for 'only rebuild if changed' interest management algorithms)
91 [ServerCallback]
92 public virtual void OnSpawned(NetworkIdentity identity) {}
93
95 // (useful for 'only rebuild if changed' interest management algorithms)
96 [ServerCallback]
97 public virtual void OnDestroyed(NetworkIdentity identity) {}
98 }
99}
virtual void OnDestroyed(NetworkIdentity identity)
Called on the server when a networked object is destroyed.
virtual void OnSpawned(NetworkIdentity identity)
Called on the server when a new networked object is spawned.
NetworkClient with connection to server.
NetworkIdentity identifies objects across the network.
NetworkServer handles remote connections and has a local connection for a local client.
static readonly Dictionary< uint, NetworkIdentity > spawned
All spawned NetworkIdentities by netId.