1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Photon Voice2 でボイスチャット(その2:マイクのオンオフ切り替え、音声の範囲設定)

Posted at

この記事の続きです。

マイクのオンオフ切り替え

ボタン作成

(1) ボタンを作る UI > Buttun
TEXTは不要なので削除

(2) Inspector のImage マイクのアイコン画像を入れる
今回はインポートしたアセット「Photon Voice2」にデフォルトで入ってる画像「gizumo-microphone-off」を使用した。

(3) 大きさを調整する。

Image from Gyazo


マイクのオンオフの方法

PunVoiceClientオブジェクトの Recorder コンポーネントの Transmit Enabled でマイクのオンオフの切り替えができる。

Image from Gyazo

スクリプト

ボタンをクリックによって、マイクのオンオフの切り替えをするスクリプトを書く。

 MicController.cs
using Photon.Voice.PUN;
using Photon.Voice.Unity;
using UnityEngine;
using UnityEngine.UI;

public class MicController : MonoBehaviour
{
    public Sprite imageMic0;
    public Sprite imageMic1;
    public Image buttonImage;
    private Recorder avatarRecorder;
    
    void Start()
    {
        buttonImage = GameObject.Find("micButton").GetComponent<Image>();
        buttonImage.sprite = imageMic0;
        avatarRecorder = GameObject.Find("PunVoiceClient").GetComponent<Recorder>();
        avatarRecorder.TransmitEnabled = false;
    }

    public void OnClick()
    {
        buttonImage = this.GetComponent<Image>();
        if (buttonImage.sprite == imageMic0)
        { 
            buttonImage.sprite = imageMic1;
            avatarRecorder = GameObject.Find("PunVoiceClient").GetComponent<Recorder>();      
            avatarRecorder.TransmitEnabled = true;
        }
        else if (buttonImage.sprite == imageMic1)
        {
            buttonImage.sprite = imageMic0;
            avatarRecorder = GameObject.Find("PunVoiceClient").GetComponent<Recorder>();
            avatarRecorder.TransmitEnabled = false;
        }
    }
}

ボタンの設定

ボタンのInspectorウインドウで、

(1) buntton > OnClick() にHierachyのボタンオブジェクトをアタッチし、OnClick関数を紐づける。

(2) 先ほどのスクリプトを追加し、
マイクの画像をアタッチする。
今回はインポートしたアセット「Photon Voice2」にデフォルトで入ってる画像を使用した。

ImageMic0 に gizumo-microphone-off.png

ImageMic1に gizumo-microphone.png

Button Imageに ボタンオブジェクトをアタッチする。

Image from Gyazo

音声の範囲設定

現実社会のように、遠くになるほど聞こえなくなるような設定をするにが、AudioSourceを3D設定にします。
AudioSource の spatialBlend を 1 にします。

下画像のケースでは、3メートルまで普通に聞こえて、以降10メートルまで低減し、それより遠くは音が聞こえません。

Image from Gyazo

以上です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?