97 lines
2.3 KiB
C#

using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon.Common;
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class Microphone : UdonSharpBehaviour
{
[SerializeField] private PermissionsPanel _PermissionsPanel;
[SerializeField] private float _MikedVoiceDistanceNear = 30.0f;
[SerializeField] private float _MikedVoiceDistanceFar = 60.0f;
[UdonSynced] private bool _MicrophoneEnabled = false;
private int _LocalOverlapCounter = 0;
private const float DEFAULT_VOICE_DISTANCE_NEAR = 0.0f;
private const float DEFAULT_VOICE_DISTANCE_FAR = 25.0f;
public override void OnDeserialization(DeserializationResult Result)
{
_EnableMicrophone_Synced();
base.OnDeserialization(Result);
}
public override void OnPlayerTriggerEnter(VRCPlayerApi Player)
{
if (Player == Networking.LocalPlayer)
{
_LocalOverlapCounter++;
_UpdateMicrophoneEffect();
}
base.OnPlayerTriggerEnter(Player);
}
public override void OnPlayerTriggerExit(VRCPlayerApi Player)
{
if (Player == Networking.LocalPlayer)
{
_LocalOverlapCounter--;
_UpdateMicrophoneEffect();
}
base.OnPlayerTriggerExit(Player);
}
public override void OnPlayerJoined(VRCPlayerApi Player)
{
if (Player == Networking.LocalPlayer)
{
Networking.LocalPlayer.SetVoiceDistanceNear(DEFAULT_VOICE_DISTANCE_NEAR);
Networking.LocalPlayer.SetVoiceDistanceFar(DEFAULT_VOICE_DISTANCE_FAR);
}
base.OnPlayerJoined(Player);
}
public override void OnPlayerLeft(VRCPlayerApi Player)
{
if (Player == Networking.LocalPlayer)
{
Networking.LocalPlayer.SetVoiceDistanceNear(DEFAULT_VOICE_DISTANCE_NEAR);
Networking.LocalPlayer.SetVoiceDistanceFar(DEFAULT_VOICE_DISTANCE_FAR);
}
base.OnPlayerLeft(Player);
}
public void EnableMicrophone(bool Enable)
{
if (Networking.IsOwner(gameObject))
{
_MicrophoneEnabled = Enable;
_EnableMicrophone_Synced();
RequestSerialization();
}
}
private void _EnableMicrophone_Synced()
{
_UpdateMicrophoneEffect();
}
private void _UpdateMicrophoneEffect()
{
Networking.LocalPlayer.SetVoiceDistanceNear((_MicrophoneEnabled && _LocalOverlapCounter > 0) ? _MikedVoiceDistanceNear : DEFAULT_VOICE_DISTANCE_NEAR);
Networking.LocalPlayer.SetVoiceDistanceFar((_MicrophoneEnabled && _LocalOverlapCounter > 0) ? _MikedVoiceDistanceFar : DEFAULT_VOICE_DISTANCE_FAR);
}
}