MRが楽しい

MRやVRについて学習したことを書き残す

MRTK v2のドキュメントを少しずつ読み解く 拡張サービス作成ウィザード

本日は MRTKv2 の調査枠です。
MRTKv2 の Guides ドキュメントを少しずつ読み進めていきます。

MRTKv2のGuidesドキュメント

以下のドキュメントを読み進めていきます。
microsoft.github.io

以下のページでは有志による本ドキュメントの日本語翻訳が行われています。
投稿時点でこちらで未翻訳、または著者が興味のある部分について記事にしていきます。
hololabinc.github.io

本記事では以下のページを読み進めます。
microsoft.github.io
f:id:bluebirdofoz:20200211174530j:plain

拡張サービス作成ウィザード

シングルトンからサービスへの移行は困難な場合があります。
このウィザードは開発者が簡単に新しいサービスを作成できるようにすることで、ドキュメントとサンプルコードを補完します。
サービスをゼロから作成する方法については、登録済みサービス構築ガイド(近日公開予定)をご覧ください。

ウィザードを起動する

メインメニューから MixedRealityToolkit -> Utilities -> Create Extension Service を開き、ウィザードを起動します。
f:id:bluebirdofoz:20200211174541j:plain

ウィザードはサービススクリプトインターフェイス、およびプロファイルクラスを生成するプロセスを案内します。
f:id:bluebirdofoz:20200211174549j:plain

Create Extension Service を利用するには Extension パッケージをインポートする必要があります。

ウィザードの進め方

最初に作成するサービスの名前、プラットフォーム、ネームスペースを設定します。
f:id:bluebirdofoz:20200211174557j:plain

次に生成する各種スクリプトファイルの出力先ディレクトリを指定します。
インスペクター、または、プロファイルはチェックボックスで生成の有無を指定します。
f:id:bluebirdofoz:20200211174605j:plain

[Next]をクリックすると拡張サービスが生成され、現在のプロファイルにサービスを登録するか確認ダイアログが表示されます。
f:id:bluebirdofoz:20200211174615j:plain

[Register]をクイックすると、現在の Extensions に作成したサービスプロバイダーが追加されます。
f:id:bluebirdofoz:20200211174623j:plain

サービススクリプトの編集

デフォルトでは、新しいスクリプトアセットはMixedRealityToolkit.Extensionsフォルダーに生成されます。
ウィザードを完了したら、ここに移動して新しいサービススクリプトを開きます。
f:id:bluebirdofoz:20200211174635j:plain

生成されたサービススクリプトには新規の MonoBehaviour スクリプトに類似したいくつかのプロンプトが含まれます。
サービスを初期化および更新する場所を通知します。
・NewService.cs

namespace Microsoft.MixedReality.Toolkit.Extensions
{
    [MixedRealityExtensionService(SupportedPlatforms.WindowsStandalone|SupportedPlatforms.MacStandalone|SupportedPlatforms.LinuxStandalone|SupportedPlatforms.WindowsUniversal)]
    public class NewService : BaseExtensionService, INewService, IMixedRealityExtensionService
    {
        private NewServiceProfile newServiceProfile;

        public NewService(IMixedRealityServiceRegistrar registrar,  string name,  uint priority,  BaseMixedRealityProfile profile) : base(registrar, name, priority, profile) 
        {
            newServiceProfile = (NewServiceProfile)profile;
        }

        public override void Initialize()
        {
            // Do service initialization here.
        }

        public override void Update()
        {
            // Do service updates here.
        }
    }
}

ウィザードで[register]を選択していた場合、このスクリプトを編集するとサービスが自動的に更新されます。

・INewService.cs

using System;
using Microsoft.MixedReality.Toolkit;

namespace Microsoft.MixedReality.Toolkit.Extensions
{
    public interface INewService : IMixedRealityExtensionService
    {
        // Expose service features and abilities here
    }
}

・NewServiceProfile.cs

using System;
using UnityEngine;
using Microsoft.MixedReality.Toolkit;

namespace Microsoft.MixedReality.Toolkit.Extensions
{
    [MixedRealityServiceProfile(typeof(INewService))]
    [CreateAssetMenu(fileName = "NewServiceProfile", menuName = "MixedRealityToolkit/NewService Configuration Profile")]
    public class NewServiceProfile : BaseMixedRealityProfile
    {
        // Store config data in serialized fields
    }
}


・NewServiceInspector.cs

#if UNITY_EDITOR
using System;
using Microsoft.MixedReality.Toolkit.Editor;
using Microsoft.MixedReality.Toolkit;
using UnityEngine;
using UnityEditor;

namespace Microsoft.MixedReality.Toolkit.Extensions.Editor
{    
    [MixedRealityServiceInspector(typeof(INewService))]
    public class NewServiceInspector : BaseMixedRealityServiceInspector
    {
        public override void DrawInspectorGUI(object target)
        {
            NewService service = (NewService)target;
            
            // Draw inspector here
        }
    }
}

#endif

サービススクリプトの利用

追加したサービススクリプトの利用例を示します。
以下の通り、サービススクリプトに Update() の実行毎にカウントアップを行う機能を追加しました。

using System;
using Microsoft.MixedReality.Toolkit.Utilities;
using Microsoft.MixedReality.Toolkit;

namespace Microsoft.MixedReality.Toolkit.Extensions
{
    [MixedRealityExtensionService(SupportedPlatforms.WindowsStandalone|SupportedPlatforms.MacStandalone|SupportedPlatforms.LinuxStandalone|SupportedPlatforms.WindowsUniversal)]
    public class NewService : BaseExtensionService, INewService, IMixedRealityExtensionService
    {
        private NewServiceProfile newServiceProfile;

        private int updateCount;

        public NewService(IMixedRealityServiceRegistrar registrar,  string name,  uint priority,  BaseMixedRealityProfile profile) : base(registrar, name, priority, profile) 
        {
            newServiceProfile = (NewServiceProfile)profile;
        }

        public override void Initialize()
        {
            // Do service initialization here.
            updateCount = 0;
        }

        public override void Update()
        {
            // Do service updates here.
            updateCount++;
        }

        public int GetUpdateCount()
        {
            return updateCount;
        }
    }
}

f:id:bluebirdofoz:20200211174726j:plain

次にサービススクリプトにアクセスするサンプルスクリプトを作成します。
以下の通り、サービススクリプトからカウントを取得してログに出力するサンプルスクリプトを作成しました。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// MixedRealityToolkitの名前空間
using Microsoft.MixedReality.Toolkit;

// サービススクリプトの名前空間
using Microsoft.MixedReality.Toolkit.Extensions;

public class TestScript : MonoBehaviour
{
    private NewService newService;

    // Start is called before the first frame update
    void Start()
    {
        // サービススクリプトの参照取得
        newService = MixedRealityToolkit.Instance.GetService<NewService>();
    }

    // Update is called once per frame
    void Update()
    {
        // サービススクリプトの関数呼び出し
        int count = newService.GetUpdateCount();
        Debug.Log("UpdateCount = " + count.ToString());
    }
}

f:id:bluebirdofoz:20200211174737j:plain

シーンを再生すると、カウント情報がログに出力されることが確認できます。
f:id:bluebirdofoz:20200211174745j:plain