【Unity】通过WWW和UnityWebRequest下载资源

下载图片

 

通过WWW下载

string url = "https://avatar.csdn.net/E/3/4/3_weixin_38211198.jpg";
IEnumerator Start()
{
    using (WWW www = new WWW(url))
    {
        yield return www;
        Renderer renderer = GetComponent<Renderer>();
        renderer.material.mainTexture = www.texture;
    }
}

WWW 已弃用,用UnityWebRequest代替

 

通过UnityWebRequest下载

string url = "https://avatar.csdn.net/E/3/4/3_weixin_38211198.jpg";
IEnumerator Start()
{
    using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
    {
        yield return request.SendWebRequest();
        Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;

        Renderer renderer = GetComponent<Renderer>();
        renderer.material.mainTexture = texture;
    }
}

 

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页