天津23维预案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

187 lines
6.2 KiB

using UnityEngine;
using UnityEngine.UI;
using AX.NetworkSystem;
using System.IO;
public class FileTransTipWinManager : MonoBehaviour {
public static TransferTask task;
/// <summary>
/// 设置文件传输进度展示。
/// </summary>
/// <param name="tips"></param>
/// <param name="files"></param>
/// <param name="type"></param>
/// <param name="ID">上传Answer和CourseWare时赋值ID,其他情况ID为空</param>
public void SetWindow(string tips, string[] files, TransferType type)
{
transform.name = "FileTransTipWindow";
transform.SetParent(GameObject.Find("Canvas").transform);
transform.localScale = new Vector3(1, 1, 1);
GetComponent<RectTransform>().anchoredPosition = new Vector2(1000, -400);
//transform.FindChild("Tip").GetComponent<Text>().text = tips;
//销毁窗口
if (type == TransferType.Upload)
{
UpLoadFiles(files);
transform.Find("Tip").GetComponent<Text>().text = "上传文件";
}
else
{
DownLoadFiles(files);
transform.Find("Tip").GetComponent<Text>().text = "下载文件";
}
if (transform.Find("OK"))
{
transform.Find("OK").gameObject.SetActive(false);
}
}
public void SetFileTrans(string tips, string[] files, TransferType type)
{
if (type == TransferType.Upload)
{
UpLoadFiles(files);
}
else
{
DownLoadFiles(files);
}
}
private void DownLoadFiles(string[] files)
{
//下载文件
string[] Lfiles = new string[files.Length];
string[] Sfiles = new string[files.Length];
//获取本地路径
for (int i = 0; i < files.Length; i++) {
Lfiles[i] = ExamInfoHelpClass.CurrentWorkPath + files[i];
}
//获取远端路径
for (int i = 0; i < files.Length; i++)
{
Sfiles[i] = "Db/" + files[i];
}
task = FileTransfer.CreateTransferTask(NetworkManager.Default, Lfiles, Sfiles, TransferType.Download);
//task.SetRootPath(ExamInfoHelpClass.CurrentWorkPath);
task.Start();//开始传输
//start后要在传输完毕后stop
task.ProgressChanged += Task_ProgressChanged;
task.TransferTaskCompleted += DownloadTask_TransferTaskCompleted;
}
private void DownloadTask_TransferTaskCompleted()
{
task.ProgressChanged -= Task_ProgressChanged;
task.TransferTaskCompleted -= DownloadTask_TransferTaskCompleted;
if (transform.Find("OK"))
{
transform.Find("OK").gameObject.SetActive(true);
}
FileTransfer.DestroyTransferTask(task);
}
public void DestroyWindow()
{
Destroy(gameObject);
}
private void UpLoadFiles(string[] files)
{
if (files.Length<= 0)
{
return;
}
string[] Lfiles = new string[files.Length];
string[] Sfiles = new string[files.Length];
//获取本地路径
for (int i = 0; i < files.Length; i++)
{
if ( files[i].Contains("Answers") || files[i].Contains("Coursewares") || files[i].Contains("Scores"))
{
var strs = files[i].Split('/');
if (strs.Length == 3)
{
Lfiles[i] = ExamInfoHelpClass.CurrentWorkPath + strs[0] + "/" + strs[2];
}
else
Debug.LogError("获取不合法目录!");
}
else
{
Lfiles[i] = ExamInfoHelpClass.CurrentWorkPath + files[i];
}
}
//获取远端路径
for (int i = 0; i < files.Length; i++)
{
Sfiles[i] = "Db/" + files[i];
}
task = FileTransfer.CreateTransferTask(NetworkManager.Default, Lfiles, Sfiles, TransferType.Upload);
task.Start();//开始传输
//start后要在传输完毕后stop
task.ProgressChanged += Task_ProgressChanged;
task.TransferTaskCompleted += UplaodTask_TransferTaskCompleted;
}
private void UplaodTask_TransferTaskCompleted()
{
var filesL = task.StoragePath;
var filesS = task.TransferPath;
//if (files[0].Contains("Answers"))
//{
// BaseItemManager.AnswerList.Clear();//清空待上传答案列表
//}
//移除事件
task.ProgressChanged -= Task_ProgressChanged;
task.TransferTaskCompleted -= UplaodTask_TransferTaskCompleted;
FileTransfer.DestroyTransferTask(task);
//如果是创建的课件,删除本地文件前先转存到对应ID路径下,避免自己的东西还要下载
if ( filesL[0].Contains("Coursewares") || filesL[0].Contains("Answers") || filesL[0].Contains("Scores"))
{
for (int i = 0; i < filesS.Length; i++)
{
var strs = filesS[i].Split('/');
if (strs.Length == 4)
{
var path = ExamInfoHelpClass.CurrentWorkPath + strs[1] + "/" + strs[2] + "/";
var file = path + strs[3];
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
File.Copy(filesL[i], file, true);
}
else
Debug.LogError("长度错误!");
}
}
if (transform.Find("OK"))
{
transform.Find("OK").gameObject.SetActive(true);
}
if (filesL[0].Contains("Questions") || filesL[0].Contains("Scores"))
return;
//传输完成,删除文件
for (int i = 0; i < filesL.Length; i++)
{
File.Delete(filesL[i]);
}
}
private void Task_ProgressChanged(float arg1, float arg2)
{
if (transform.Find("Slider"))
{
transform.Find("Slider").GetComponent<Slider>().value = arg2 / 100;
}
else
{
transform.Find("process").GetComponent<Image>().fillAmount = arg2 / 100;
}
transform.Find("indicator").GetComponent<Text>().text = (int)arg2 + "%";
}
}