環境
- Windows10 PC
- iPhone 12 Pro
- HoloLens2
- Android 10
- Unity 20203.4f1
Azure
Azureポータルにログイン
Spatial Anchors Accountsにアクセス
各種値を入力して作成
下記値をメモしておく。
Account ID , Account Domain , Access Key
Unity
MixedRealityFeatureToolを起動しProjectPathを指定
Azure Spatial Anchors SDK2.12 にチェック
MixedRealityToolkit Foundation 2.7.3にチェック
Mixed Reality OpenXR Plugin1.3.1にチェック
Get Features → Import → Approve → Exit
Unityのポップアップで[yes]を選択→再起動
MRTK Project Configurator でOpenXRを選択
XR Plug-in Management Settingsに移動
ProjectSettings
PCタブでOpenXR→Microsoft HoloLens feature groupにチェック
UWPタブでOpenXR→Microsoft HoloLens feature groupにチェック
OpenXRの設定に移動し下記設定
Render Mode -> Single Pass Instanced
Depth Submission Mode -> Depth 16 Bit4
Interaction Profiles → Microsoft Hand Interaction Profile
MRTK Project Configuratorに戻り Next → Apply
MRTK Project Configuratorを開く
Next → Next → TMPをインポート → Done
パッケージマネージャーを開き下記PackageをImport
ARKit XR Plugin 4.2.2
AR Foundation 4.2.2
ARCore XR Plugin 4.2.2
File → NewScane → Basic → Create
Create Empty → 名前をSpatialAnchorManagerに変更
AddComponent → Spatial Anchor Manager
Spatial Anchor Manager のインスペクターから下記変数にAzureの値を入力
Spatial Anchors Account Id
Spatial Anchors Account Key
Spatial Anchors Acccount Domain
3D Object > Cube > 名前をAnchorに変更
Transform.Scale X:0.1 Y:0.1 Z:0.1
AddComponent → NearInteractioinGrabbable
AddComponent → ObjectManipulator
Prefab化してScaneから削除
Create Empty → 名前をTestに変更
AddComponent → NewScript → Test
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
Packages > Mixed Reality Toolkit Foundation > SDK > Features > UX >interactable > Prefabs >PressableButtonHoloLens2_48x48 ドラッグアンドドロップしてシーンに追加
Transform.Position.z → 0.5
ButtonConfigHelper > OnClick に Test > OnClickedNextButtonを登録
File > Save でシーンを保存
HoloLens2
Baseを開いた状態で
File > Save as... > HoloLens2
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
TextMeshPro-Text
FontSize:20
Width:400
TestのインスペクターからTextに作成したオブジェクトをセット
File > Save
File > Build Settings
Add Open Scenes で Scenes/HoloLens2を追加
Build > sinファイルを開いてDebug
Android・iOS
Baseを開いた状態で
File > Save as... > Mobile
MixedRealityToolkitを選択
Copy&Customize → Clone
Camera > Clone > Camera Settings Providers > Add Camera Settings Provider
New data provider > Type > UnityAR > UnityARCameraSettings
XR > AR Session Origin を追加
AddComponent → AR AnchorManager
UI > TextMeshPro-Textを追加
RectTransformの値を変更
Left,Top,Right,Bottom → 50
Anchors Min X:0 Y0 Max X:1 Y:1
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