unity unityWebRequest 通过http下载服务器资源

直接下载不显示进度

csharp 复制代码
    private void OnDownloadAssets()//下载资源
    {
        StartCoroutine(DownloadFormServer_IE(url, savePath));
    }

    //其他方法
    private IEnumerator DownloadFormServer_IE(string url, string path)//从服务器下载资源
    {
        Debug.Log("正在下载" + url);
        UnityWebRequest request = UnityWebRequest.Get(url);
        
        //直接下载不显示进度
        yield return request.SendWebRequest();

        if (request.result== UnityWebRequest.Result.ProtocolError || request.result== UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log(request.responseCode);
            Debug.Log(request.ersror);
              yield break;
        }
        DownloadHandler downloadHandler = request.downloadHandler;
        if (!downloadHandler.isDone)
        {
            Debug.Log("正在下载");
            yield return downloadHandler;
        }
        else
        {
            Debug.Log("下载完成");
            byte[] data = request.downloadHandler.data;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                fs.Write(data, 0, data.Length);
            }
            this.SendEvent(new OnFinishDownloadOneAsset_AssetsManager());
        }
    }

显示下载进度

//注册事件的方法

private void OnDownloadAssets()//下载资源

{

GameController.Instance.StartCoroutine(DownloadFormServer_IE(url, savePath));

}

复制代码
//其他方法
private IEnumerator DownloadFormServer_IE(string url, string path)//从服务器下载资源
{
    Debug.Log("正在下载" + url);
    UnityWebRequest request = UnityWebRequest.Get(url);

    //显示下载进度和速度
    request.SendWebRequest();
    if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
    {
        Debug.Log(request.responseCode);
        Debug.Log(request.error);
        yield break;
    }
    while (!request.isDone)
    {
       //下载速度
        string netSpeedStr = "0kb";
        if (request.downloadHandler != null && request.downloadHandler.data != null)
        {
            float speed = request.downloadHandler.data.Length / 1024;
            if (speed > 1024)
            {
                speed = (speed / 1024);
                netSpeedStr = speed.ToString("f") + "mb";                
            }
            else
            { 
                netSpeedStr = speed + "kb";                
            }
        }
        //下载进度 和速度
        Debug.Log("进度" + (request.downloadProgress*100).ToString("f") + "%  "+ netSpeedStr );
        yield return null;
    }

    DownloadHandler downloadHandler = request.downloadHandler;
    Debug.Log("下载完成");
    byte[] data = request.downloadHandler.data;
    using (FileStream fs = new FileStream(path, FileMode.Create))
    {
        fs.Write(data, 0, data.Length);
    }
    this.SendEvent(new OnFinishDownloadOneAsset_AssetsManager());
}
相关推荐
雷帝木木4 小时前
Flutter for OpenHarmony:Flutter 三方库 cbor 构建 IoT 设备的极致压缩防窃协议(基于标准二进制 JSON 表达格式)
网络·物联网·flutter·http·json·harmonyos·鸿蒙
ht巷子5 小时前
boost.asio网络学习:Http Server
网络·c++·http
C蔡博士6 小时前
Unity像素游戏精灵导入最佳实践
游戏·unity·游戏引擎·2d·pixel arts
GLDbalala8 小时前
Unity基于自定义管线实现贴花效果
unity·游戏引擎
深念Y9 小时前
Chrome MCP Server 配置失败全记录:一场历时数小时的“fetch failed”排查之旅
前端·自动化测试·chrome·http·ai·agent·mcp
小江的记录本10 小时前
【JWT】JWT(JSON Web Token)结构化知识体系(完整版)
前端·网络·web安全·http·网络安全·json·安全架构
小江的记录本10 小时前
【会话:Cookie与Session】Cookie与Session的区别(附对比表)
java·数据库·后端·sql·http·https·安全架构
上证50指数etf10 小时前
unity面试总结(项目篇)
unity·游戏引擎
老虎062711 小时前
Spring mysql 网络接口( HTTP 方法与增删改查, Spring MVC Controller 接收参数的常用注解与参数传递方式)
mysql·spring·http
楼田莉子11 小时前
Linux网络:应用层HTTP网络协议
网络·c++·后端·网络协议·学习·http