Skip to content

Commit

Permalink
feat: adding event that is invoked when object visibility changes
Browse files Browse the repository at this point in the history
adding event on NetworkVisibility that can be used to do extra stuff before show, hide or spawn message is sent ot client
  • Loading branch information
James-Frowen committed Feb 10, 2023
1 parent 14283b1 commit d82cd76
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Assets/Mirage/Runtime/NetworkVisibility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;

namespace Mirage
Expand All @@ -12,6 +12,19 @@ namespace Mirage
[DisallowMultipleComponent]
public abstract class NetworkVisibility : NetworkBehaviour
{
public delegate void VisibilityChanged(INetworkPlayer player, bool visible);

/// <summary>
/// Invoked on server when visibility changes for player
/// <para>Invoked before Show/Hide/Spawn message is sent to client</para>
/// </summary>
public event VisibilityChanged OnVisibilityChanged;

internal void InvokeVisibilityChanged(INetworkPlayer player, bool visible)
{
OnVisibilityChanged?.Invoke(player, visible);
}

/// <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>
Expand Down
8 changes: 8 additions & 0 deletions Assets/Mirage/Runtime/ServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,21 @@ private void Respawn(NetworkIdentity identity)
/// <param name="player"></param>
internal void ShowToPlayer(NetworkIdentity identity, INetworkPlayer player)
{
var visiblity = identity.Visibility;
if (visiblity != null)
visiblity.InvokeVisibilityChanged(player, true);

// dont send if loading scene
if (player.SceneIsReady)
SendSpawnMessage(identity, player);
}

internal void HideToPlayer(NetworkIdentity identity, INetworkPlayer player)
{
var visiblity = identity.Visibility;
if (visiblity != null)
visiblity.InvokeVisibilityChanged(player, false);

player.Send(new ObjectHideMessage { netId = identity.NetId });
}

Expand Down

0 comments on commit d82cd76

Please sign in to comment.