-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding component that can be used to disable renderers on host …
…player Disables renderers based on if the object is visible to the host player or not
- Loading branch information
1 parent
d82cd76
commit 64c09bf
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
Assets/Mirage/Components/Visibility/HostRendererVisibility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using UnityEngine; | ||
|
||
namespace Mirage | ||
{ | ||
/// <summary> | ||
/// Disables all Renders on GameObject when the NetworkIdentity is not visible too the host player because of a <see cref="NetworkVisibility"/> | ||
/// </summary> | ||
[RequireComponent(typeof(NetworkVisibility))] | ||
public class HostRendererVisibility : NetworkBehaviour | ||
{ | ||
private NetworkVisibility _visibility; | ||
private Renderer[] _renderers; | ||
|
||
private void Awake() | ||
{ | ||
_visibility = GetComponent<NetworkVisibility>(); | ||
_visibility.OnVisibilityChanged += OnVisibilityChanged; | ||
_renderers = GetComponentsInChildren<Renderer>(); | ||
} | ||
|
||
private void OnDestroy() | ||
{ | ||
if (_visibility != null) | ||
_visibility.OnVisibilityChanged -= OnVisibilityChanged; | ||
} | ||
|
||
/// <summary> | ||
/// Can be used to override default GetComponentsInChildren for renderers. Useful if setting up Renderer after Awake is called | ||
/// </summary> | ||
/// <param name="renderers"></param> | ||
public void SetRenderers(Renderer[] renderers) | ||
{ | ||
_renderers = renderers; | ||
} | ||
|
||
private void OnVisibilityChanged(INetworkPlayer player, bool visible) | ||
{ | ||
if (!IsLocalClient) | ||
return; | ||
|
||
if (player == Server.LocalPlayer) | ||
SetHostVisibility(visible); | ||
} | ||
|
||
private void SetHostVisibility(bool visible) | ||
{ | ||
for (var i = 0; i < _renderers.Length; i++) | ||
_renderers[i].enabled = visible; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Mirage/Components/Visibility/HostRendererVisibility.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.