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 版本並可以運行。

相關主題