powerfun-unity/Assets/Scripts/Scenes/Ride/Scripts/AbstratctLoadingController.cs
2021-08-13 17:19:02 +08:00

219 lines
5.8 KiB
C#

using UnityEngine;
using Mapbox.Unity.Map;
using DG.Tweening;
using UnityEngine.UI;
using Assets.Scripts.Apis;
using System;
using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Threading.Tasks;
namespace Assets.Scenes.Ride.Scripts
{
public class AbstratctLoadingController : MonoBehaviour
{
protected Text mapName;
protected AbstractMap _map { get; set; }
protected MapApi mapApi { get; set; }
protected RawImage mapRouteImage;
protected CanvasGroup canvasGroup;
protected Slider slider;
protected Text processText;
protected Text rideNowText;
protected Button rideNow;
protected Button cancel;
protected Text mapDescText;
protected Text distance;
protected Text elevaction;
protected Text slope;
protected GameObject panel;
protected GameObject loadingPanel { get; set; }
#region
protected float process = 0;//0-100 %
public float targetAlpha = 0f;
public float alphaSpeed = 2.0f;
private bool depressFlag = false;
protected CyclingController cyclingController { get; set; }
#endregion
protected virtual void Start()
{
App.TextureCache.Clear();
mapApi = new MapApi();
ShowLoading();
}
protected virtual 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);
}
}
}
//显示loading动画
protected virtual void ShowLoading()
{
}
//在awake前触发。注入主控制器并设置UIManager
public virtual void InjectController(CyclingController controller)
{
cyclingController = controller;
}
//初始化
public virtual void Init()
{
InitGameObject();
InitGameObjectData();
}
//加载界面
protected virtual void InitGameObject()
{
mapName = panel.transform.Find("MapName").GetComponent<Text>();
distance = panel.transform.Find("Distance").GetComponent<Text>();
elevaction = panel.transform.Find("Elevaction").GetComponent<Text>();
slope = panel.transform.Find("Slope").GetComponent<Text>();
mapRouteImage = panel.transform.Find("MapRouteImage").GetComponent<RawImage>();
slider = panel.transform.Find("Process").GetComponent<Slider>();
processText = panel.transform.Find("ProcessText").GetComponent<Text>();
mapDescText = panel.transform.Find("MapDesc").GetComponent<Text>();
rideNow = panel.transform.Find("RideNow").GetComponent<Button>();
rideNowText = rideNow.transform.Find("Text").GetComponent<Text>();
cancel = panel.transform.Find("Cancel").GetComponent<Button>();
var Model = transform.Find("ModalPanel");
Model.SetAsLastSibling();
var root = transform.parent;
_map = root.Find("Map").GetComponent<AbstractMap>();
_map.OnInitialized += _map_OnInitialized;
canvasGroup = transform.GetComponent<CanvasGroup>();
rideNow.enabled = false;
rideNow.interactable = false;
UIManager.AddEvent(rideNow.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, StartRide);
UIManager.AddEvent(cancel.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, Cancel);
}
//加载数据
protected virtual void InitGameObjectData()
{
}
//绘制地图
public virtual async void DrawMapRouteAsync(int routeId,int type=0)
{
this.AddProcess(10);
mapApi = new MapApi();
var result = await mapApi.GetMapLoadingCoverageUrl(routeId, type);
if (!result.result)
return;
var url = result.data;
if (App.TextureCache.ContainsKey(url))
{
mapRouteImage.texture = App.TextureCache[url];
var canvasGroup = mapRouteImage.GetComponent<CanvasGroup>();
canvasGroup.DOFade(1, 1);
}
else
{
Utils.DisplayImageAysnc(StartCoroutine, mapRouteImage, url, DowloadCallBack);
}
}
protected void DowloadCallBack(string url)
{
if (!App.TextureCache.ContainsKey(url))
{
App.TextureCache.Add(url, mapRouteImage.texture);
}
else
{
App.TextureCache[url] = mapRouteImage.texture;
}
var canvasGroup = mapRouteImage.GetComponent<CanvasGroup>();
canvasGroup.DOFade(1, 1);
}
//设定当前loading进度
public virtual void AddProcess(float v)
{
process += v;
slider.value = process;
slider.onValueChanged.AddListener((f) =>
{
processText.text = $"{(f).ToString("#0")}%";
});
if (process >= 100)
{
rideNow.interactable = true;
rideNow.enabled = true;
}
}
protected string GetMaxString(string value, int length)
{
var s = Helper.SubStr(value, length);
return s;
}
protected virtual void StartRide(BaseEventData baseEvent)
{
var checkAnt = cyclingController.CheckAnt();
//#if UNITY_EDITOR
checkAnt = true;
//#endif
if (!checkAnt && !cyclingController.isWatch)
{
var alert = (GameObject)Instantiate(Resources.Load("UI/Prefab/Ride/Alert"), panel.transform);
alert.SetActive(true);
}
else
{
cyclingController.InitTcp();
MapUDPService.SendGizpModel();
cyclingController.ReSetUIManager();
cyclingController.GoContinueRide();
//启用ridenow
canvasGroup.blocksRaycasts = false;
depressFlag = true;
}
}
protected virtual void Cancel(BaseEventData baseEvent)
{
SceneManager.LoadScene("MainScene");
}
protected IEnumerator HiddenLoading()
{
yield return new WaitForSeconds(1f);
loadingPanel.SetActive(false);
}
protected void _map_OnInitialized()
{
var visualizer = _map.MapVisualizer;
visualizer.OnMapVisualizerStateChanged += (s) =>
{
if (s == ModuleState.Finished)
{
AddProcess(40);
}
};
}
}
}