浏览代码

Add sample showing ARWorldMap support

/1.5-preview
Tim Mowrer 6 年前
当前提交
dcfcb523
共有 5 个文件被更改,包括 1237 次插入6 次删除
  1. 13
      Packages/manifest.json
  2. 1001
      Assets/Scenes/ARWorldMap.unity
  3. 7
      Assets/Scenes/ARWorldMap.unity.meta
  4. 211
      Assets/Scripts/ARWorldMapController.cs
  5. 11
      Assets/Scripts/ARWorldMapController.cs.meta

13
Packages/manifest.json


{
"dependencies": {
"com.unity.xr.arcore": "1.0.0-preview.20",
"com.unity.xr.arfoundation": "1.0.0-preview.19",
"com.unity.xr.arkit": "1.0.0-preview.16"
}
}
"dependencies": {
"com.unity.xr.arcore": "1.0.0-preview.23",
"com.unity.xr.arfoundation": "1.0.0-preview.20",
"com.unity.xr.arkit": "1.0.0-preview.17"
},
"registry": "https://staging-packages.unity.com"
}

1001
Assets/Scenes/ARWorldMap.unity
文件差异内容过多而无法显示
查看文件

7
Assets/Scenes/ARWorldMap.unity.meta


fileFormatVersion: 2
guid: 8d8902fd0b55449dcbf3572b49d6b1b1
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

211
Assets/Scripts/ARWorldMapController.cs


using System.Collections;
using System.Collections.Generic;
using System.IO;
using Unity.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
#if UNITY_IOS
using UnityEngine.XR.ARKit;
#endif
public class ARWorldMapController : MonoBehaviour
{
[SerializeField]
ARSession m_ARSession;
[SerializeField]
Text m_ErrorText;
[SerializeField]
Button m_SaveButton;
[SerializeField]
Button m_LoadButton;
[SerializeField]
Text m_LogText;
[SerializeField]
Text m_MappingStatus;
public void OnSaveButton()
{
#if UNITY_IOS
StartCoroutine(Save());
#endif
}
public void OnLoadButton()
{
#if UNITY_IOS
StartCoroutine(Load());
#endif
}
public void OnResetButton()
{
m_ARSession.Reset();
}
#if UNITY_IOS
IEnumerator Save()
{
var sessionSubsystem = ARSubsystemManager.sessionSubsystem;
if (sessionSubsystem == null)
{
Log("No session subsystem available. Could not save.");
yield break;
}
var request = sessionSubsystem.GetARWorldMapAsync();
while (!request.status.IsDone())
yield return null;
if (request.status.IsError())
{
Log(string.Format("Session serialization failed with status {0}", request.status));
yield break;
}
var worldMap = request.GetWorldMap();
request.Dispose();
SaveAndDisposeWorldMap(worldMap);
}
IEnumerator Load()
{
var sessionSubsystem = ARSubsystemManager.sessionSubsystem;
if (sessionSubsystem == null)
{
Log("No session subsystem available. Could not load.");
yield break;
}
var file = File.Open(path, FileMode.Open);
if (file == null)
{
Log(string.Format("File {0} does not exist.", path));
yield break;
}
Log(string.Format("Reading {0}...", path));
int bytesPerFrame = 1024 * 10;
var bytesRemaining = file.Length;
var binaryReader = new BinaryReader(file);
var allBytes = new List<byte>();
while (bytesRemaining > 0)
{
var bytes = binaryReader.ReadBytes(bytesPerFrame);
allBytes.AddRange(bytes);
bytesRemaining -= bytesPerFrame;
yield return null;
}
var data = new NativeArray<byte>(allBytes.Count, Allocator.Temp);
data.CopyFrom(allBytes.ToArray());
Log(string.Format("Deserializing to ARWorldMap...", path));
ARWorldMap worldMap;
if (ARWorldMap.TryDeserialize(data, out worldMap))
data.Dispose();
if (worldMap.valid)
{
Log("Deserialized successfully.");
}
else
{
Debug.LogError("Data is not a valid ARWorldMap.");
yield break;
}
Log("Apply ARWorldMap to current session.");
sessionSubsystem.ApplyWorldMap(worldMap);
}
void SaveAndDisposeWorldMap(ARWorldMap worldMap)
{
Log("Serializing ARWorldMap to byte array...");
var data = worldMap.Serialize(Allocator.Temp);
Log(string.Format("ARWorldMap has {0} bytes.", data.Length));
var file = File.Open(path, FileMode.Create);
var writer = new BinaryWriter(file);
writer.Write(data.ToArray());
writer.Close();
data.Dispose();
worldMap.Dispose();
Log(string.Format("ARWorldMap written to {0}", path));
}
#endif
string path
{
get
{
return Path.Combine(Application.persistentDataPath, "my_session.worldmap");
}
}
bool supported
{
get
{
#if UNITY_IOS
var sessionSubsystem = ARSubsystemManager.sessionSubsystem;
if (sessionSubsystem != null)
return sessionSubsystem.WorldMapSupported();
#endif
return false;
}
}
void Awake()
{
m_LogMessages = new List<string>();
}
void Log(string logMessage)
{
m_LogMessages.Add(logMessage);
}
void Update()
{
if (supported)
{
m_ErrorText.enabled = false;
m_SaveButton.enabled = true;
m_LoadButton.enabled = true;
}
else
{
m_ErrorText.enabled = true;
m_SaveButton.enabled = false;
m_LoadButton.enabled = false;
}
var sessionSubsystem = ARSubsystemManager.sessionSubsystem;
if (sessionSubsystem == null)
return;
var numLogsToShow = 20;
string msg = "";
for (int i = Mathf.Max(0, m_LogMessages.Count - numLogsToShow); i < m_LogMessages.Count; ++i)
{
msg += m_LogMessages[i];
msg += "\n";
}
m_LogText.text = msg;
#if UNITY_IOS
m_MappingStatus.text = string.Format("Mapping Status: {0}", sessionSubsystem.GetWorldMappingStatus());
#endif
}
List<string> m_LogMessages;
}

11
Assets/Scripts/ARWorldMapController.cs.meta


fileFormatVersion: 2
guid: 650a45a045e92478a9d4ce3ff7858893
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存