From c95f6b7c70db0d3902640876a849064326ffee6e Mon Sep 17 00:00:00 2001 From: lishuo Date: Wed, 28 Jun 2023 15:59:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:AR=E7=9B=B8=E5=85=B3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/AR/ARDownloader.cs | 155 +++++++----- .../Scripts/Scenes/AR/AbstractVideoPlayer.cs | 40 +-- Assets/Scripts/Scenes/AR/OnlineVideoPlayer.cs | 4 +- Assets/Scripts/Scenes/AR/UI/VideoLoading.cs | 37 ++- Assets/Scripts/Scenes/AR/UI/VideoUIManager.cs | 72 +++--- Assets/Scripts/Scenes/AR/VideoGameManager.cs | 50 ++-- Assets/Scripts/Scenes/AR/VideoPlayer.cs | 13 +- .../Model/CyclingModels/CompetitionModel.cs | 2 +- .../UI/Prefab/GameRoom/GameRoomDownLoad.cs | 55 +--- .../UI/Prefab/GameRoom/GameRoomMapItem.cs | 85 ++----- .../UI/Prefab/Panel/GameRoomListController.cs | 236 ++++++++++-------- Assets/Scripts/UIManager.cs | 1 + 12 files changed, 382 insertions(+), 368 deletions(-) diff --git a/Assets/AR/ARDownloader.cs b/Assets/AR/ARDownloader.cs index e3664b66..3b1650b2 100644 --- a/Assets/AR/ARDownloader.cs +++ b/Assets/AR/ARDownloader.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Assets.Scenes.Ride.Scripts; using Assets.Scripts.Apis.Models; using Cysharp.Threading.Tasks; +using UnityEngine; using UnityEngine.Networking; namespace Assets.AR @@ -20,10 +21,46 @@ namespace Assets.AR private readonly string _serverARRoutePath; private readonly string _serverVideoPath; - public bool IsVideoDownloaded { get; private set; } - public bool IsARDataDownloaded { get; private set; } - public bool IsARRouteDownloaded { get; private set; } - public float Total { get; private set; } = 0; + private bool IsVideoNeedDownload { get; set; } + private bool IsARDataNeedDownload { get; set; } + private bool IsARRouteNeedDownload { get; set; } + public bool IsDone => !IsVideoNeedDownload && !IsARDataNeedDownload && !IsARRouteNeedDownload; + + private float _total { get; set; } + private float _videoProgress; + public float VideoProgress + { + get => _videoProgress; + set + { + _videoProgress = value; + ComputeProgress(); + } + } + + private float _dataProgress; + public float DataProgress + { + get => _dataProgress; + set + { + _dataProgress = value; + ComputeProgress(); + } + } + + private float _routeProgress; + public float RouteProgress + { + get => _routeProgress; + set + { + _routeProgress = value; + ComputeProgress(); + } + } + + private Action ReportProgress { get; set; } public ARDownloader(MapRoute route) { @@ -41,52 +78,73 @@ namespace Assets.AR _serverARRoutePath = _route.VideoRoute; _serverVideoPath = _route.Url; } - - public async UniTask DownLoadVideoAsync(IProgress progress = null, - CancellationTokenSource cancellation = default(CancellationTokenSource)) + + private void ComputeProgress() { - return await DownloadToFileAsync(_serverVideoPath, _localVideoPath, progress, cancellation); + var progress = (_videoProgress + _routeProgress + _dataProgress) / _total; + ReportProgress?.Invoke(progress); + } + + public async Task DownLoadDefault(Action progress = null,CancellationTokenSource cancellation = default(CancellationTokenSource)) + { + ReportProgress = progress; + + if(IsVideoNeedDownload) + await DownLoadVideoAsync(Progress.Create(x => VideoProgress = x),cancellation); + if(IsARRouteNeedDownload) + await DownLoadARRouteAsync(Progress.Create(x=> RouteProgress = x),cancellation); + if(IsARDataNeedDownload) + await DownLoadARDataAsync(Progress.Create(x=> DataProgress = x),cancellation); } - public async UniTask DownLoadARRouteAsync(IProgress progress = null, - CancellationTokenSource cancellation = default(CancellationTokenSource)) + private async UniTask DownLoadVideoAsync(IProgress progress = null,CancellationTokenSource cancellation = default(CancellationTokenSource)) { - return await DownloadToFileAsync(_serverARRoutePath, _localARRoutePath, progress, cancellation); + var result = await DownloadToFileAsync(_serverVideoPath, _localVideoPath, progress, cancellation); + IsVideoNeedDownload = !result.isDone; + return result; } - public async UniTask DownLoadARDataAsync(IProgress progress = null, + private async UniTask DownLoadARRouteAsync(IProgress progress = null,CancellationTokenSource cancellation = default(CancellationTokenSource)) + { + var result = await DownloadToFileAsync(_serverARRoutePath, _localARRoutePath, progress, cancellation); + IsARRouteNeedDownload = !result.isDone; + return result; + } + + private async UniTask DownLoadARDataAsync(IProgress progress = null, CancellationTokenSource cancellation = default(CancellationTokenSource)) { - return await DownloadToFileAsync(_serverARDataPath, _localARDataPath, progress, cancellation); + var result = await DownloadToFileAsync(_serverARDataPath, _localARDataPath, progress, cancellation); + IsARDataNeedDownload = !result.isDone; + return result; } public async Task CheckAllAsync() { - IsARRouteDownloaded = await CheckARRouteAsync(); - IsARDataDownloaded = await CheckARDataAsync(); - IsVideoDownloaded = await CheckVideoAsync(); + IsARRouteNeedDownload = await CheckARRouteAsync(); + IsARDataNeedDownload = await CheckARDataAsync(); + IsVideoNeedDownload = await CheckVideoAsync(); - if (IsVideoDownloaded) - Total += 1f; - if (IsARRouteDownloaded) - Total += 1f; - if (IsARDataDownloaded) - Total += 1f; + if (IsVideoNeedDownload) + _total += 1f; + if (IsARRouteNeedDownload) + _total += 1f; + if (IsARDataNeedDownload) + _total += 1f; - return IsVideoDownloaded || IsARRouteDownloaded || IsARDataDownloaded; + return IsVideoNeedDownload || IsARRouteNeedDownload || IsARDataNeedDownload; } /// /// return true if the video is needed to download /// /// - public async Task CheckVideoAsync() + private async Task CheckVideoAsync() { try { if (!File.Exists(_localVideoPath)) return true; - var result = await HeadFileAsync(_serverVideoPath, _localVideoPath); - return result.responseCode != 304; + return await HeadFileAsync(_serverVideoPath, _localVideoPath); } catch (Exception e) { @@ -95,35 +153,33 @@ namespace Assets.AR } /// - /// return true if the video is needed to download + /// return true if the video need to update or download /// /// - public async Task CheckARRouteAsync() + private async Task CheckARRouteAsync() { if (!File.Exists(_localARRoutePath)) return true; - var result = await HeadFileAsync(_serverARRoutePath, _localARRoutePath); - return result.responseCode != 304; + return await HeadFileAsync(_serverARRoutePath, _localARRoutePath); } /// /// return true if the video is needed to download /// /// - public async Task CheckARDataAsync() + private async Task CheckARDataAsync() { if (!File.Exists(_localARDataPath)) return true; - var result = await HeadFileAsync(_serverARDataPath, _localARDataPath); - return result.responseCode != 304; + return await HeadFileAsync(_serverARDataPath, _localARDataPath); } - /// /// 下载文件到本地(持续存入磁盘减少内存占用) /// /// 文件连接 /// 本地文件全路径 /// 下载进度 - public static async UniTask DownloadToFileAsync(string downloadUrl, string fullPath, + /// + private static async UniTask DownloadToFileAsync(string downloadUrl, string fullPath, IProgress progress = null, CancellationTokenSource cancellation = default(CancellationTokenSource)) { var dh = new DownloadHandlerFile(fullPath) @@ -138,12 +194,8 @@ namespace Assets.AR var result = await request.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update, (cancellation?.Token ?? default(CancellationToken))); - var etag = result.GetResponseHeader("ETag"); + var etag = result.GetResponseHeader("x-oss-hash-crc64ecma"); var path = fullPath + ".etag"; - if (!File.Exists(path)) - { - File.Create(path); - } File.WriteAllText(path, etag); return result; @@ -155,7 +207,7 @@ namespace Assets.AR /// download url /// local path for storage /// - public static async UniTask HeadFileAsync(string downloadUrl, string fullPath, + private static async Task HeadFileAsync(string downloadUrl, string fullPath, IProgress progress = null, CancellationTokenSource cancellation = default(CancellationTokenSource)) { try @@ -168,26 +220,13 @@ namespace Assets.AR } var request = UnityWebRequest.Head(downloadUrl); - if (!string.IsNullOrEmpty(etag)) - request.SetRequestHeader("If-None-Match", etag); - - var result = - await request - .SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update, - (cancellation?.Token ?? default(CancellationToken))); - - if (result.responseCode != 304) - { - etag = result.GetResponseHeader("ETag"); - File.WriteAllText(fullPath + ".etag", etag); - } - - return result; - + var result = await request.SendWebRequest().ToUniTask(progress, PlayerLoopTiming.Update, (cancellation?.Token ?? default(CancellationToken))); + var responseTag = result.GetResponseHeader("x-oss-hash-crc64ecma"); + return !responseTag.Equals(etag); } catch (Exception e) { - return null; + return false; } } } diff --git a/Assets/Scripts/Scenes/AR/AbstractVideoPlayer.cs b/Assets/Scripts/Scenes/AR/AbstractVideoPlayer.cs index ede86032..985e2277 100644 --- a/Assets/Scripts/Scenes/AR/AbstractVideoPlayer.cs +++ b/Assets/Scripts/Scenes/AR/AbstractVideoPlayer.cs @@ -4,7 +4,6 @@ using UnityEngine; using UnityEngine.UI; using Assets.Core; using Assets.Scripts.Apis.Models; -using DG.Tweening; using Mapbox.Utils; using Assets.Scenes.Ride.Scripts; using UnityEngine.Assertions; @@ -116,18 +115,17 @@ namespace Assets.Scripts.Scenes.VideoRide ticks++; } - public virtual void ComputeAnimator() + protected virtual void ComputeAnimator() { - if (animator != null) - { - animator.SetFloat(PreSpeed, (float)preSpeed); - animator.SetFloat(AnimatorSpeed, (float)OnlineSpeed); - animator.SetFloat(Grade, (float)currentSlope); - animator.SetFloat(Power, (float)power); - } + if (animator == null) return; + + animator.SetFloat(PreSpeed, (float)preSpeed); + animator.SetFloat(AnimatorSpeed, (float)OnlineSpeed); + animator.SetFloat(Grade, (float)currentSlope); + animator.SetFloat(Power, (float)power); } //计算人物当前属性 - public virtual void ComputePlayer() + protected virtual void ComputePlayer() { if (power > 0) { @@ -141,7 +139,7 @@ namespace Assets.Scripts.Scenes.VideoRide } } //计算当前人物的经纬度 - public virtual void Forward() + protected virtual void Forward() { try { @@ -183,9 +181,11 @@ namespace Assets.Scripts.Scenes.VideoRide public void ComputeNextSlope() { double sumDistance = 0; + mapData = manager.GetMapData(); if (mapData == null) return; + var pointList = mapData.List; int preIndex = 0; for (int i = 0; i < pointList.Count; i++) @@ -206,7 +206,7 @@ namespace Assets.Scripts.Scenes.VideoRide { currentIndex = pointList.Count - 1; } - Debug.Log(currentIndex); + preIndex = currentIndex > 0 ? currentIndex - 1 : 0;//前一个索引 int nextIndex = currentIndex == pointList.Count - 1 ? currentIndex : currentIndex + 1; //计算下一个点的坡度和距离 @@ -218,6 +218,7 @@ namespace Assets.Scripts.Scenes.VideoRide nextSlopeDistance = sumDistance - totalDistance * 1000; //NextSlopeTotalDistance = pointList[nextIndex].Distance; currentSlopeDistance = (totalDistance * 1000 - (sumDistance - pointList[currentIndex].Distance)); + //计算累计爬升 totalClimb = 0; for (int i = 1; i <= currentIndex; i++) @@ -229,16 +230,19 @@ namespace Assets.Scripts.Scenes.VideoRide } } } + private void OnDisable() { head?.SetActive(false); } + private void OnEnable() { head?.SetActive(true); } + //显示人物头顶的名字 - public virtual void CreateHeadImage() + protected virtual void CreateHeadImage() { if (head == null) { @@ -270,17 +274,17 @@ namespace Assets.Scripts.Scenes.VideoRide private void OnBecameInvisible() { - head.SetActive(false); + head?.SetActive(false); } private void OnBecameVisible() { - head.SetActive(true); + head?.SetActive(true); } - public void SetStartDistance(double distance) + public void SetStartDistance(double _distance) { - this.StartDistance = distance *1000; + this.StartDistance = _distance *1000; } public void Destroy() { @@ -288,7 +292,9 @@ namespace Assets.Scripts.Scenes.VideoRide { manager.Pause(); } + head?.Destroy(); + gameObject.Destroy(); } //设置当前玩家属性 diff --git a/Assets/Scripts/Scenes/AR/OnlineVideoPlayer.cs b/Assets/Scripts/Scenes/AR/OnlineVideoPlayer.cs index d5592780..874b174c 100644 --- a/Assets/Scripts/Scenes/AR/OnlineVideoPlayer.cs +++ b/Assets/Scripts/Scenes/AR/OnlineVideoPlayer.cs @@ -4,7 +4,7 @@ namespace Assets.Scripts.Scenes.VideoRide { public class OnlineVideoPlayer : AbstractVideoPlayer { - public override void CreateHeadImage() + protected override void CreateHeadImage() { base.CreateHeadImage(); if (Diff < 0) @@ -39,7 +39,7 @@ namespace Assets.Scripts.Scenes.VideoRide this.EndDistance = endDistance * 1000; } - public override void ComputePlayer() + protected override void ComputePlayer() { distance = this.speed; } diff --git a/Assets/Scripts/Scenes/AR/UI/VideoLoading.cs b/Assets/Scripts/Scenes/AR/UI/VideoLoading.cs index d0e3c5bb..bcd3bd57 100644 --- a/Assets/Scripts/Scenes/AR/UI/VideoLoading.cs +++ b/Assets/Scripts/Scenes/AR/UI/VideoLoading.cs @@ -291,31 +291,17 @@ namespace Assets.Scripts.Scenes.VideoRide private async Task StartDownloading() { - slider.maxValue = downloader.Total * 100; - if(downloader.IsVideoDownloaded) - await downloader.DownLoadVideoAsync(Progress.Create(HandleProgress)); - if(downloader.IsARRouteDownloaded) - await downloader.DownLoadARRouteAsync(Progress.Create(HandleProgress)); - if(downloader.IsARDataDownloaded) - await downloader.DownLoadARDataAsync(Progress.Create(HandleProgress)); - - //cancelToken = new CancellationTokenSource(); - //CompleteDownLoad(); + cancelToken = new CancellationTokenSource(); + await downloader.DownLoadDefault(ReportProgress, cancelToken); } - private void HandleProgress(float value) + private void ReportProgress(float progress) { if (cancelToken != null && cancelToken.IsCancellationRequested) return; - - slider.value += (float)Math.Round(value * 100, 0); - Debug.Log(value); - - var progress = slider.value / slider.maxValue * 100; - downloadText.text = progress.ToString(CultureInfo.InvariantCulture) + "%"; - - var isComplete = slider.value == slider.maxValue; - if (isComplete) + slider.value = progress * 100; + downloadText.text = (progress * 100).ToString("f2") + "%"; + if (progress == 1f) CompleteDownLoad(); } @@ -323,11 +309,16 @@ namespace Assets.Scripts.Scenes.VideoRide { var filepath = $"{PFConstants.VideoFolder}/{route.FileName}"; manager.SetMedia(filepath); + downloading = false; + cancelToken = null; + + watch.SetActive(App.routeResult == null); watch.GetComponent