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());
}
相关推荐
Chandler2414 小时前
一图掌握 网络协议 核心要点
网络协议·tcp/ip·计算机网络·http
软泡芙1 天前
【Unity】HybridCLR:原生C#热更新革命
unity·游戏引擎
十碗饭吃不饱2 天前
WebClient工具调用HTTP接口报错远程主机断开连接
网络·网络协议·http
失散132 天前
分布式专题——33 一台新机器进行Web页面请求的历程
分布式·tcp/ip·http·路由器·交换机
大Mod_abfun2 天前
Unity游戏基础-5(一些细节)
游戏·unity·游戏引擎
Arva .2 天前
HTTP Client
网络协议·http·lua
2501_915106323 天前
CDN 可以实现 HTTPS 吗?实战要点、部署模式与真机验证流程
网络协议·http·ios·小程序·https·uni-app·iphone
千里马-horse3 天前
HTTP、WebSocket、XMPP、CoAP、MQTT、DDS 六大协议在机器人通讯场景应用
mqtt·websocket·http·机器人·xmpp·coap·fastdds
心疼你的一切3 天前
使用Unity引擎开发Rokid主机应用的模型交互操作
游戏·ui·unity·c#·游戏引擎·交互
安卓开发者3 天前
鸿蒙NEXT网络通信实战:使用HTTP协议进行网络请求
网络·http·harmonyos