160 lines
4.5 KiB
C#

using Cysharp.Threading.Tasks;
using Assets.AR;
using Assets.Core;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
class GameRoomDownLoad : MonoBehaviour
{
public GameObject step1;
public GameObject step2;
public GameObject step3;
public GameObject downloadBtn;
public GameObject backBtn;
public Slider downloadSlider;
public GameObject runInbackBtn;
public GameObject enterBtn;
public GameObject cancelBtn;
public GameObject downLoadList;
public GameObject downLoadTask;
private GameRoomListController manager;
public int RoomId { get; set; }
public string FileName { get; set; }
public string RouteName { get; set; }
private string FileUrl { get; set; }
private float _videoProgress;
public float VideoProgress
{
get => _videoProgress;
set
{
_videoProgress = value;
ComputeProgress();
}
}
private float _dataProgress = 0f;
public float DataProgress
{
get => _dataProgress;
set
{
_dataProgress = value;
ComputeProgress();
}
}
private float _routeProgress = 0f;
public float RouteProgress
{
get => _routeProgress;
set
{
_routeProgress = value;
ComputeProgress();
}
}
private void Start()
{
UIManager.AddEvent(downloadBtn, EventTriggerType.PointerClick, Download_Click);
UIManager.AddEvent(backBtn, EventTriggerType.PointerClick, CancelClick);
UIManager.AddEvent(runInbackBtn, EventTriggerType.PointerClick, (d) =>
{
gameObject.SetActive(false);
downLoadList.SetActive(true);
});
UIManager.AddEvent(enterBtn, EventTriggerType.PointerClick, (ee) =>
{
manager.GetInRoom();
Reset();
gameObject.SetActive(false);
});
UIManager.AddEvent(cancelBtn, EventTriggerType.PointerClick, (ee) =>
{
Reset();
gameObject.SetActive(false);
});
}
private ARDownloader _downloader;
public void Init(int Id, string fileName, string url, GameRoomListController gameRoomListController, string routeName,ARDownloader downloader)
{
RoomId = Id;
FileName = fileName;
RouteName = routeName;
FileUrl = url;
manager = gameRoomListController;
_downloader = downloader;
}
public void ComeIntoStep2(string fileName, string routeName)
{
FileName = fileName;
RouteName = routeName;
step2.SetActive(true);
step2.transform.Find("RouteName").GetComponent<Text>().text = routeName;
step3.transform.Find("RouteName").GetComponent<Text>().text = routeName;
step3.SetActive(false);
}
private void Reset()
{
step2.SetActive(false);
step3.SetActive(false);
downloadSlider.value = 0f;
}
private void CancelClick(BaseEventData data)
{
Reset();
gameObject.SetActive(false);
}
private async void Download_Click(BaseEventData baseEventData)
{
step2.SetActive(true);
step2.transform.Find("RouteName").GetComponent<Text>().text = RouteName;
downloadSlider.maxValue = _downloader.Total * 100;
if(_downloader.IsVideoDownloaded)
await _downloader.DownLoadVideoAsync(Progress.Create<float>(x => VideoProgress = x));
if(_downloader.IsARRouteDownloaded)
await _downloader.DownLoadARRouteAsync(Progress.Create<float>(x=> RouteProgress = x));
if(_downloader.IsARDataDownloaded)
await _downloader.DownLoadARDataAsync(Progress.Create<float>(x=> DataProgress = x));
}
private void ComputeProgress()
{
//update progress
var progress = (_videoProgress + _routeProgress + _dataProgress) / _downloader.Total;
downloadSlider.value = progress * 100;
//update download info
if (!Loom.DownloadStack.ContainsKey(FileName) )
{
Loom.DownloadStack.Add(FileName, new DownloadInfo(FileName, RouteName,progress));
}
Loom.DownloadStack[FileName].Process = progress;
if (progress >= 1)
{
Loom.DownloadStack.Remove(FileName);
downloadSlider.value = 100;
step3.SetActive(true);
step3.transform.Find("RouteName").GetComponent<Text>().text = RouteName;
}
}
}