2021-03-23 16:07:31 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Mapbox.Unity.Map;
|
|
|
|
|
|
using Mapbox.Utils;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using Mapbox.Unity.MeshGeneration.Modifiers;
|
|
|
|
|
|
using Mapbox.Unity.MeshGeneration.Data;
|
2021-03-28 18:17:15 +08:00
|
|
|
|
using Mapbox.Unity.Utilities;
|
2021-03-23 16:07:31 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
2021-03-22 19:20:51 +08:00
|
|
|
|
{
|
2021-03-23 16:07:31 +08:00
|
|
|
|
public class RouteController : MonoBehaviour
|
2021-03-22 19:20:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
|
AbstractMap _map;
|
|
|
|
|
|
[SerializeField]
|
2021-03-28 18:17:15 +08:00
|
|
|
|
MeshModifier SnapModifier;
|
2021-03-22 19:20:51 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
|
Material _material;
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
|
[Range(1, 10)]
|
|
|
|
|
|
private float UpdateFrequency = 2;
|
|
|
|
|
|
|
|
|
|
|
|
private int _counter;
|
|
|
|
|
|
|
|
|
|
|
|
GameObject _directionsGO;
|
2021-03-28 18:17:15 +08:00
|
|
|
|
GameObject _mipMapRoute;
|
2021-03-25 16:22:09 +08:00
|
|
|
|
MainController mainCotroller;
|
2021-03-22 19:20:51 +08:00
|
|
|
|
|
|
|
|
|
|
protected virtual void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_map == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_map = FindObjectOfType<AbstractMap>();
|
|
|
|
|
|
}
|
|
|
|
|
|
_map.OnInitialized += CreateRoute;
|
|
|
|
|
|
_map.OnUpdated += CreateRoute;
|
2021-03-28 18:17:15 +08:00
|
|
|
|
|
2021-03-22 19:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
|
{
|
2021-03-25 16:22:09 +08:00
|
|
|
|
mainCotroller = transform.parent.GetComponent<MainController>();
|
2021-03-22 19:20:51 +08:00
|
|
|
|
StartCoroutine(QueryTimer());
|
2021-03-29 20:32:30 +08:00
|
|
|
|
//CreateRoute();
|
2021-03-22 19:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
_map.OnInitialized -= CreateRoute;
|
|
|
|
|
|
_map.OnUpdated -= CreateRoute;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerator QueryTimer()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new WaitForSeconds(UpdateFrequency);
|
|
|
|
|
|
CreateRoute();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
void CreateRoute()
|
|
|
|
|
|
{
|
2021-03-28 18:17:15 +08:00
|
|
|
|
UnityTile tile;
|
|
|
|
|
|
bool foundTile = _map.MapVisualizer.ActiveTiles.TryGetValue(Conversions.LatitudeLongitudeToTileId(_map.CenterLatitudeLongitude.x, _map.CenterLatitudeLongitude.y, (int)_map.Zoom), out tile);
|
|
|
|
|
|
if (foundTile)
|
2021-03-22 19:20:51 +08:00
|
|
|
|
{
|
2021-03-28 18:17:15 +08:00
|
|
|
|
var meshData = new MeshData();
|
|
|
|
|
|
var dat = new List<Vector3>();
|
2021-03-22 19:20:51 +08:00
|
|
|
|
|
2021-03-28 18:17:15 +08:00
|
|
|
|
var mapData = mainCotroller.GetMapData();
|
|
|
|
|
|
if (mapData != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var mapDataItem in mapData.List)
|
|
|
|
|
|
{
|
|
|
|
|
|
var point = mapDataItem.Point;
|
|
|
|
|
|
Vector3 item = _map.GeoToWorldPosition(new Vector2d(point[0], point[1]));
|
|
|
|
|
|
item.y += 0.3f;
|
|
|
|
|
|
dat.Add(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
var feat = new VectorFeatureUnity();
|
|
|
|
|
|
feat.Points.Add(dat);
|
|
|
|
|
|
//处理海拔高度的问题
|
|
|
|
|
|
SnapModifier.Run(feat, meshData, tile);
|
|
|
|
|
|
CreatMapRoute(feat);//创建路线
|
|
|
|
|
|
CreateMiniMapRoute(feat);//创建小地图路线
|
2021-03-22 19:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-03-28 18:17:15 +08:00
|
|
|
|
void CreatMapRoute(VectorFeatureUnity feat)
|
2021-03-22 19:20:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (_directionsGO != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_directionsGO.Destroy();
|
|
|
|
|
|
}
|
2021-03-23 16:07:31 +08:00
|
|
|
|
_directionsGO = new GameObject("MapRoute");
|
2021-03-28 18:17:15 +08:00
|
|
|
|
var lineRender = _directionsGO.AddComponent<LineRenderer>();
|
|
|
|
|
|
lineRender.material = new Material(Shader.Find("Sprites/Default"));
|
|
|
|
|
|
var dat = feat.Points[0];
|
|
|
|
|
|
ColorUtility.TryParseHtmlString("#FF2742", out Color c);
|
|
|
|
|
|
lineRender.endColor = c;
|
|
|
|
|
|
lineRender.startColor = c;
|
|
|
|
|
|
//设置宽度
|
2021-03-29 20:32:30 +08:00
|
|
|
|
lineRender.SetWidth(0.5f, 0.5f);
|
2021-03-28 18:17:15 +08:00
|
|
|
|
lineRender.SetVertexCount(dat.Count);
|
|
|
|
|
|
lineRender.SetPositions(feat.Points[0].ToArray());
|
|
|
|
|
|
//lineRender.numCapVertices = 90;
|
|
|
|
|
|
float alpha = 1.0f;
|
|
|
|
|
|
//Gradient gradient = new Gradient();
|
|
|
|
|
|
//gradient.SetKeys(
|
|
|
|
|
|
// new GradientColorKey[] { new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 1.0f) },
|
|
|
|
|
|
// new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) }
|
|
|
|
|
|
//);
|
|
|
|
|
|
//lineRender.colorGradient = gradient;
|
|
|
|
|
|
lineRender.numCornerVertices = 90;
|
2021-03-22 19:20:51 +08:00
|
|
|
|
|
2021-03-28 18:17:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
void CreateMiniMapRoute(VectorFeatureUnity feat)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_mipMapRoute != null)
|
2021-03-22 19:20:51 +08:00
|
|
|
|
{
|
2021-03-28 18:17:15 +08:00
|
|
|
|
_mipMapRoute.Destroy();
|
2021-03-22 19:20:51 +08:00
|
|
|
|
}
|
2021-03-28 18:17:15 +08:00
|
|
|
|
_mipMapRoute = new GameObject("MiniMapRoute");
|
|
|
|
|
|
var lineRender = _mipMapRoute.AddComponent<LineRenderer>();
|
|
|
|
|
|
lineRender.material = new Material(Shader.Find("Sprites/Default"));
|
|
|
|
|
|
var dat = feat.Points[0];
|
2021-03-29 20:32:30 +08:00
|
|
|
|
lineRender.endColor = Color.white;
|
|
|
|
|
|
lineRender.startColor = Color.white;
|
2021-03-28 18:17:15 +08:00
|
|
|
|
//设置宽度
|
|
|
|
|
|
lineRender.SetWidth(3f, 3f);
|
|
|
|
|
|
lineRender.SetVertexCount(dat.Count);
|
|
|
|
|
|
lineRender.SetPositions(feat.Points[0].ToArray());
|
2021-03-29 20:32:30 +08:00
|
|
|
|
//lineRender.numCapVertices = 90;
|
2021-03-28 18:17:15 +08:00
|
|
|
|
float alpha = 1.0f;
|
2021-03-29 20:32:30 +08:00
|
|
|
|
//Gradient gradient = new Gradient();
|
|
|
|
|
|
//gradient.SetKeys(
|
|
|
|
|
|
// new GradientColorKey[] { new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 1.0f) },
|
|
|
|
|
|
// new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) }
|
|
|
|
|
|
//);
|
|
|
|
|
|
//lineRender.colorGradient = gradient;
|
2021-03-28 18:17:15 +08:00
|
|
|
|
lineRender.numCornerVertices = 90;
|
|
|
|
|
|
_mipMapRoute.layer = 9;
|
2021-03-22 19:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|