123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System.Collections.Generic;
- using UnityEngine;
- namespace Mirror
- {
- public class DistanceInterestManagement : InterestManagement
- {
- [Tooltip("The maximum range that objects will be visible at. Add DistanceInterestManagementCustomRange onto NetworkIdentities for custom ranges.")]
- public int visRange = 10;
- [Tooltip("Rebuild all every 'rebuildInterval' seconds.")]
- public float rebuildInterval = 1;
- double lastRebuildTime;
-
- int GetVisRange(NetworkIdentity identity)
- {
- DistanceInterestManagementCustomRange custom = identity.GetComponent<DistanceInterestManagementCustomRange>();
- return custom != null ? custom.visRange : visRange;
- }
- public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnection newObserver)
- {
- int range = GetVisRange(identity);
- return Vector3.Distance(identity.transform.position, newObserver.identity.transform.position) < range;
- }
- public override void OnRebuildObservers(NetworkIdentity identity, HashSet<NetworkConnection> newObservers, bool initialize)
- {
-
- int range = GetVisRange(identity);
- Vector3 position = identity.transform.position;
-
-
-
-
-
-
-
- foreach (NetworkConnectionToClient conn in NetworkServer.connections.Values)
- {
-
- if (conn != null && conn.isAuthenticated && conn.identity != null)
- {
-
- if (Vector3.Distance(conn.identity.transform.position, position) < range)
- {
- newObservers.Add(conn);
- }
- }
- }
- }
- void Update()
- {
-
- if (!NetworkServer.active) return;
-
- if (NetworkTime.localTime >= lastRebuildTime + rebuildInterval)
- {
- RebuildAll();
- lastRebuildTime = NetworkTime.localTime;
- }
- }
- }
- }
|