1
2

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.

MRTK2.7.3 + AzureSpatialAnchorsSDK2.12をHoloLens2、Android、iOSで動かす

Last updated at Posted at 2022-02-18

環境

  • Windows10 PC
  • iPhone 12 Pro
  • HoloLens2
  • Android 10
  • Unity 20203.4f1

Azure

Azureポータルにログイン
Spatial Anchors Accountsにアクセス
各種値を入力して作成
下記値をメモしておく。
Account ID , Account Domain , Access Key

Unity

Unity HubからProjectを作成
image.png

MixedRealityFeatureToolを起動しProjectPathを指定
image.png

Azure Spatial Anchors SDK2.12 にチェック
image.png

MixedRealityToolkit Foundation 2.7.3にチェック
image.png

Mixed Reality OpenXR Plugin1.3.1にチェック
image.png

Get Features → Import → Approve → Exit
image.png

Unityのポップアップで[yes]を選択→再起動
MRTK Project Configurator でOpenXRを選択
image.png

XR Plug-in Management Settingsに移動
image.png

ProjectSettings
PCタブでOpenXR→Microsoft HoloLens feature groupにチェック
image.png

UWPタブでOpenXR→Microsoft HoloLens feature groupにチェック
image.png

iOSタブでARKitにチェック
image.png

AndroidタブでARCoreにチェック
image.png

OpenXRの設定に移動し下記設定
Render Mode -> Single Pass Instanced
Depth Submission Mode -> Depth 16 Bit4
Interaction Profiles → Microsoft Hand Interaction Profile
image.png

MRTK Project Configuratorに戻り Next → Apply
image.png
image.png

MRTK Project Configuratorを開く
image.png
Next → Next → TMPをインポート → Done

パッケージマネージャーを開き下記PackageをImport
image.png
image.png
ARKit XR Plugin 4.2.2
AR Foundation 4.2.2
ARCore XR Plugin 4.2.2

File → NewScane → Basic → Create
image.png

シーンを保存
image.png

シーンにMRTKConfigureを追加
image.png

Create Empty → 名前をSpatialAnchorManagerに変更
AddComponent → Spatial Anchor Manager
Spatial Anchor Manager のインスペクターから下記変数にAzureの値を入力
Spatial Anchors Account Id
Spatial Anchors Account Key
Spatial Anchors Acccount Domain
image.png

3D Object > Cube > 名前をAnchorに変更
Transform.Scale X:0.1 Y:0.1 Z:0.1
AddComponent → NearInteractioinGrabbable
AddComponent → ObjectManipulator
image.png

Prefab化してScaneから削除

Create Empty → 名前をTestに変更
AddComponent → NewScript → Test

Test.cs
using UnityEngine;
using Microsoft.Azure.SpatialAnchors.Unity;
using System.Threading.Tasks;
using Microsoft.Azure.SpatialAnchors;
using System;
using TMPro;

public class Test : MonoBehaviour
{
    [SerializeField] private SpatialAnchorManager spatialAnchorManager = default;
    [SerializeField] private GameObject anchorPrefab = default;
    [SerializeField] private TMP_Text text = default;
    private bool onClickedNext;
    private string anchorId;
    private GameObject anchor;

    private async void Start()
    {
        while (true)
        {
            await AnchorTest();
        }
    }

    public async Task AnchorTest()
    {
        // アンカーの作成テスト
        text.text = "Start Editor Test";
        await WaitOnClickedNext();
        text.text = "Start Session";
        await spatialAnchorManager.StartSessionAsync();
        anchor = Instantiate(anchorPrefab);
        text.text = "Place Anchor";
        await WaitOnClickedNext();
        text.text = "Save Anchor";
        var cloudNativeAnchor = anchor.AddComponent<CloudNativeAnchor>();
        await cloudNativeAnchor.NativeToCloud();
        while (!spatialAnchorManager.IsReadyForCreate)
        {
            await Task.Delay(330);
            float createProgress = spatialAnchorManager.SessionStatus.RecommendedForCreateProgress;
            createProgress = createProgress < 1 ? createProgress : 1;
            text.text = $"Spatial Scanning\nProgress rate\n{(int)(createProgress * 100)}%";
        }
        var cloudAnchor = cloudNativeAnchor.CloudAnchor;
        cloudAnchor.Expiration = DateTimeOffset.Now.AddDays(1);
        await spatialAnchorManager.CreateAnchorAsync(cloudAnchor);
        anchorId = cloudAnchor.Identifier;
        text.text = "Saved Anchor";
        await WaitOnClickedNext();
        text.text = "Stop Session";
        await WaitOnClickedNext();
        spatialAnchorManager.StopSession();
        await spatialAnchorManager.ResetSessionAsync();
        Destroy(anchor);

        // アンカーの表示テスト
        text.text = "Start Viewer Test";
        await WaitOnClickedNext();
        text.text = "Start Session";
        spatialAnchorManager.AnchorLocated += OnCloudAnchorLocated;
        void OnCloudAnchorLocated(object sender, AnchorLocatedEventArgs args)
        {
            if (args.Status != LocateAnchorStatus.Located)
                return;

            UnityDispatcher.InvokeOnAppThread(() =>
            {
                anchor = Instantiate(anchorPrefab);
                var cloudNativeAnchor = anchor.AddComponent<CloudNativeAnchor>();
                cloudNativeAnchor.CloudToNative(args.Anchor);
                text.text = "Anchor Located";
            });

        }
        var anchorLocateCriteria = new AnchorLocateCriteria();
        anchorLocateCriteria.Identifiers = new string[] { anchorId };
        await spatialAnchorManager.StartSessionAsync();
        var watcher = spatialAnchorManager.Session.CreateWatcher(anchorLocateCriteria);
        text.text = "Spacial Scanning";
        await WaitOnClickedNext();
        text.text = "Stop Session";
        await WaitOnClickedNext();
        spatialAnchorManager.AnchorLocated -= OnCloudAnchorLocated;
        spatialAnchorManager.StopSession();
        await spatialAnchorManager.ResetSessionAsync();
        Destroy(anchor);
    }

    private async Task WaitOnClickedNext()
    {
        onClickedNext = false;
        while (!onClickedNext)
        {
            await Task.Delay(100);
        }
    }

    public void OnClickedNextButton()
    {
        this.onClickedNext = true;
    }
}

Testのインスペクターから下記変数を登録
SpatialAnchorManager → シーンのSpatialAnchorManager
AnchorPrefab → 先ほど作成したAnchorのPrefab
image.png

Packages > Mixed Reality Toolkit Foundation > SDK > Features > UX >interactable > Prefabs >PressableButtonHoloLens2_48x48 ドラッグアンドドロップしてシーンに追加
Transform.Position.z → 0.5
ButtonConfigHelper > OnClick に Test > OnClickedNextButtonを登録
image.png

File > Save でシーンを保存

HoloLens2

Baseを開いた状態で
File > Save as... > HoloLens2
image.png

File > Build Settings > UWP > Switch Platform
MRTK Project Configurator が出てくるのでApply > Next

シーンに戻りMixedRealityToolkitを選択 → DefaultHoloLens2ConfigurationProfileに変更
AddComponent → ARAnchorManager

MixedRealityPlayspace > MainCamera の子オブジェクトとして UI > TextMeshProを作成
下記変更を加える
Canvas
MRTK Canvas に変更
Pos X:0 Y:0.1 Z:0.8
Width:0 Height:0
Scale X:0.001 Y:0.001 Z:0.001
image.png

TextMeshPro-Text
FontSize:20
Width:400
image.png

TestのインスペクターからTextに作成したオブジェクトをセット

File > Save
File > Build Settings
Add Open Scenes で Scenes/HoloLens2を追加
Build > sinファイルを開いてDebug

Android・iOS

Baseを開いた状態で
File > Save as... > Mobile
image.png

MixedRealityToolkitを選択
Copy&Customize → Clone
Camera > Clone > Camera Settings Providers > Add Camera Settings Provider
New data provider > Type > UnityAR > UnityARCameraSettings
image.png

XR > AR Session Origin を追加
AddComponent → AR AnchorManager
image.png

XR > AR Session を追加
image.png

UI > TextMeshPro-Textを追加
RectTransformの値を変更
Left,Top,Right,Bottom → 50
Anchors Min X:0 Y0 Max X:1 Y:1
image.png
TestのインスペクターからTextに作成したオブジェクトをセット

File > Save
File > Build Settings
Add Open Scenes で Scenes/Mobileを追加しScanes/HoloLens2のチェックを外す

Android

Switch Platform > Android
MRTK Project Configurator > Apply > Next > Done
Player Settings > Playerに移動
Other Settings > Graphics APIs からVulKanを削除
Publishing Settings > Custom Main Gradle Template にチェック
Assets\Plugins\Android\mainTemplate.gradleを開きdependenciesに下記2行を追加

implementation('com.squareup.okhttp3:okhttp:[3.11.0]')
implementation('com.microsoft.appcenter:appcenter-analytics:[1.10.0]')

File > Build Settings > Build

iOS

Switch Platform > iOS
MRTK Project Configurator > Apply > Next > Done
File > Build Settings > Build

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?