using XGame.Framework.Asyncs; using System; using UnityEngine.Networking; namespace XGame.Framework.FileSystem { internal sealed class ReadFileAsync : Async, IReadFileAsync { public string FilePath { get; private set; } public bool Success { get; private set; } public byte[] Data { get; private set; } public ReadFileAsync(string filePath) { FilePath = filePath; } public void Start() { if (string.IsNullOrEmpty(FilePath)) { Success = false; Exception = new Exception($"源文件路径为空..."); Completed(); return; } WebRequestRead(); } private void WebRequestRead() { var uri = new Uri(FilePath); var request = UnityWebRequest.Get(uri); var operation = request.SendWebRequest(); operation.completed += (op) => { var bRet = request.result == UnityWebRequest.Result.Success && string.IsNullOrEmpty(request.error); if (!bRet) { Log.Error($"ReadFileAsync failed. FilePath:{FilePath} UnityWebRequest Result:{request.result} Error:{request.error}"); } Success = bRet; Data = request.downloadHandler.data; Completed(); }; } } }