2022年05月13日

Agora.io VideoSDK for Unity ビデオ通話実装例

Agora.io に VideoSDK for Unity が公開されています。

Unity とは Unity Technologies が開発しているマルチプラットフォーム対応のゲームエンジンです。
Unity プロジェクトに VideoSDK を組み込むことで iOS/Android/MacOSX/Windows のアプリ開発が可能となります。
※ 開発には C# でのコーディングが必要となります。

この記事では、チャンネル入室や映像表示といった基本的な機能がどのようにして実装できるか、VideoSDK のサンプルデモを使って紹介します。
※ VideoSDK および Unity にあまり馴染みのない方は、予めクイックスタートガイドを読んでおくことをお勧めします。

お役立ち資料ダウンロード

オンライン体験におけるブイキューブの技術サポートのご案内

【図解】システム開発のお手伝い

ブイキューブのソリューションアーキテクトが、寄り添います!
各種ライブ配信システムのアーキテクチャについて わかりやすい構成図にてご紹介!

無料ダウンロード

実装例

一連の処理は TestHelloUnityVideo.cs に実装されていますので、中身を見てみましょう。

初期化

TestHelloUnityVideo.cs
    // instance of agora engine
        private IRtcEngine mRtcEngine;
    
        // load agora engine
        public void loadEngine(string appId)
        {
            ...
            // init engine
            mRtcEngine = IRtcEngine.GetEngine(appId);
            ...
        }
    

SDK を使う最初のステップとして、GetEngine() IRtcEngine オブジェクトを初期化します。この時、引数として AppID を渡します。

入室処理

TestHelloUnityVideo.cs
    public void join(string channel)
        {
            ...
            // enable video
            mRtcEngine.EnableVideo();
            ...
            // join channel
            mRtcEngine.JoinChannel(channel, null, 0);
            ...
        }
    

チャンネルへの接続は JoinChannel() を使います。接続前に予め EnableVideo() をコールして映像の送受信を有効にしておきます。
※ 音声についてはデフォルトで有効になっています。

入室ロジック

TestHome.cs
    public void onJoinButtonClicked() 
        {
            // get parameters (channel name, channel profile, etc.)
            GameObject go = GameObject.Find("ChannelName");
            InputField field = go.GetComponent<InputField>();
    
            // create app if nonexistent
            if (ReferenceEquals(app, null))
            {
                app = new TestHelloUnityVideo(); // create app
                app.loadEngine(AppID); // load engine
            }
    
            // join channel and jump to next scene
            app.join(field.text);
            ...
        }
    

ここまで紹介した loadEngine() join() は、別スクリプト (TestHome.cs) のメソッド onJoinButtonClicked() の中で順番に呼び出されています。 このメソッドはユーザーが Join ボタンが押された時のイベントハンドラとして登録されています。

映像まわり

カメラ映像を Unity シーン内に表示するには VideoSurface コンポーネントを使います:

TestHelloUnityVideo.cs
    public void onSceneHelloVideoLoaded()
        {
            // Attach the SDK Script VideoSurface for video rendering
            GameObject quad = GameObject.Find("Quad");
            if (ReferenceEquals(quad, null))
            {
                Debug.Log("BBBB: failed to find Quad");
                return;
            }
            else
            {
                quad.AddComponent<VideoSurface>();
            }
    
            GameObject cube = GameObject.Find("Cube");
            if (ReferenceEquals(cube, null))
            {
                Debug.Log("BBBB: failed to find Cube");
                return;
            }
            else
            {
                cube.AddComponent<VideoSurface>();
            }
        }
    

サンプルでは、シーン内に配置された PrimitiveType (Cube や Plane の総称) に、このコンポーネントを追加する処理をしています。
これにより、これらオブジェクトの表面上に自映像がマッピングされるようになります。

また、RawImage 上にカメラ映像を表示させることもできます。サンプル内、以下の箇所にて実装が確認できます:

TestHelloUnityVideo.cs
    private void onUserJoined(uint uid, int elapsed)
        {
            ...
            // create a GameObject and assign to this new user
            VideoSurface videoSurface = makeImageSurface(uid.ToString());
            if (!ReferenceEquals(videoSurface, null))
            {
                // configure videoSurface
                videoSurface.SetForUser(uid);
                videoSurface.SetEnable(true);
                videoSurface.SetVideoSurfaceType(AgoraVideoSurfaceType.RawImage);
                ...
            }
        }
        ...
        public VideoSurface makeImageSurface(string goName)
        {
            GameObject go = new GameObject();
            ...
            go.name = goName;
    
            // to be renderered onto
            go.AddComponent<RawImage>();
            ...
            // configure videoSurface
            VideoSurface videoSurface = go.AddComponent<VideoSurface>();
            return videoSurface;
        }
    

onUserJoined() はリモート側のユーザーがチャンネル接続した時に呼ばれるイベントハンドラとなります。
サンプルでは、カメラ映像の表示枠として RawImage を組み込んだ GameObject を用意し、そこに VideoSurface を追加しています。
その後、VideoSurface のメソッドをコールして各種設定を行ないます:

  • SetForUser()... 指定した UID のカメラ映像を使います(デフォルトはローカルのカメラ映像)
  • SetEnable()... カメラ映像の表示・非表示のフラグを設定します
  • SetVideoSurfaceType()... レンダラーを指定します。デフォルトでは PrimitiveType 向けの設定のため RawImage を指定しています

ガイドブックダウンロード

【すぐ読める!ガイドブック】ビデオ通話・ライブ配信SDK「Agora」

【大充実】通話・配信SDK「Agora」ガイドブック

通話・配信サービス開発や、配信技術のリプレイスを検討中の方、必見の内容です。
Agora SDKの特徴から活用例まで徹底解説!

無料ダウンロード
ブイキューブ

執筆者ブイキューブ

Agoraの日本総代理店として、配信/通話SDKの提供だけでなく、導入支援から行い幅広いコミュニケーションサービスに携わっている。

関連記事

先頭へ戻る