Table of Contents

遷移版本 4.6 到版本 4000 實例:遷移 SpatialMap_Sparse_Building 範例

本文介紹如何將 SpatialMap_Sparse_Building 範例從 EasyAR Sense Unity Plugin 4.6 版本遷移到 4000 版本。

替換插件包

參考 通用遷移指南 替換插件包。

修改不相容程式碼:最快可執行

將原 MapBuilding_SparseSample 腳本中的 SparseSpatialMapWorkerFrameFilter 替換為 SparseSpatialMapBuilderFrameFilter

原版本程式碼:

private SparseSpatialMapWorkerFrameFilter sparse;

修改為:

private SparseSpatialMapBuilderFrameFilter sparse;

在 Awake 中更新取得 SparseSpatialMapBuilderFrameFilter 的程式碼:

原版本程式碼:

sparse = Session.GetComponentInChildren<SparseSpatialMapWorkerFrameFilter>();

修改為:

Session.StateChanged += (state) =>
{
    if (state == ARSession.SessionState.Ready)
    {
        sparse = Session.Assembly.FrameFilters.Where(f => f is SparseSpatialMapBuilderFrameFilter).FirstOrDefault() as SparseSpatialMapBuilderFrameFilter;
    }
};

在 Update 中去掉列印 Status 的程式碼,根據新版本 API 將稀疏空間地圖的參考更新為 sparse.Target:

原版本程式碼:

private void Update()
{
    Status.text = $"Device Model: {SystemInfo.deviceModel} {deviceModel}" + Environment.NewLine +
        "Frame Source: " + ((Session.Assembly != null && Session.Assembly.FrameSource) ? Session.Assembly.FrameSource.GetType().ToString().Replace("easyar.", "").Replace("FrameSource", "") : "-") + Environment.NewLine +
        "Tracking Status: " + Session.TrackingStatus + Environment.NewLine +
        "Sparse Point Cloud Count: " + (sparse.LocalizedMap == null ? "-" : sparse.LocalizedMap.PointCloud.Count.ToString()) + Environment.NewLine +
        "Cube Location: " + (onSparse ? "On Sparse Spatial Map" : (Session.TrackingStatus.OnSome && Session.TrackingStatus != MotionTrackingStatus.NotTracking ? "Air" : "-")) + Environment.NewLine +
        Environment.NewLine +
        "Gesture Instruction" + Environment.NewLine +
        "\tMove to Sparse Spatial Map Point: One Finger Move" + Environment.NewLine +
        "\tScale: Two Finger Pinch";

    if (Input.touchCount == 1 && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    {
        var touch = Input.touches[0];
        if (touch.phase == TouchPhase.Moved)
        {
            var viewPoint = new Vector2(touch.position.x / Screen.width, touch.position.y / Screen.height);
            if (sparse && sparse.LocalizedMap)
            {
                var points = sparse.LocalizedMap.HitTest(viewPoint);
                foreach (var point in points)
                {
                    onSparse = true;
                    TouchControl.transform.position = sparse.LocalizedMap.transform.TransformPoint(point);
                    break;
                }
            }
        }
    }
}

修改為:

private void Update()
{
    if (Input.touchCount == 1 && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    {
        var touch = Input.touches[0];
        if (touch.phase == TouchPhase.Moved)
        {
            var viewPoint = new Vector2(touch.position.x / Screen.width, touch.position.y / Screen.height);
            if (sparse && sparse.Target)
            {
                var points = sparse.Target.HitTest(viewPoint);
                foreach (var point in points)
                {
                    TouchControl.transform.position = sparse.Target.transform.TransformPoint(point);
                    break;
                }
            }
        }
    }
}

此時範例已經基本可以執行。

重新建構場景:準備使用新功能

刪除場景中的 Sparse SpatialMap 節點:

刪除場景中的 AR Session:

重新 AR Session:

重新指定範例腳本中的 ARSession 為新建立的 AR Session (EasyAR):

此時範例場景和腳本都已更新到 4000.0 版本並可以執行。

相關主題