2022-08-05 15:10:09 +08:00

163 lines
4.5 KiB
C#

using Assets.Scenes.Ride.Scripts;
using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using PolyAndCode.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
private int RoomId { get; set; }
private string FileName { get; set; }
private string FileUrl { get; set; }
private void Start()
{
UIManager.AddEvent(downloadBtn, EventTriggerType.PointerClick, DownloadClick);
UIManager.AddEvent(backBtn, EventTriggerType.PointerClick, CancelClick);
UIManager.AddEvent(runInbackBtn, EventTriggerType.PointerClick, (d) =>
{
gameObject.SetActive(false);
downLoadList.SetActive(true);
var content = downLoadList.transform.Find("Viewport/Content");
var list = FindObjectsOfType<GameRoomDownloadTask>();
//新增
var obj = list.Where(c => c.RoomId == RoomId).FirstOrDefault();
if (obj == null)
{
var newtask = Instantiate(downLoadTask, content);
newtask.GetComponent<GameRoomDownloadTask>().Init(RoomId, FileName,gameObject);
}
});
UIManager.AddEvent(enterBtn, EventTriggerType.PointerClick, (ee) =>
{
manager.GetInRoom();
Reset();
gameObject.SetActive(false);
});
UIManager.AddEvent(cancelBtn, EventTriggerType.PointerClick, (ee) =>
{
Reset();
gameObject.SetActive(false);
});
}
private void Update()
{
if (!string.IsNullOrEmpty(FileName) && Loom.DownLoadTaskList.ContainsKey(FileName))
{
processing(Loom.DownLoadTaskList[FileName]);
}
}
public void Init(int Id,string fileName,string url, GameRoomListController gameRoomListController)
{
RoomId = Id;
FileName = fileName;
FileUrl = url;
manager = gameRoomListController;
}
public void ComeIntoStep2(string fileName)
{
FileName = fileName;
step2.SetActive(true);
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 void processing(float p)
{
downloadSlider.value = p;
if (Loom.DownLoadTaskList.ContainsKey(FileName))
{
Loom.DownLoadTaskList[FileName] = p;
}
else
{
Loom.DownLoadTaskList.Add(FileName, p);
}
}
private void DownloadClick(BaseEventData baseEventData)
{
var fileName = FileName;
var path = PFConstants.VideoFolder;
var filepath = path + "/" + fileName;
var url = FileUrl;
step2.SetActive(true);
//新增
var content = downLoadList.transform.Find("Viewport/Content");
var newtask = Instantiate(downLoadTask, content);
var currentTask = newtask.GetComponent<GameRoomDownloadTask>();
currentTask.Init(RoomId, fileName,gameObject);
Loom.Current.StartCoroutine(Utils.DownloadVideo(fileName, url
, (p, req) =>
{
currentTask.UpdateProcess(p);
processing(p);
}
, (p) =>
{
if (string.IsNullOrEmpty(p))
{
var process = 100f;
if (Loom.DownLoadTaskList.ContainsKey(FileName))
{
Loom.DownLoadTaskList[FileName] = process;
}
else
{
Loom.DownLoadTaskList.Add(FileName, process);
}
currentTask.UpdateProcess(process);
downloadSlider.value = process;
step3.SetActive(true);
}
else
{
Utils.showToast(gameObject, p);
}
}
));
}
}