2
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 でボイスチャット(その1:導入)

Last updated at Posted at 2022-11-13

以前書いたこの記事にボイスチャットを実装してみました。

動作確認環境

  • Unity 2021.3.4f1
  • Windows 10

準備

Photon のダッシュボードにアクセス
https://dashboard.photonengine.com/ja-JP/

「新しくアプリを作成する」
Image from Gyazo

Photonの種別で「Voice」を選択し、Voiceアプリを作成する。

Image from Gyazo

Voice のアプリケーションIDを控えておく 。
Image from Gyazo

すでにPhotonのアプリケーションを稼働している場合は、該当アプリケーションIDも控えておく。


PUN2をすでにもってる人は Assets > Photon のフォルダを削除 (Voice の中にPUN2 が入ってるため)
Unity AssetStoreから Photon Voice 2 をダウンロード

Unity

Photon Voice 2 をインポート

Photonの設定をする
Projetウィンドウ
Assets > Photon > PhotonUnityNetworking > Resources

Image from Gyazo

アバターのPrefabのInspector ウィンドウに
Photon Voice View と Speaker を追加。
Photon View と Audio Souceが自動的に追加される。

Image from Gyazo

Photon Transform View と Photon Animator View を追加
Image from Gyazo


空のオブジェクト「PhotonController」を作り、 以下のスクリプトをアタッチする。
PhotonController.cs
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

public class PhotonController : MonoBehaviourPunCallbacks
{
    private void Start()
    {
        // プレイヤー自身の名前を"Player"に設定する
        PhotonNetwork.NickName = "Player";

        // PhotonServerSettingsの設定内容を使ってマスターサーバーへ接続する
        PhotonNetwork.ConnectUsingSettings();
    }

    // マスターサーバーへの接続が成功した時に呼ばれるコールバック
    public override void OnConnectedToMaster()
    {
        // "Room"という名前のルームに参加する(ルームが存在しなければ作成して参加する)
        PhotonNetwork.JoinOrCreateRoom("Room", new RoomOptions(), TypedLobby.Default);
    }

    // ゲームサーバーへの接続が成功した時に呼ばれるコールバック
    public override void OnJoinedRoom()
    {
        //何番目の入室者か
        int num = PhotonNetwork.CurrentRoom.PlayerCount;

        Debug.Log("num: "+num);

        string avatarName="";

        avatarName = "Avatar1";


        // ランダムな座標に自身のアバター(ネットワークオブジェクト)を生成する
        var position = new Vector3(Random.Range(-10f, 10f), Random.Range(10f, 0f), Random.Range(-10f, 10f));
        GameObject avatar = PhotonNetwork.Instantiate(avatarName, position, Quaternion.identity);

        var camera = GameObject.Find("MainCamera");

        camera.transform.parent = avatar.transform;
        //camera.transform.position = avatar.transform.position;

        var pos = avatar.transform.position;

        camera.transform.position = new Vector3(pos.x , pos.y+1.2f, pos.z-2.5f);


    }

   
}


空のオブジェクト「PhotonVoiceClient」を作り、 Pun Voice Client と Recorderコンポーネントを追加

Image from Gyazo

これでうまくいきました。

あとはビルドして試してみましょう。
※WebGLには非対応



注意点

接続中にヘッドホンを外すなどマイクを別のものに変えると、
こちらの声は相手に聞こえなくなります。(相手の声は聞こえる)

コンソールに以下の警告が出ます。

[WARNING] [Speaker] [Avatar1(Clone)] Cannot start playback because speaker is not linked

この場合は一度終了して、再接続してください。

以上です。

2
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
2
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?