Skip to content

Commit a7e8b19

Browse files
committed
feat: InterestManagementBase
A interest management base class that allows more advanced interest management by bypassing the built-in HashSet checks via Rebuild overriding
1 parent c2d8343 commit a7e8b19

5 files changed

Lines changed: 98 additions & 57 deletions

File tree

Assets/Mirror/Core/InterestManagement.cs

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,12 @@ namespace Mirror
88
{
99
[DisallowMultipleComponent]
1010
[HelpURL("https://mirror-networking.gitbook.io/docs/guides/interest-management")]
11-
public abstract class InterestManagement : MonoBehaviour
11+
public abstract class InterestManagement : InterestManagementBase
1212
{
1313
// allocate newObservers helper HashSet
1414
readonly HashSet<NetworkConnectionToClient> newObservers =
1515
new HashSet<NetworkConnectionToClient>();
1616

17-
// Awake configures InterestManagement in NetworkServer/Client
18-
// Do NOT check for active server or client here.
19-
// Awake must always set the static aoi references.
20-
// make sure to call base.Awake when overwriting!
21-
protected virtual void Awake()
22-
{
23-
if (NetworkServer.aoi == null)
24-
{
25-
NetworkServer.aoi = this;
26-
}
27-
else Debug.LogError($"Only one InterestManagement component allowed. {NetworkServer.aoi.GetType()} has been set up already.");
28-
29-
if (NetworkClient.aoi == null)
30-
{
31-
NetworkClient.aoi = this;
32-
}
33-
else Debug.LogError($"Only one InterestManagement component allowed. {NetworkClient.aoi.GetType()} has been set up already.");
34-
}
35-
36-
[ServerCallback]
37-
public virtual void Reset() {}
38-
39-
// Callback used by the visibility system to determine if an observer
40-
// (player) can see the NetworkIdentity. If this function returns true,
41-
// the network connection will be added as an observer.
42-
// conn: Network connection of a player.
43-
// returns True if the player can see this object.
44-
public abstract bool OnCheckObserver(NetworkIdentity identity, NetworkConnectionToClient newObserver);
4517

4618
// rebuild observers for the given NetworkIdentity.
4719
// Server will automatically spawn/despawn added/removed ones.
@@ -77,32 +49,7 @@ protected void RebuildAll()
7749
}
7850
}
7951

80-
// Callback used by the visibility system for objects on a host.
81-
// Objects on a host (with a local client) cannot be disabled or
82-
// destroyed when they are not visible to the local client. So this
83-
// function is called to allow custom code to hide these objects. A
84-
// typical implementation will disable renderer components on the
85-
// object. This is only called on local clients on a host.
86-
// => need the function in here and virtual so people can overwrite!
87-
// => not everyone wants to hide renderers!
88-
[ServerCallback]
89-
public virtual void SetHostVisibility(NetworkIdentity identity, bool visible)
90-
{
91-
foreach (Renderer rend in identity.GetComponentsInChildren<Renderer>())
92-
rend.enabled = visible;
93-
}
94-
95-
/// <summary>Called on the server when a new networked object is spawned.</summary>
96-
// (useful for 'only rebuild if changed' interest management algorithms)
97-
[ServerCallback]
98-
public virtual void OnSpawned(NetworkIdentity identity) {}
99-
100-
/// <summary>Called on the server when a networked object is destroyed.</summary>
101-
// (useful for 'only rebuild if changed' interest management algorithms)
102-
[ServerCallback]
103-
public virtual void OnDestroyed(NetworkIdentity identity) {}
104-
105-
public void Rebuild(NetworkIdentity identity, bool initialize)
52+
public override void Rebuild(NetworkIdentity identity, bool initialize)
10653
{
10754
// clear newObservers hashset before using it
10855
newObservers.Clear();
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

Assets/Mirror/Core/InterestManagementBase.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Mirror/Core/NetworkClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static partial class NetworkClient
115115

116116
// interest management component (optional)
117117
// only needed for SetHostVisibility
118-
public static InterestManagement aoi;
118+
public static InterestManagementBase aoi;
119119

120120
// scene loading
121121
public static bool isLoadingScene;

Assets/Mirror/Core/NetworkServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static partial class NetworkServer
6868

6969
// interest management component (optional)
7070
// by default, everyone observes everyone
71-
public static InterestManagement aoi;
71+
public static InterestManagementBase aoi;
7272

7373
// OnConnected / OnDisconnected used to be NetworkMessages that were
7474
// invoked. this introduced a bug where external clients could send

0 commit comments

Comments
 (0)