赞
踩
使用unity加载网上图片并保存到本地的方法。
1:首先你需要在网上搜索图片,然后鼠标放在图片上—右键—拷贝图片地址,然后将拷贝的地址复制到代码中。
2:下面是代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI; public class LoadWWWImage : MonoBehaviour { public Image image; string img_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556255298650&di=d6faf9d53a3ae80038c8e27017e42104&imgtype=0&src=http%3A%2F%2Fpic67.nipic.com%2Ffile%2F20150518%2F12897672_150105002000_2.jpg"; void Start () { StartCoroutine(RYVACBT_iadufa(img_url)); } // https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556255623395&di=efa13766c9f4d9ea446e5f0c47e0a98f&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Fback_pic%2F04%2F39%2F87%2F68584e736ec9a0a.jpg IEnumerator RYVACBT_iadufa(string url) { WWW www = new WWW(url); yield return www; if (www.error == null) { print("---load Succeed---"); Texture2D t = www.texture; Sprite csprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0, 0)); byte[] bytedata = t.EncodeToPNG(); //将下载的图片动态保存到本地 File.WriteAllBytes(Application.dataPath + "/a",bytedata); image.sprite = csprite; } } }
运行unity就能在页面上显示该图片:
然后图片也保存在这里了
更多unity基础会陆续更新:欢迎小伙伴关注下哦。
关注微信公众号:GameResources 获取游戏资源
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。