脚本示例
语音舞台
使用 BasisVoiceRoutingShim,从大型扬声器播放演讲者语音的舞台。
using Basis.Scripts.Networking.NetworkedAvatar;
using Basis.Shims;
using Cilbox;
using UnityEngine;
[Cilboxable]
public class VoiceStage : MonoBehaviour
{
public Collider stageArea;
public Transform speaker;
public float range = 60f;
int route = -1;
ushort routedPlayer;
float nextCheck;
void Update()
{
if (Time.time < nextCheck) return;
nextCheck = Time.time + 0.5f;
BasisNetworkPlayer onStage = null;
foreach (BasisNetworkPlayer p in BasisNetworkPlayer.GetAllPlayers())
{
if (p.Player != null && stageArea.bounds.Contains(p.Player.GetPosition())) { onStage = p; break; }
}
if (onStage != null && route >= 0 && onStage.playerId == routedPlayer) return;
Stop();
if (onStage == null || !BasisVoiceRoutingShim.HasConsent(onStage)) return;
route = BasisVoiceRoutingShim.RouteVoiceToObject(onStage, speaker, 2f, range);
routedPlayer = onStage.playerId;
}
void Stop()
{
if (route >= 0) BasisVoiceRoutingShim.StopVoiceRoute(route);
route = -1;
}
void OnDestroy() => Stop();
}- 每个客户端都根据自己已有的位置在本地路由语音。不会产生额外的网络流量。
- 会先检查演讲者的同意。未允许语音路由的玩家会被正常听到。
- 请在 Basis > Tools > Cilbox Permissions 中确认您的沙盒允许使用
BasisNetworkPlayer.GetAllPlayers。
Edit on GitHub
Last updated on