夜風のMixedReality

xRと出会って変わった人生と出会った技術を書き残すためのGeekなHoloRangerの居場所

Microsoft Maps SDK For Unityで山手線一周ツアーを作る

本日はMicorosoft Maps SDK の応用枠です。

〇山手線ツアー

今回は以前作成した山手線各駅の立体地図をMapsSDKで表示する機能を拡張します。

redhologerbera.hatenablog.com

具体的に前回の山手線ツアーではシーンを読み込んだと同時にツアーが開始されていましたので、任意のタイミングに開始、停止が行える機能を実装、さらにMapPinで駅名を表示する機能を実装します。

〇任意のタイミングで開始を行う

前回までで作成している山手線ツアーのスクリプトは[MapsSDK]の[Examples]で提供されている[CityTourExampler]シーンの[SeattleTour]コンポーネントを改造しています。

redhologerbera.hatenablog.com

この[SeattleTour]コンポーネントではStart関数でコルーチンを走らせて処理を行っています。

    {
        StartCoroutine(RunTour());
    }

    private IEnumerator RunTour()
    {
        ...
    }

まずはStart関数を void TourStart()などの関数名に書き換えパブリックな関数とします。

 ​public void TourStart()
   ​{
       ...
       ​StartCoroutine(RunTour());
   ​}

   ​private IEnumerator RunTour()
   ​{
       ...
   ​}

これで自動的に実行されることはなくなりPressableButtonなどのUIで関数を呼び出すことでツアーが始まる仕組みができました。

〇任意のタイミングでツアーを停止する。

次にツアーを任意のタイミングで停止する機能を実装します。

もともとの[SeattleTour]コンポーネントではコンポーネントがアタッチされたオブジェクトがアクティブな間foreach分で終わりなきループ処理が行われています。

   void Start()
    {
        StartCoroutine(RunTour());
    }

    private IEnumerator RunTour()
    {
        yield return new WaitForSeconds(5.0f);

        while (isActiveAndEnabled) // loop the tour as long as we are running.
        {
            foreach (var scene in MapScenes)
            {
                yield return _map.SetMapScene(scene);

                yield return new WaitForSeconds(3.0f);
            }
        }
    }

このため新たにbool型の変数を加え任意のタイミングで停止できるようにします。

    private bool _isActive;//変数を追加
   private IEnumerator RunTour()
    {
        yield return new WaitForSeconds(5.0f);

        while (_isActive)_isActiveがTrueの間処理
        {
            foreach (var scene in MapScenes)
            {
                yield return _map.SetMapScene(scene);

                yield return new WaitForSeconds(3.0f);
                Debug.Log(_isActive);
                if (!_isActive)//変数を追加
                    break;//もし_isActiveがFalseならループ処理を終了
            }
        }
    }

これで[_isActive]で処理を終えることができるようになりました。

次に任意のタイミングで停止できるように呼び出せる関数を作成します。

  public void TourEnd()
    {
        _isActive = false;
    }

また同様にツアー開始に _isActiveをTrueにできるようにTourStart()にも処理を一行加えます。

    public void TourStart()
    {
        _isActive = true;//追加
        StartCoroutine(RunTour());
    }

以上で山手線ツアーを任意のタイミングで開始、終了できるようになりました。

スクリプト

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.Maps.Unity;
using Microsoft.Geospatial;
using Unity.VisualScripting;

public class YamanoteTour : MonoBehaviour
{


    //Defalt Location 37.7,137.9,4.8
    private static readonly List<MapScene> MapScenes =
        new List<MapScene>
        {
            // TokyoStation -> Yurakucho
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.681382, 139.76608399999998), 16.8f),
            // Yurakucho->shinbashi
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.675069, 139.763328), 17.3f),
            // Shinbashi
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.665498 , 139.75964), 16.0f),
            // Hamamatucho
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.655646, 139.756749), 17.0f),
            // Tamachi
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.645736 , 139.74757499999998), 17.0f),
            // TakanawaGateway
            // Shinagawa
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.630152, 139.74044000000004), 16.15f),
            // Osaki
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.6197,  139.72855300000003), 17.0f),
            // Gotanda
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.626446, 139.72344399999997), 17.5f),
            // Meguro
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.633998 , 139.715828), 17.5f),
            // Ebisu
            new MapSceneOfLocationAndZoomLevel(new LatLon(  35.64669, 139.710106), 17.5f),
            // Shibuya
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.658517, 139.70133399999997), 15f),
            // Harajuku
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.670168 ,139.70268699999997),17f),
            // Yoyogi
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.683061,139.702042),17f),
            // Shinjuku
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.690921,139.70025799999996),17f),
            // ShinOkubo
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.701306,139.70004399999993),17f),
            // Takadanobaba
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.712285,    139.70378200000005),17f),
            // Mejiro
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.721204 ,139.706587),17f),
            // Ikebukuro
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.728926 ,139.71038),17f),
            // Otuka
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.731401 ,139.72866199999999),17f),
            // Sugamo
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.733492,139.73934499999996),17f),
            // Komagome
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.736489,    139.74687500000005),17f),
            // Tabata
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.738062 ,139.76085999999998),17f),
            // Nishi Nippori
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.732135,    139.76678700000002),17f),
            // Nippori
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.727772 ,139.770987),17f),
            // Uguisudani
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.720495 ,139.77883700000007),17f),
            // Ueno
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.713768,    139.77725399999997),17f),
            // Okachimachi
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.707438 ,139.774632),17f),
            // Akihabara
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.698683 ,139.77421900000002),17f),
            // Kanda
            new MapSceneOfLocationAndZoomLevel(new LatLon(35.69169  ,139.77088300000003),17f)
        };
 
    [SerializeField]
    private MapRenderer _map = null;

    private bool _isActive;
    private void Awake()
    {
        Debug.Assert(_map != null);
    }

    public void TourStart()
    {
        _isActive = true;
        StartCoroutine(RunTour());
    }

    public void TourEnd()
    {
        _isActive = false;
        _mapLayer.enabled = false;
    }

    private IEnumerator PinCreate()
    {
        yield return new WaitForSeconds(11.0f);
        _mapPinProvider.PinAwake();
    }
    private IEnumerator RunTour()
    {
        yield return new WaitForSeconds(5.0f);

        while (_isActive) // loop the tour as long as we are running.
        {
            foreach (var scene in MapScenes)
            {
                yield return _map.SetMapScene(scene);

                yield return new WaitForSeconds(3.0f);
                Debug.Log(_isActive);
                if (!_isActive)
                {
                    break;
                }
            }
        }
    }
}