2017-05-21 208 views
1

我一直在試圖使光子語音統一的私人聊天成爲可能,但目前爲止還無法做到。我讀了音頻組的概念https://doc.photonengine.com/en-us/voice/current/getting-started/voice-for-pun 在項目中首先,player1加入是創建房間並將默認音頻組設置爲零的主客戶端。我正在使用演示pushtotalk,我希望加入的玩家能夠以一對一的方式與player1交談。我試圖給其他玩家加入主客戶端player1之後的不同音頻組,並使主客戶端按下按鈕或事件來訂閱它們以使其工作,但未能這樣做。只需要玩家1能夠在私人對話中聽所有其他玩家。請給出使用photon語音腳本中的演示場景進行解釋的例子。這裏的代碼 -如何在Unity中的Photon Voice中與所有玩家對話?

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

回答

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 }); 
      } 
     } 
    } 
相關問題