如何使用ARKIT 2.0在Unity中创建Persistence AR体验?

时间:2019-01-25 07:12:21

标签: c# unity3d arkit

您好,我在Unity中使用ARKIT 2.0尝试UnityARWorldMap示例。我在Ipad上运行,将立方体放在触摸位置,然后单击保存按钮。关闭应用程序后,我单击了加载按钮并进行了跟踪为什么没有多维数据集出现在同一位置。保存多维数据集并随后加载它们以创建持久性AR体验需要什么?

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;

public class WorldMapManager : MonoBehaviour
{
[SerializeField]
UnityARCameraManager m_ARCameraManager;

ARWorldMap m_LoadedMap;

serializableARWorldMap serializedWorldMap;

// Use this for initialization
void Start ()
{
    UnityARSessionNativeInterface.ARFrameUpdatedEvent += OnFrameUpdate;
}

ARTrackingStateReason m_LastReason;

void OnFrameUpdate(UnityARCamera arCamera)
{
    if (arCamera.trackingReason != m_LastReason)
    {
        Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None);
        Debug.LogFormat("worldTransform: {0}", arCamera.worldTransform.column3);
        Debug.LogFormat("trackingState: {0} {1}", arCamera.trackingState, arCamera.trackingReason);
        m_LastReason = arCamera.trackingReason;
    }
}

static UnityARSessionNativeInterface session
{
    get { return UnityARSessionNativeInterface.GetARSessionNativeInterface(); }
}

static string path
{
    get { return Path.Combine(Application.persistentDataPath, "myFirstWorldMap.worldmap"); }
}

void OnWorldMap(ARWorldMap worldMap)
{
    if (worldMap != null)
    {
        worldMap.Save(path);
        Debug.LogFormat("ARWorldMap saved to {0}", path);
    }
}

public void Save()
{
    session.GetCurrentWorldMapAsync(OnWorldMap);
}

public void Load()
{
    Debug.LogFormat("Loading ARWorldMap {0}", path);
    var worldMap = ARWorldMap.Load(path);
    if (worldMap != null)
    {
        m_LoadedMap = worldMap;
        Debug.LogFormat("Map loaded. Center: {0} Extent: {1}", worldMap.center, worldMap.extent);

        UnityARSessionNativeInterface.ARSessionShouldAttemptRelocalization = true;

        var config = m_ARCameraManager.sessionConfiguration;
        config.worldMap = worldMap;
        UnityARSessionRunOption runOption = UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking;

        Debug.Log("Restarting session with worldMap");
        session.RunWithConfigAndOptions(config, runOption);

    }
}


void OnWorldMapSerialized(ARWorldMap worldMap)
{
    if (worldMap != null)
    {
        //we have an operator that converts a ARWorldMap to a serializableARWorldMap
        serializedWorldMap = worldMap;
        Debug.Log ("ARWorldMap serialized to serializableARWorldMap");
    }
}


public void SaveSerialized()
{
    session.GetCurrentWorldMapAsync(OnWorldMapSerialized);
}

public void LoadSerialized()
{
    Debug.Log("Loading ARWorldMap from serialized data");
    //we have an operator that converts a serializableARWorldMap to a ARWorldMap
    ARWorldMap worldMap = serializedWorldMap;
    if (worldMap != null)
    {
        m_LoadedMap = worldMap;
        Debug.LogFormat("Map loaded. Center: {0} Extent: {1}", worldMap.center, worldMap.extent);

        UnityARSessionNativeInterface.ARSessionShouldAttemptRelocalization = true;

        var config = m_ARCameraManager.sessionConfiguration;
        config.worldMap = worldMap;
        UnityARSessionRunOption runOption = UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking;

        Debug.Log("Restarting session with worldMap");
        session.RunWithConfigAndOptions(config, runOption);
    }

}
}

0 个答案:

没有答案