Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@James-Frowen
Created May 28, 2021 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save James-Frowen/3c89bd92d3696bb53d0737d82b5b05a9 to your computer and use it in GitHub Desktop.
Save James-Frowen/3c89bd92d3696bb53d0737d82b5b05a9 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using UnityEngine;
namespace Mirror
{
/// <summary>
/// Component that controls visibility of networked objects between scenes.
/// <para>Any object with this component on it will only be visible to other objects in the same scene</para>
/// <para>This would be used when the server has multiple additive subscenes loaded to isolate players to their respective subscenes</para>
/// <para>Does not actively check if scene has changed</para>
/// </summary>
[DisallowMultipleComponent]
[AddComponentMenu("Network/NetworkSceneCheckerSimple")]
[RequireComponent(typeof(NetworkIdentity))]
[HelpURL("https://mirror-networking.com/docs/Components/NetworkSceneChecker.html")]
public class NetworkSceneCheckerSimple : NetworkVisibility
{
static readonly ILogger logger = LogFactory.GetLogger<NetworkSceneCheckerSimple>();
/// <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 player can see this object.</returns>
public override bool OnCheckObserver(NetworkConnection conn)
{
var player = conn.identity;
if (player == null)
{
if (logger.LogEnabled()) logger.LogWarning($"SceneChecker: {conn} had no player");
return false;
}
var playerScene = player.gameObject.scene;
if (!playerScene.IsValid())
{
if (logger.WarnEnabled()) logger.LogWarning($"SceneChecker: Could not find scene for {conn}");
return false;
}
var thisScene = this.gameObject.scene;
var visible = playerScene == thisScene;
if (logger.LogEnabled()) logger.LogWarning($"SceneChecker: {conn} can see '{this}': {visible}");
return visible;
}
/// <summary>
/// Callback used by the visibility system to (re)construct the set of observers that can see this object.
/// <para>Implementations of this callback should add network connections of players that can see this object to the observers set.</para>
/// </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)
{
foreach (var conn in NetworkServer.connections.Values)
{
if (this.OnCheckObserver(conn))
{
observers.Add(conn);
}
}
}
public override void OnSetHostVisibility(bool visible)
{
// nothing
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment