简体   繁体   中英

How to talk from all players to one player in Photon Voice in Unity?

I have being trying to make private chat in photon voice unity possible but so far not able to. I read the concept for audio groups https://doc.photonengine.com/en-us/voice/current/getting-started/voice-for-pun In the project First the player1 joining is the master client who creates the room and have the default audio group set to zero. I am using the demo pushtotalk where I want the joining players to be able to talk to the player1 in one to one talk. I tried giving the other players who join after the master client player1 different audio groups and make master client to subscribe them at push of a button or event to make it work but failed to do so. Just want the player1 to be able to listen to all the other players in a private conversation. Please give examples for explanation using the demo scene in photon voice script. Here is the code -

using Client.Photon.LoadBalancing;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class PushToTalkPrivateButton : MonoBehaviour
{
    [SerializeField]
    private Button pushToTalkPrivateButton;
    [SerializeField]
    private Text buttonText;
    private PushToTalkScript pttScript;
    public byte AudioGroup;
    public bool Subscribed;

            private void Start()
    {
        pttScript = FindObjectOfType<PushToTalkScript>();
        PhotonVoiceNetwork.Client.OnStateChangeAction += OnVoiceClientStateChanged;
    }


            private void OnVoiceClientStateChanged(ClientState state)
    {
        Debug.LogFormat("VoiceClientState={0}", state);
        if (pushToTalkPrivateButton != null)
        {
            switch (state)
            {
                case ClientState.Joined:
                    pushToTalkPrivateButton.gameObject.SetActive(true);
                    Subscribed = Subscribed || PhotonVoiceNetwork.Client.ChangeAudioGroups(null, new byte[1] { AudioGroup });
                    break;
                default:
                    pushToTalkPrivateButton.gameObject.SetActive(false);
                    break;
            }
        }
    }

    public void SetAudioGroup(PhotonPlayer player)
    {
        if (!Subscribed)
        {
            buttonText.text = string.Format("Talk-To-Player{0}", player.ID);
            int targetActorNr = player.ID;
            if (PhotonNetwork.player.ID < targetActorNr)
            {
                AudioGroup = (byte) (targetActorNr + PhotonNetwork.player.ID*10);
            }
            else if (PhotonNetwork.player.ID > targetActorNr)
            {
                AudioGroup = (byte) (PhotonNetwork.player.ID + targetActorNr*10);
            }
            else
            {
                return;
            }
            if (PhotonVoiceNetwork.ClientState == ClientState.Joined)
            {
                Subscribed = PhotonVoiceNetwork.Client.ChangeAudioGroups(null, new byte[1] { AudioGroup });
            }
        }
    }

    public void PushToTalkOn()
    {
        if (Subscribed)
        {
            PhotonVoiceNetwork.Client.GlobalAudioGroup = AudioGroup;
            pttScript.PushToTalk(true);
        }
    }

    public void PushToTalkOff()
    {
        pttScript.PushToTalkOff();
    }
}

I just modified OnVoiceClientStateChanged() and SetAudioGroup() . AudioGroup need be set before you call ChangeAudioGroups() .

    private void OnVoiceClientStateChanged(ClientState state)
    {
        Debug.LogFormat("VoiceClientState={0}", state);
        if (pushToTalkPrivateButton != null)
        {
            switch (state)
            {
                case ClientState.Joined:
                    //Subscribed = Subscribed || PhotonVoiceNetwork.Client.ChangeAudioGroups(null, new byte[1] { AudioGroup });
                    break;
                default:
                    pushToTalkPrivateButton.gameObject.SetActive(false);
                    PhotonVoiceNetwork.Client.ChangeAudioGroups(byte[0], null);
                    break;
            }
        }
    }

    public void SetAudioGroup(PhotonPlayer player)
    {
        if (!Subscribed)
        {
            buttonText.text = string.Format("Talk-To-Player{0}", player.ID);
            int targetActorNr = player.ID;
            if (PhotonNetwork.player.ID < targetActorNr)
            {
                AudioGroup = (byte) (targetActorNr + PhotonNetwork.player.ID*10);
            }
            else if (PhotonNetwork.player.ID > targetActorNr)
            {
                AudioGroup = (byte) (PhotonNetwork.player.ID + targetActorNr*10);
            }
            else
            {
                return;
            }
            if (PhotonVoiceNetwork.ClientState == ClientState.Joined)
            {
                pushToTalkPrivateButton.gameObject.SetActive(true);
                Subscribed = PhotonVoiceNetwork.Client.ChangeAudioGroups(null, new byte[1] { AudioGroup });
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM