# 参加者の管理

> Manage participant events when users join or leave channels.

Vivox SDK は他のすべての参加者に表示されるチャンネル内の個別の参加に関する情報を送ります。これには以下の情報が含まれます。

* ユーザーがチャンネルに参加するとき。
* ユーザーがチャンネルから退出するとき。
* ユーザーの状態に関する重要な変更があるとき (ユーザーが話しているまたは入力しているかどうかなど)。

参加者イベントの処理は省略可能です。ユーザーの状態を視覚的に表示しない場合 (例えば誰の音声が有効になっているかを表示するなど)、ゲームではそれらのイベントを無視できます。

ユーザー状態情報を可視化するには、ゲームで以下のメッセージを処理する必要があります。

* `VivoxService.Instance.ParticipantAddedToChannel`
* `VivoxService.Instance.ParticipantRemovedFromChannel`
* `VivoxParticipant.ParticipantMuteStateChanged`
* `VivoxParticipant.ParticipantSpeechDetected`
* `VivoxParticipant.ParticipantAudioEnergyChanged`

## VivoxParticipant##vivoxparticipant

[`ParticipantAddedToChannel`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_ParticipantAddedToChannel) と [`ParticipantRemovedFromChannel`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_ParticipantRemovedFromChannel) はいずれも VivoxParticipant に付属します。VivoxParticipant には、以下のような、追加された参加者に関する情報が含まれます。

* PlayerId
* DisplayName
* その VivoxParticipant が参加しているチャンネルの ChannelName。
* その VivoxParticipant が IsSelf (チャンネル内のローカルプレイヤーを表す参加者) かどうか。

VivoxParticipant には、以下のような、その参加者の現在の状態も含まれます。

* IsMuted 状態
* AudioEnergy
* SpeechDetected (そのプレイヤーの AudioEnergy が Vivox によって話し声と認識されるレベルに到達しているかどうか)。

VivoxParticipants は、ローカルプレイヤーがその参加者をミュートしているか、またはその参加者が現在チャンネル内で発話中かどうかを伝えるために、[`VivoxParticipant.ParticipantMuteStateChanged`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.VivoxParticipant.html#Unity_Services_Vivox_VivoxParticipant_ParticipantMuteStateChanged) と [`VivoxParticipant.ParticipantSpeechDetected`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.VivoxParticipant.html#Unity_Services_Vivox_VivoxParticipant_ParticipantSpeechDetected) によって参加者の UI 表示と緊密に結び付ける必要があります。

[`VivoxParticipant.ParticipantAudioEnergyChanged`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.VivoxParticipant.html#Unity_Services_Vivox_VivoxParticipant_ParticipantAudioEnergyChanged) を使用すると、SpeechDetected よりも正確な音量単位 (VU) メーターを作成できます。

以下のコードは、Vivox ChatChannelSample からの簡略化されたセグメントであり、これらのシステムの例です。

```cs
public 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();
    }
}
```
