using AX.NetworkSystem; using System; using System.IO; using UnityEngine; public class UploadFileTest : MonoBehaviour { public string SessionTag; public string[] FileList; public string Folder; private string[] fullnameList; private float[] progressList; private bool[] completedList; private void Start() { if (FileList == null) Debug.LogError("出大事了!"); progressList = new float[FileList.Length]; completedList = new bool[FileList.Length]; fullnameList = new string[FileList.Length]; var upload = FileTransfer.GetUploadPath(); for (var i = 0; i < FileList.Length; ++i) fullnameList[i] = upload + FileList[i]; } private void OnGUI() { GUILayout.BeginArea(new Rect(0, 0, 500, 400)); GUILayout.BeginVertical(); if (GUILayout.Button("开始上传一个文件")) { for (var i = 0; i < FileList.Length; ++i) { var fullname = FileList[i]; NetworkManager.GetSessionByTag(SessionTag).UploadFileAsync(fullname, OnProgressChanged, OnCompleted); } } if (GUILayout.Button("开始上传一个文件夹")) { var baseFolder = FileTransfer.GetUploadPath(); var files = Directory.GetFiles(baseFolder + Folder, "*.*", SearchOption.AllDirectories); for (var i = 0; i < files.Length; ++i) { var fullname = files[i].Replace(baseFolder, ""); if (fullname.EndsWith(".meta")) continue; NetworkManager.GetSessionByTag(SessionTag).UploadFileAsync(fullname, OnFolderCompleted); } } for (var i = 0; i < FileList.Length; ++i) { GUILayout.BeginHorizontal(); GUILayout.Label(FileList[i] + ": 进度 "); GUILayout.Label(progressList[i].ToString() + ", 完成 "); GUILayout.Label(completedList[i] ? "√" : "×"); GUILayout.EndHorizontal(); } GUILayout.EndVertical(); GUILayout.EndArea(); } private void OnFolderCompleted(string fullname) { Debug.Log(fullname); } private void OnCompleted(string fullname) { var index = Array.IndexOf(fullnameList, fullname); completedList[index] = true; } private void OnProgressChanged(string fullname, float progress) { var index = Array.IndexOf(fullnameList, fullname); progressList[index] = progress; } }