diff --git a/Assets/Mirror/Components/NetworkOwnerChecker.cs b/Assets/Mirror/Components/NetworkOwnerChecker.cs new file mode 100644 index 000000000..491ad165f --- /dev/null +++ b/Assets/Mirror/Components/NetworkOwnerChecker.cs @@ -0,0 +1,42 @@ +using UnityEngine; +using System.Collections.Generic; + +namespace Mirror +{ + /// + /// Component that limits visibility of networked objects to the authority client. + /// Any object with this component on it will only be visible to the client that has been assigned authority for it. + /// This would be used for spawning a non-player networked object for single client to interact with, e.g. in-game puzzles. + /// + [DisallowMultipleComponent] + [AddComponentMenu("Network/NetworkOwnerChecker")] + [RequireComponent(typeof(NetworkIdentity))] + [HelpURL("https://mirror-networking.com/docs/Components/NetworkOwnerChecker.html")] + public class NetworkOwnerChecker : NetworkVisibility + { + static readonly ILogger logger = LogFactory.GetLogger(typeof(NetworkSceneChecker)); + + /// + /// Callback used by the visibility system to determine if an observer (player) can see this object. + /// If this function returns true, the network connection will be added as an observer. + /// + /// Network connection of a player. + /// True if the client is the owner of this object. + public override bool OnCheckObserver(NetworkConnection conn) + { + if (logger.LogEnabled()) logger.Log($"OnCheckObserver {netIdentity.connectionToClient} {conn}"); + + return (netIdentity.connectionToClient == conn); + } + + /// + /// Callback used by the visibility system to (re)construct the set of observers that can see this object. + /// + /// The new set of observers for this object. + /// True if the set of observers is being built for the first time. + public override void OnRebuildObservers(HashSet observers, bool initialize) + { + // Do nothing here because the authority client is always added as an observer internally. + } + } +} diff --git a/Assets/Mirror/Components/NetworkOwnerChecker.cs.meta b/Assets/Mirror/Components/NetworkOwnerChecker.cs.meta new file mode 100644 index 000000000..fee7725c8 --- /dev/null +++ b/Assets/Mirror/Components/NetworkOwnerChecker.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 25fd0c51bbe07c140bc30978b91e9182 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3} + userData: + assetBundleName: + assetBundleVariant: