feat: Added NetworkOwnerChecker Component (#2388)

* Added NetworkOwnerChecker component

* Simplified OnRebuildObservers

Co-authored-by: MrGadget1024 <chris@clevertech.net>
This commit is contained in:
MrGadget 2021-01-17 20:59:27 -05:00 committed by GitHub
parent 1f31897e92
commit fb874169f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,42 @@
using UnityEngine;
using System.Collections.Generic;
namespace Mirror
{
/// <summary>
/// Component that limits visibility of networked objects to the authority client.
/// <para>Any object with this component on it will only be visible to the client that has been assigned authority for it.</para>
/// <para>This would be used for spawning a non-player networked object for single client to interact with, e.g. in-game puzzles.</para>
/// </summary>
[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));
/// <summary>
/// Callback used by the visibility system to determine if an observer (player) can see this object.
/// <para>If this function returns true, the network connection will be added as an observer.</para>
/// </summary>
/// <param name="conn">Network connection of a player.</param>
/// <returns>True if the client is the owner of this object.</returns>
public override bool OnCheckObserver(NetworkConnection conn)
{
if (logger.LogEnabled()) logger.Log($"OnCheckObserver {netIdentity.connectionToClient} {conn}");
return (netIdentity.connectionToClient == conn);
}
/// <summary>
/// Callback used by the visibility system to (re)construct the set of observers that can see this object.
/// </summary>
/// <param name="observers">The new set of observers for this object.</param>
/// <param name="initialize">True if the set of observers is being built for the first time.</param>
public override void OnRebuildObservers(HashSet<NetworkConnection> observers, bool initialize)
{
// Do nothing here because the authority client is always added as an observer internally.
}
}
}

View File

@ -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: