UnityWebRequest 请求网络图片

UnityWebRequest可以用来请求网络上的图片或者请求一张本地图片。

除了GetTexture之外,还有ab包的下载等。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class LoadSp : MonoBehaviour
{
    public Image Image_Main;

    private void Start()
    {
        StartCoroutine(DonwoladPic());
    }

    IEnumerator DonwoladPic()
    {
        string url = "http://api.map.baidu.com/images/weather/day/qing.png";

        //  string path= "E:/Screenshot/caidan.png";
        using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
        {
            yield return request.SendWebRequest();
            Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;

            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);

            Image_Main.sprite = sprite;
        }
    }
}

(1)支持的其他的加载API

UnityWebRequest 请求网络图片_第1张图片

 (2)ab包常用的API。

 

 

你可能感兴趣的:(资源文件)