协程使用
协程下载图片
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class CorutineController : MonoBehaviour
{
private Image image;
void Start()
{
image = GetComponent<Image>();
StartCoroutine(DownLoad());
}
IEnumerator DownLoad()
{
WWW www = new WWW("https://www4.bing.com//th?id=OHR.KirkjufellAurora_ZH-CN7878752057_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp");
while (!www.isDone)
{
Debug.Log("CorutineTest, progress = " + www.progress); // 打印下载进度
yield return null;
}
Texture2D texture2D = www.texture;
texture2D.name = "风景";
image.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
GetComponent<RectTransform>().sizeDelta = new Vector2(texture2D.width, texture2D.height);
File.WriteAllBytes(Application.dataPath + "/GAME/Scripts/HS/Image/风景.png", www.bytes); // 保存图片
}
}
最后效果



6799

被折叠的 条评论
为什么被折叠?



