참여 관리
Manage participant events when users join or leave channels.
읽는 시간 2분최근 업데이트: 19일 전
Vivox SDK는 채널에 있는 다른 모든 참가자가 볼 수 있는 개별 참가자에 관한 정보를 게시합니다. 게시되는 정보는 다음과 같습니다.
- 사용자가 채널에 참여할 때
- 사용자가 채널을 나갈 때
- 사용자가 말을 하고 있거나 텍스트를 입력 중인 경우와 같이 사용자의 상태에 중요한 변화가 있을 때
VivoxService.Instance.ParticipantAddedToChannelVivoxService.Instance.ParticipantRemovedFromChannelVivoxParticipant.ParticipantMuteStateChangedVivoxParticipant.ParticipantSpeechDetectedVivoxParticipant.ParticipantAudioEnergyChanged
VivoxParticipant
ParticipantAddedToChannelParticipantRemovedFromChannel- PlayerId
- DisplayName
- VivoxParticipant가 구성원으로 속한 채널의 ChannelName
- VivoxParticipant IsSelf 여부 - 채널 내에서 로컬 플레이어를 나타내는 참가자
- IsMuted 상태
- AudioEnergy
- SpeechDetected(Vivox가 스피치로 간주할 정도로 플레이어의 AudioEnergy가 증가했는지 여부)
VivoxParticipant.ParticipantMuteStateChangedVivoxParticipant.ParticipantSpeechDetectedVivoxParticipant.ParticipantAudioEnergyChangedpublic class RosterManager : MonoBehaviour{ private const string LobbyChannelName = "lobbyChannel"; private Dictionary<string, List<RosterItem>> rosterObjects = new Dictionary<string, List<RosterItem>>(); public GameObject rosterItemPrefab; private void Start() { VivoxService.Instance.ParticipantAddedToChannel += OnParticipantAdded; VivoxService.Instance.ParticipantRemovedFromChannel += OnParticipantRemoved; } public void ClearAllRosters() { foreach(List<RosterItem> rosterList in rosterObjects.Values) { foreach(RosterItem item in rosterList) { Destroy(item.gameObject); } rosterList.Clear(); } rosterObjects.Clear(); } public void ClearChannelRoster(string channelName) { List<RosterItem> rosterList = rosterObjects[channelName]; foreach(RosterItem item in rosterList) { Destroy(item.gameObject); } rosterList.Clear(); rosterObjects.Remove(channelName); } private void CleanRoster(string channelName) { RectTransform rt = this.gameObject.GetComponent<RectTransform>(); rt.sizeDelta = new Vector2(0, rosterObjects[channelName].Count * 50); } void UpdateParticipantRoster(VivoxParticipant participant, bool isAddParticipant) { if (isAddParticipant) { GameObject newRosterObject = GameObject.Instantiate(rosterItemPrefab, this.gameObject.transform); RosterItem newRosterItem = newRosterObject.GetComponent<RosterItem>(); List<RosterItem> thisChannelList; if (rosterObjects.ContainsKey(participant.ChannelName)) { //Add this object to an existing roster rosterObjects.TryGetValue(participant.ChannelName, out thisChannelList); newRosterItem.SetupRosterItem(participant); thisChannelList.Add(newRosterItem); rosterObjects[participant.ChannelName] = thisChannelList; } else { //Create a new roster to add this object to thisChannelList = new List<RosterItem>(); thisChannelList.Add(newRosterItem); newRosterItem.SetupRosterItem(participant); rosterObjects.Add(participant.ChannelName, thisChannelList); } CleanRoster(participant.ChannelName); } else { if (rosterObjects.ContainsKey(participant.ChannelName)) { RosterItem removedItem = rosterObjects[participant.ChannelName].FirstOrDefault(p => p.Participant.PlayerId == participant.PlayerId); if (removedItem != null) { rosterObjects[participant.ChannelName].Remove(removedItem); Destroy(removedItem.gameObject); CleanRoster(participant.ChannelName); } else { Debug.LogError("Trying to remove a participant that has no roster item."); } } } } void OnParticipantAdded(VivoxParticipant participant) { UpdateParticipantRoster(participant, true); } void OnParticipantRemoved(VivoxParticipant participant) { UpdateParticipantRoster(participant, false); }}public class RosterItem : MonoBehaviour{ // Player specific items. public VivoxParticipant Participant; public Text PlayerNameText; public Image ChatStateImage; public Sprite MutedImage; public Sprite SpeakingImage; public Sprite NotSpeakingImage; Button m_muteButton; private void UpdateChatStateImage() { if (Participant.IsMuted) { ChatStateImage.sprite = MutedImage; } else { if (Participant.SpeechDetected) { ChatStateImage.sprite = SpeakingImage; } else { ChatStateImage.sprite = NotSpeakingImage; } } } public void SetupRosterItem(VivoxParticipant participant) { //Set the Participant variable of this RosterItem to the VivoxParticipant added in the RosterManager Participant = participant; PlayerNameText.text = Participant.DisplayName; // Update the image to the active state of the user (either the SpeakingImage, the MutedImage, or the NotSpeakingImage) and then attach // the function to run if an event is fired denoting a change to that users state UpdateChatStateImage(); Participant.ParticipantMuteStateChanged += UpdateChatStateImage; Participant.ParticipantSpeechDetected += UpdateChatStateImage; //A button on the UI element itself is implemented to handle muting on the participant represented by the UI element m_muteButton = gameObject.GetComponent<Button>(); m_muteButton.onClick.AddListener(() => { // If already muted, unmute, and vice versa. if (Participant.IsMuted) { Participant.UnmutePlayerLocally(); } else { Participant.MutePlayerLocally(); } }); } void OnDestroy() { Participant.ParticipantMuteStateChanged -= UpdateChatStateImage; Participant.ParticipantSpeechDetected -= UpdateChatStateImage; m_muteButton.onClick.RemoveAllListeners(); }}