Table of Contents

バージョン 4.6 からバージョン 4000 インスタンスへの移行: SpatialMap_Sparse_Building サンプルの移行

この文書では、SpatialMap_Sparse_Building サンプルを EasyAR Sense Unity Plugin 4.6 バージョンから 4000 バージョンに移行する方法について説明します。

プラグインパッケージの置き換え

共通移行ガイド を参照してプラグインパッケージを置き換えます。

非互換コードの修正: 最速実行可能状態

元の MapBuilding_SparseSample スクリプト内の SparseSpatialMapWorkerFrameFilterSparseSpatialMapBuilderFrameFilter に置き換えます:

元バージョンのコード:

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 バージョンに更新され、実行可能になります。

関連トピック