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 版本并可以运行。

相关主题