Unity Ftp、Http下载资源的简例

 Http下载文件:

using System;
using System.ComponentModel;
using System.Net;
using UnityEngine;

public class TestDownloadBehaviour_http : MonoBehaviour
{
    public string RemoteFileURI;
    public string SaveFilePath;

    // Start is called before the first frame update
    void Start()
    {
        DownloadFile();
    }

    // Update is called once per frame
    void DownloadFile()
    {
        using (WebClient wc = new WebClient())
        {
            try
            {
                wc.Proxy = null;
                Uri address = new Uri(RemoteFileURI);
                //调用DownloadFile方法下载文件
                // wc.DownloadFile(textBox1.Text.ToString(), textBox2.Text.ToString());

                //调用DownloadFileAsync异步下载文件
                wc.DownloadFileAsync(address, Application.dataPath + "/" + SaveFilePath);

                wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgessChanged);
                //下载完成的响应事件
                wc.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDownloadFileComplited);
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message);
            }
        }

    }

    void OnDownloadProgessChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        Debug.LogFormat("progress = {0}, total = {1}, received = {2}", e.ProgressPercentage, e.TotalBytesToReceive, e.BytesReceived);
    }

    void OnDownloadFileComplited(object sender, AsyncCompletedEventArgs e)
    {
        Debug.LogFormat("下载完成");
    }
}

FTP下载文件:

using System;
using System.ComponentModel;
using System.Net;
using UnityEngine;

public class TestDownloadBehaviour : MonoBehaviour
{
    public string RemoteFileURI;
    public string SaveFilePath;

    long _FileSize; // FTP下载时,无法获取到整个文件Size。所以提前调用WEB API来获取Size,然后进行下载。

    // Start is called before the first frame update
    void Start()
    {
        DownloadFile();
    }

    // Update is called once per frame
    void DownloadFile()
    {
        FtpWebRequest f = WebRequest.Create(RemoteFileURI) as FtpWebRequest;
        f.Method = WebRequestMethods.Ftp.GetFileSize;
        FtpWebResponse fr = f.GetResponse() as FtpWebResponse;
        _FileSize = fr.ContentLength;
        fr.Close();

        using (WebClient wc = new WebClient())
        {
            try
            {
                wc.Proxy = null;
                Uri address = new Uri(RemoteFileURI);
                //调用DownloadFile方法下载文件
                // wc.DownloadFile(textBox1.Text.ToString(), textBox2.Text.ToString());
                
                //调用DownloadFileAsync异步下载文件
                wc.DownloadFileAsync(address, Application.dataPath + "/" + SaveFilePath);

                wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgessChanged);
                //下载完成的响应事件
                wc.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDownloadFileComplited);

            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message);
            }
        }

    }

    void OnDownloadProgessChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        Debug.LogFormat("progress = {0}, total = {1}, received = {2}", (float)e.BytesReceived / _FileSize, _FileSize, e.BytesReceived);
    }

    void OnDownloadFileComplited(object sender, AsyncCompletedEventArgs e)
    {
        Debug.LogFormat("下载完成");
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity是一款非常流行的游戏开发引擎,拥有各种各样的功能和特性,它的插件资源丰富多彩,可以提高游戏开发的效率和质量。Unity的插件资源可以在Unity Asset Store上找到并下载,这是Unity的官方资源平台。 在Unity Asset Store中,有许多分类的插件资源,比如人物角色、场景建造、音效制作、UI设计、游戏系统等。用户可以根据自己的需要选择相应的资源下载Unity Asset Store中的插件资源大多数是付费的,但也有一些免费资源供用户使用。此外,Unity Asset Store还提供了用户评价和评论功能,方便用户在选择插件资源的时候了解其他用户的使用情况和体验反馈。 当然,除了Unity Asset Store外,还有一些第三方的插件资源平台可以供用户下载。但需要注意的是,这些资源平台并非官方提供,安全性和质量也不及官方资源平台有保障。 插件资源下载的步骤也非常简单。首先,在Unity中打开Asset Store工具;然后,根据自己的需求,搜索相应的资源;最后,选择需要的资源进行下载和导入即可。在导入资源之前,需要为其设置正确的路径和导入设置,以确保资源可以在游戏中正常使用。 总的来说,Unity插件资源下载使用非常方便,是Unity游戏开发的重要组成部分。用户只需要选择适合自己的资源,进行下载和导入,就可以在游戏开发中节省大量的时间和精力,提高游戏的质量和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值