AR文件更新流程&AR网络延时问题

This commit is contained in:
lishuo committed 2023-05-18 19:06:40 +08:00
1 parent 16cd2fd26b
commit 74ba5790f9
18 files changed
+867 -508

No files matched your search

+97 -85
View File
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
@@ -14,13 +13,12 @@ using Assets.Scenes.Ride.Scripts.Model;
using Assets.AR;
using System.Threading.Tasks;
using System.Threading;
using Cysharp.Threading.Tasks;
namespace Assets.Scripts.Scenes.VideoRide
{
public class VideoLoading : MonoBehaviour, IProgress<float>
public class VideoLoading : MonoBehaviour
{
VideoGameManager manager { get; set; }
private Text mapName;
private RawImage mapRouteImage;
private CanvasGroup canvasGroup;
@@ -35,26 +33,27 @@ namespace Assets.Scripts.Scenes.VideoRide
private Text slope;
private GameObject panel;
private GameObject download;
private GameObject watch;
protected GameObject loadingPanel { get; set; }
private GameObject watch ;
protected GameObject loadingPanel;
private Text level;
private Text rideNum;
private Text uploadByUserName;
private RawImage head;
private RawImage country;
private RawImage mapCountry;
private RawImage altitudeGraph { get; set; }
private RawImage altitudeGraph;
private Transform mapRanking;
private Text mapId;
private Text downloadText { get; set; }
private Text downloadText;
private Transform ui;
private MapRoute route;
protected float process = 0;//0-100 %
private VideoGameManager manager { get; set; }
private MapRoute route;
private ARDownloader downloader;
private CancellationTokenSource cancelToken { get; set; }
private bool startBtnLock = false;
private bool downloading = false;
private float total = 1f;
private void Awake()
{
@@ -104,31 +103,12 @@ namespace Assets.Scripts.Scenes.VideoRide
processText.text = $"{(f).ToString("#0")}%";
});
download = transform.Find("Panel/Download").gameObject;
watch = transform.Find("Panel/Watch").gameObject;
UIManager.AddEvent(watch, UnityEngine.EventSystems.EventTriggerType.PointerClick, WatchHandler);
canvasGroup = transform.GetComponent<CanvasGroup>();
var fileName = route.FileName;// "12067924_720p.mp4";
var url = route.Url; //@"http://192.168.0.97:6031/12067924_720p.mp4";
var path = PFConstants.VideoFolder;
var filepath = path + "/" + fileName;
if (File.Exists(filepath))
{
watch.SetActive(true);
slider.value = 100;
download.gameObject.SetActive(false);
manager.SetMedia(filepath);
}
else
{
watch.SetActive(false);
//检查本地文件是否存在 如果存在直接设置mediaPlayer 否则显示下载按钮
UIManager.AddEvent(download.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, DownloadAsync);
}
#region
level = panel.transform.Find("level/Text").GetComponent<Text>();
rideNum = panel.transform.Find("RideNum").GetComponent<Text>();
@@ -177,9 +157,35 @@ namespace Assets.Scripts.Scenes.VideoRide
{
panel.transform.Find("RideTip").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
});
UIManager.AddEvent(download.gameObject, EventTriggerType.PointerClick, DownloadAsync);
#endregion
downloadText = rideNow.GetComponentInChildren<Text>();//视频下载进度text
CheckUpdate();
}
private async void CheckUpdate()
{
downloader = new ARDownloader(route);
var showDownload = await downloader.CheckAllAsync();
var isContinue = App.routeResult != null;
watch.SetActive(!showDownload && !isContinue);
download.gameObject.SetActive(showDownload);
if (!showDownload)
{
InitializeVideo();
}
}
private void InitializeVideo()
{
var path = PFConstants.VideoFolder + "/" + route.FileName;
slider.value = 100;
manager.SetMedia(path);
}
protected void InitGameObjectData()
@@ -267,18 +273,14 @@ namespace Assets.Scripts.Scenes.VideoRide
},false,true);
}
private bool downloading = false;
private CancellationTokenSource cancelToken;
private async void DownloadAsync(BaseEventData baseEvent)
{
if (downloading)
return;
beforeDownload();
await startDownloading();
if (downloading) return;
ResetDownload();
await StartDownloading();
}
private void beforeDownload()
private void ResetDownload()
{
downloading = true;
download.SetActive(false);
@@ -287,45 +289,47 @@ namespace Assets.Scripts.Scenes.VideoRide
downloadText.text = "0%";
}
private async Task startDownloading()
private async Task StartDownloading()
{
var route = manager.mapRoute;
var path = PFConstants.VideoFolder;
slider.maxValue = downloader.Total * 100;
if(downloader.IsVideoDownloaded)
await downloader.DownLoadVideoAsync(Progress.Create<float>(HandleProgress));
if(downloader.IsARRouteDownloaded)
await downloader.DownLoadARRouteAsync(Progress.Create<float>(HandleProgress));
if(downloader.IsARDataDownloaded)
await downloader.DownLoadARDataAsync(Progress.Create<float>(HandleProgress));
var localPath = PFConstants.ARFolder;
var arDataPath = route.ARConfig;
var videoRoute = route.VideoRoute;
var dataPath = $"{localPath}/{route.Id}";
Helper.CreateDirectoryIfNotExsit(dataPath);
Helper.CreateDirectoryIfNotExsit(dataPath);
cancelToken = new CancellationTokenSource();
await Loom.DownloadToFileAsync(arDataPath, $"{dataPath}/{route.Id}.json");
await Loom.DownloadToFileAsync(videoRoute, $"{dataPath}/route-{route.Id}.json");
var res = await Loom.DownloadToFileAsync(route.Url, $"{path}/{route.FileName}",this,cancelToken);
if (res.isDone)
{
var filepath = $"{PFConstants.VideoFolder}/{route.FileName}";
manager.SetMedia(filepath);
rideNow.enabled = true;
rideNow.interactable = true;
downloadText.text = App.GetLocalString("Ride Now");
downloading = false;
}
//cancelToken = new CancellationTokenSource();
//CompleteDownLoad();
}
public void Report(float value)
private void HandleProgress(float value)
{
if (cancelToken.IsCancellationRequested)
if (cancelToken != null && cancelToken.IsCancellationRequested)
return;
slider.value += (float)Math.Round(value * 100, 0);
Debug.Log(value);
slider.value = value < 1 ? (float)Math.Round(value * 100, 0) : 100;
downloadText.text = slider.value.ToString(CultureInfo.InvariantCulture) + "%";
var progress = slider.value / slider.maxValue * 100;
downloadText.text = progress.ToString(CultureInfo.InvariantCulture) + "%";
var isComplete = slider.value == slider.maxValue;
if (isComplete)
CompleteDownLoad();
}
private void CompleteDownLoad()
{
var filepath = $"{PFConstants.VideoFolder}/{route.FileName}";
manager.SetMedia(filepath);
watch.GetComponent<Button>().enabled = true;
rideNow.enabled = true;
rideNow.interactable = true;
downloadText.text = App.GetLocalString("Ride Now");
downloading = false;
}
//进入观察模式
private void WatchHandler(BaseEventData data)
{
@@ -360,20 +364,27 @@ namespace Assets.Scripts.Scenes.VideoRide
return;
}
UIManager.SetModalPanel(transform.Find("ModalPanel").GetComponent<PFUIPanel>());
UIManager.ShowConfirm("Tips", "CancelDownloadConfirm",
() =>
{
cancelToken?.Cancel();
SceneManager.LoadSceneAsync("MainScene");
UIManager.CloseConfirm();
},
2,
() =>
{
UIManager.CloseConfirm();
});
CancelDownload();
}
private void CancelDownload()
{
UIManager.SetModalPanel(transform.Find("ModalPanel").GetComponent<PFUIPanel>());
UIManager.ShowConfirm("Tips", "CancelDownloadConfirm",
() =>
{
cancelToken?.Cancel();
SceneManager.LoadSceneAsync("MainScene");
UIManager.CloseConfirm();
},
2,
() =>
{
UIManager.CloseConfirm();
});
}
private async void DrawMapRouteAsync(int routeId, int type = 0)
{
var result = await ConfigHelper.mapApi.GetMapLoadingCoverageUrl(routeId, type);
@@ -391,6 +402,7 @@ namespace Assets.Scripts.Scenes.VideoRide
Utils.DisplayImageAysnc(StartCoroutine, mapRouteImage, url, DowloadCallBack);
}
}
private void DowloadCallBack(string url, RawImage rawImage)
{
if (!App.TextureCache.ContainsKey(url))
@@ -68,7 +68,7 @@ namespace Assets.Scripts.Scenes.VideoRide
private void Update()
{
if (manager != null)
if (manager != null && manager.GetMapRoute() != null)
{
mapName.text = manager.GetMapRoute().Name;
if (manager.CurrentPlayer != null)
+42 -28
View File
@@ -62,7 +62,6 @@ namespace Assets.Scripts.Scenes.VideoRide
public GameObject videoPlayer;
public GameObject target;
public int CurrentUserId { get; set; }
protected override void Awake()
{
@@ -119,34 +118,49 @@ namespace Assets.Scripts.Scenes.VideoRide
yield return null;
videoLoading.Init();//初始化loading页面
}
protected override void Update()
private float tcpTimer = 0f;
private void FixedUpdate()
{
timer -= Time.deltaTime;
while (timer <= 0)
if (cyclingController == null)
return;
try
{
if (cyclingController == null)
return;
try
timer -= Time.deltaTime;
tcpTimer -= Time.deltaTime;
while (timer <= 0)
{
cyclingController.Run(null);//发送数据到tcp
var onlineRiders = cyclingController.riders;
CreateOnlineUser(onlineRiders); //解析tcp返回数据
if (isStart)
{
ComputePlayerRuntimeData();
ticks++;
}
TickTimer();
}
catch (Exception e)
while (tcpTimer <= 0)
{
Debug.Log(e.Message);
MillTimer();
}
timer += 1f;
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
private void TickTimer()
{
if (isStart)
{
ComputePlayerRuntimeData();
ticks++;
}
timer +=1f;
}
private void MillTimer()
{
cyclingController.Run(null);//发送数据到tcp
var onlineRiders = cyclingController.riders;
CreateOnlineUser(onlineRiders); //解析tcp返回数据
tcpTimer += 0.2f;
}
//计算骑手的数据
@@ -302,7 +316,6 @@ namespace Assets.Scripts.Scenes.VideoRide
//移除退出的选手
private void RemovePlayers(List<BaseRider> list)
{
var players = FindObjectsOfType<OnlineVideoPlayer>();
foreach (var item in players)
{
@@ -371,10 +384,7 @@ namespace Assets.Scripts.Scenes.VideoRide
//开始游戏
public void StartGame()
{
//if (startTime != DateTime.MinValue)
{
startTime = UIManager.Now.GetDateTime();
}
startTime = UIManager.Now.GetDateTime();
isStart = true;
}
//骑行是否开始
@@ -445,7 +455,6 @@ namespace Assets.Scripts.Scenes.VideoRide
}
return mapRoute;
}
public void ShowUpRemainTime(double remain)
{
@@ -477,18 +486,22 @@ namespace Assets.Scripts.Scenes.VideoRide
RankingId = cyclingController.recorderData.SaveWithLocalRecordAysnc(cyclingModel, selectParamModel, imageFileName, recordId, path);
}
public void AddEvent(GameObject sender, EventTriggerType eventType, UnityAction<BaseEventData> unityAction)
{
UIManager.AddEvent(sender, eventType, unityAction);
}
public Texture GetCountryImageByCode(string code)
{
return UIManager.Instance.loginRegOptions.GetCountryImage(code);
}
public Texture GetCountryImageByName(string name)
{
return UIManager.Instance.loginRegOptions.GetCountryImageByName(name);
}
public Vector2d Along(double endDistance)
{
if (mapData != null)
@@ -504,6 +517,7 @@ namespace Assets.Scripts.Scenes.VideoRide
return new Vector2d(0, 0);
}
}
private void OnDestroy()
{
App.gameRoomDetail = null;