-
Notifications
You must be signed in to change notification settings - Fork 4
/
PickupMicrophone.cs
151 lines (128 loc) · 4.15 KB
/
PickupMicrophone.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
namespace Guribo.UdonBetterAudio.Scripts.Examples
{
[DefaultExecutionOrder(10000)]
public class PickupMicrophone : UdonSharpBehaviour
{
protected const int NoUser = -1;
public BetterPlayerAudio playerAudio;
public BetterPlayerAudioOverride betterPlayerAudioOverride;
[UdonSynced] [SerializeField] protected int micUserId = NoUser;
protected int OldMicUserId = NoUser;
public override void OnPickup()
{
var localPlayer = Networking.LocalPlayer;
if (!Utilities.IsValid(localPlayer))
{
return;
}
TakeOwnership(localPlayer, false);
micUserId = localPlayer.playerId;
}
public override void OnDrop()
{
micUserId = NoUser;
}
public override void OnDeserialization()
{
UpdateMicUser();
}
public override void OnPreSerialization()
{
UpdateMicUser();
}
private void OnEnable()
{
NewUserStartUsingMic(micUserId);
}
private void OnDisable()
{
CleanUpOldUser(micUserId);
}
private void OnDestroy()
{
CleanUpOldUser(micUserId);
}
/// <summary>
/// if the current user has changed switch let only the new user be affected by the mic
/// </summary>
private void UpdateMicUser()
{
if (micUserId != OldMicUserId)
{
CleanUpOldUser(OldMicUserId);
NewUserStartUsingMic(micUserId);
}
OldMicUserId = micUserId;
}
/// <summary>
/// take ownership of the microphone if the user doesn't have it yet, or force it
/// </summary>
/// <param name="localPlayer"></param>
/// <param name="force"></param>
private void TakeOwnership(VRCPlayerApi localPlayer, bool force)
{
if (!Utilities.IsValid(localPlayer))
{
Debug.LogWarning("PickupMicrophone.TakeOwnership: Invalid local player", this);
return;
}
if (force || !Networking.IsOwner(localPlayer, gameObject))
{
Networking.SetOwner(localPlayer, gameObject);
}
}
/// <summary>
/// if the mic is still held by the given user let that person no longer be affected by the mic
/// </summary>
private void CleanUpOldUser(int oldUser)
{
if (!Utilities.IsValid(playerAudio))
{
Debug.LogError("PickupMicrophone.CleanUpOldUser: playerAudio is invalid");
return;
}
if (oldUser == NoUser)
{
return;
}
var currentMicUser = VRCPlayerApi.GetPlayerById(oldUser);
if (Utilities.IsValid(currentMicUser))
{
if (Utilities.IsValid(betterPlayerAudioOverride))
{
betterPlayerAudioOverride.RemoveAffectedPlayer(currentMicUser);
}
playerAudio.ClearPlayerOverride(currentMicUser.playerId);
}
}
/// <summary>
/// let the given user be affected by the mic
/// </summary>
private void NewUserStartUsingMic(int newUser)
{
if (!Utilities.IsValid(playerAudio))
{
Debug.LogError("PickupMicrophone.CleanUpOldUser: playerAudio is invalid");
return;
}
if (newUser == NoUser)
{
return;
}
var newMicUser = VRCPlayerApi.GetPlayerById(newUser);
if (!Utilities.IsValid(newMicUser))
{
return;
}
if (Utilities.IsValid(betterPlayerAudioOverride))
{
betterPlayerAudioOverride.AffectPlayer(newMicUser);
}
playerAudio.OverridePlayerSettings( betterPlayerAudioOverride);
}
}
}