unity 从硬盘上选择图片并保存到本地资源下

15 篇文章 1 订阅

最近再做一个从电脑上选择图片设置成头像图标之类的功能。

 void OnIconSelectBtnClick()
    {

        #region 调用Window文件选择窗体  选中图片文件
        OpenFileName openFileName = new OpenFileName();
        openFileName.structSize = Marshal.SizeOf(openFileName);
        openFileName.filter = "图片文件(*.png)\0*.png";
        openFileName.file = new string(new char[256]);
        openFileName.maxFile = openFileName.file.Length;
        openFileName.fileTitle = new string(new char[64]);
        openFileName.maxFileTitle = openFileName.fileTitle.Length;
        openFileName.initialDir = Application.streamingAssetsPath.Replace('/', '\\');
        openFileName.title = "图标选择";
        openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
        if (LocalDialog.GetOpenFileName(openFileName))
        {
            projectIconPath = openFileName.file;
        }
        #endregion

        //设置图标
        if (!string.IsNullOrEmpty(projectIconPath))
        {
            //图片保存到本地
            PictureLoad.instance.LoadTextuer("file://"+projectIconPath, Application.dataPath + "/Resources/"+ openFileName.fileTitle);
        }

    }

使用WWW请求来下载图片到本地

 public void LoadTextuer(string LoadPath,string SavePath)
    {
        Debug.Log("开始下载");
        StartCoroutine(LoadImg(LoadPath,SavePath));
    }
    IEnumerator LoadImg(string LoadPath,string SavePath)
    {
        //开始下载图片
        WWW www = new WWW(LoadPath);
        yield return www;
        //下载完成,保存图片到路径filePath
        if (www.isDone==true)
        {
            Debug.Log("下载完成");
            Texture2D texture2D = www.texture;
            byte[] bytes = texture2D.EncodeToPNG();
            File.WriteAllBytes(SavePath, bytes);
        }      

    }

下载完成,可以看到图片已经保存到unity目录下面了。

图片

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值