mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
perf: Distance Interest Management caches custom ranges to avoid runtime TryGetComponent overhead (#3372)
* fix: Distance Interest Mgmt static dictionary * Clear on Reset * Allow for changing range at runtime * Use OnSpawned / OnDestroyed * reverted default visRange * Update DistanceInterestManagement.cs * Update DistanceInterestManagement.cs --------- Co-authored-by: mischa <16416509+vis2k@users.noreply.github.com>
This commit is contained in:
parent
340a4b907a
commit
229a1a6509
@ -14,16 +14,32 @@ public class DistanceInterestManagement : InterestManagement
|
|||||||
public float rebuildInterval = 1;
|
public float rebuildInterval = 1;
|
||||||
double lastRebuildTime;
|
double lastRebuildTime;
|
||||||
|
|
||||||
|
// cache custom ranges to avoid runtime TryGetComponent lookups
|
||||||
|
readonly Dictionary<NetworkIdentity, DistanceInterestManagementCustomRange> CustomRanges = new Dictionary<NetworkIdentity, DistanceInterestManagementCustomRange>();
|
||||||
|
|
||||||
// helper function to get vis range for a given object, or default.
|
// helper function to get vis range for a given object, or default.
|
||||||
|
[ServerCallback]
|
||||||
int GetVisRange(NetworkIdentity identity)
|
int GetVisRange(NetworkIdentity identity)
|
||||||
{
|
{
|
||||||
return identity.TryGetComponent(out DistanceInterestManagementCustomRange custom) ? custom.visRange : visRange;
|
return CustomRanges.TryGetValue(identity, out DistanceInterestManagementCustomRange custom) ? custom.visRange : visRange;
|
||||||
}
|
}
|
||||||
|
|
||||||
[ServerCallback]
|
[ServerCallback]
|
||||||
public override void Reset()
|
public override void Reset()
|
||||||
{
|
{
|
||||||
lastRebuildTime = 0D;
|
lastRebuildTime = 0D;
|
||||||
|
CustomRanges.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnSpawned(NetworkIdentity identity)
|
||||||
|
{
|
||||||
|
if (identity.TryGetComponent(out DistanceInterestManagementCustomRange custom))
|
||||||
|
CustomRanges[identity] = custom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnDestroyed(NetworkIdentity identity)
|
||||||
|
{
|
||||||
|
CustomRanges.Remove(identity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnectionToClient newObserver)
|
public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnectionToClient newObserver)
|
||||||
|
Loading…
Reference in New Issue
Block a user