如何在Unity的Photon Voice中将所有玩家与一名玩家交谈?

时间:2017-05-21 23:02:03

标签: c# unity5 photon

我一直试图在光子声音统一中进行私人聊天,但到目前为止还不能。我读了音频组的概念https://doc.photonengine.com/en-us/voice/current/getting-started/voice-for-pun 在项目中,首先,player1加入是创建房间并将默认音频组设置为零的主客户端。我正在使用demo pushtotalk,我希望加入的玩家能够在一对一的谈话中与player1交谈。我尝试在主客户端播放器1之后加入其他玩家不同的音频组,并让主客户端按下按钮或事件来订阅它们以使其工作但未能这样做。只是希望player1能够在私人谈话中听取所有其他玩家的意见。请使用光子语音脚本中的演示场景给出解释示例。这是代码 -

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

1 个答案:

答案 0 :(得分:0)

我刚修改了OnVoiceClientStateChanged()SetAudioGroup()。 在致电AudioGroup之前,需要先设置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 });
            }
        }
    }