powerfun-unity/Assets/Scenes/Ride/Scripts/LoadingController.cs
2021-04-19 18:03:04 +08:00

119 lines
3.4 KiB
C#

using UnityEngine;
using Mapbox.Unity.Map;
using UnityEngine.UI;
using Assets.Scripts.Apis;
using System;
using Assets.Scripts;
namespace Assets.Scenes.Ride.Scripts
{
//[ExecuteInEditMode]
public class LoadingController : MonoBehaviour
{
Text mapName;
AbstractMap _map;
Text level;
Text distance;
Text elevaction;
Text slope;
Text rideNum;
Text uploadByUserName;
RawImage head;
RawImage country;
MapApi mapApi;
GameObject panel;
CanvasGroup canvasGroup;
private void Start()
{
int routeId = App.RouteIdParam;
mapApi = new MapApi();
panel = transform.Find("Panel").gameObject;
mapName = transform.Find("Panel/MapName").GetComponent<Text>();
distance = transform.Find("Panel/Distance").GetComponent<Text>();
level = transform.Find("Panel/level/Text").GetComponent<Text>();
elevaction = transform.Find("Panel/Elevaction").GetComponent<Text>();
slope = transform.Find("Panel/Slope").GetComponent<Text>();
rideNum = transform.Find("Panel/RideNum").GetComponent<Text>();
uploadByUserName = transform.Find("Panel/UploadByUserName").GetComponent<Text>();
country = transform.Find("Panel/UploadByCountry").GetComponent<RawImage>();
head = transform.Find("Panel/UploadByHead").GetComponent<RawImage>();
var root = transform.parent;
_map = root.Find("Map").GetComponent<AbstractMap>();
canvasGroup = transform.GetComponent<CanvasGroup>();
if (routeId > 0)
{
var map = mapApi.GetById(routeId);
var route = map.data;
mapName.text = route.Name;
level.text = route.Hard;
elevaction.text = Math.Round(route.EleDifference,0).ToString()+"FT";
slope.text = Math.Round(route.AverageGrade,2).ToString() + "%";
rideNum.text = route.TheHeat.ToString();
distance.text = route.Distance.ToString("f1")+"KM";
var userList = mapApi.GetOnlineUserInfo(new int[] { route.UserId }).data;
if (userList.Count > 0)
{
uploadByUserName.text = userList[0].Name;
var countryUrl = App.Host + $"User/GetCountryImg?userid={userList[0].Id}";
Utils.DisplayImage(StartCoroutine, head, userList[0].HeadImage);
Utils.DisplayImage(StartCoroutine, country, countryUrl);
}
_map.OnInitialized += _map_OnInitialized;
_map.OnEditorPreviewEnabled += OnEditorPreviewEnabled;
_map.OnEditorPreviewDisabled += OnEditorPreviewDisabled;
}
}
void _map_OnInitialized()
{
var visualizer = _map.MapVisualizer;
visualizer.OnMapVisualizerStateChanged += (s) =>
{
if (s == ModuleState.Finished)
{
canvasGroup.blocksRaycasts = false;
depressFlag = true;
}
};
}
void OnEditorPreviewEnabled()
{
transform.gameObject.SetActive(false);
}
void OnEditorPreviewDisabled()
{
transform.gameObject.SetActive(true);
}
public float targetAlpha = 0f;
public float alphaSpeed = 2.0f;
private bool depressFlag = false;
void Update()
{
if (canvasGroup == null)
{
return;
}
if (targetAlpha != canvasGroup.alpha && depressFlag)
{
canvasGroup.alpha = Mathf.Lerp(canvasGroup.alpha, targetAlpha, alphaSpeed * Time.deltaTime);
if (Mathf.Abs(targetAlpha - canvasGroup.alpha) <= 0.01f)
{
canvasGroup.alpha = targetAlpha;
}
if (canvasGroup.alpha == 0)
{
transform.gameObject.SetActive(false);
}
}
}
}
}