如何使用Unity从URL加载图像?

时间:2015-08-01 19:43:28

标签: c# unity3d-2dtools

请救我脱离疯狂。

无论我多少次谷歌,我总是最终得到以下代码的(通常已弃用)版本:

IEnumerator setImage(string url) {
    Texture2D texture = profileImage.canvasRenderer.GetMaterial().mainTexture as Texture2D;

    WWW www = new WWW(url);
    yield return www;

    Debug.Log("Why on earh is this never called?");

    www.LoadImageIntoTexture(texture);
    www.Dispose();
    www = null;
}

我使用的是Unity 5而不是4.我尝试加载的网址存在。 请对我有所启发。

如何通过HTTP加载图像并将其显示在UnityEngine.UI.Image中?

6 个答案:

答案 0 :(得分:6)

对于Unity 2018+,使用UnityWebRequest代替WWW类。

void Start(){    
    StartCoroutine(DownloadImage(url));
}

IEnumerator DownloadImage(string MediaUrl)
{   
    UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
    yield return request.SendWebRequest();
    if(request.isNetworkError || request.isHttpError) 
        Debug.Log(request.error);
    else
        YourRawImage.texture = ((DownloadHandlerTexture) request.downloadHandler).texture
} 

答案 1 :(得分:3)

快速澄清: https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

public void yourMethod()
{
   StartCoroutine(setImage("http://url/image.jpg")); //balanced parens CAS
}

IEnumerator setImage(string url) {
    Texture2D texture = profileImage.canvasRenderer.GetMaterial().mainTexture as Texture2D;

    WWW www = new WWW(url);
    yield return www;

    // calling this function with StartCoroutine solves the problem
    Debug.Log("Why on earh is this never called?");

    www.LoadImageIntoTexture(texture);
    www.Dispose();
    www = null;
}

答案 2 :(得分:3)

要异步时:

using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;

public static async Task<Texture2D> GetRemoteTexture ( string url )
{
    using( UnityWebRequest www = UnityWebRequestTexture.GetTexture( url ) )
    {
        //begin requenst:
        var asyncOp = www.SendWebRequest();

        //await until it's done: 
        while( asyncOp.isDone==false )
        {
            await Task.Delay( 1000/30 );//30 hertz
        }

        //read results:
        if( www.isNetworkError || www.isHttpError )
        {
            //log error:
            #if DEBUG
            Debug.Log( $"{ www.error }, URL:{ www.url }" );
            #endif

            //nothing to return on error:
            return null;
        }
        else
        {
            //return valid results:
            return DownloadHandlerTexture.GetContent( www );
        }
    }
}

用法示例:

[SerializeField] string _imageUrl;
[SerializeField] Material _material;

async void Start ()
{
    Texture2D texture = await GetRemoteTexture( _imageUrl );
    _material.mainTexture = texture;
}

答案 3 :(得分:1)

感谢Umair M提出的问题,我发现需要使用统一的StartCoroutine调用此方法。

答案 4 :(得分:1)

我们不能在画布的Image组件上直接应用纹理材料。因此,我们应该从运行时下载的纹理创建一个精灵,然后将其应用于Image组件。

尝试一下,

IEnumerator DownloadImage(string MediaUrl)
    {   
        UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
        yield return request.SendWebRequest();
        if(request.isNetworkError || request.isHttpError) 
            Debug.Log(request.error);
        else{
            // ImageComponent.texture = ((DownloadHandlerTexture) request.downloadHandler).texture;

            Texture2D tex = ((DownloadHandlerTexture) request.downloadHandler).texture;
            Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(tex.width / 2, tex.height / 2));
            ImageComponent.GetComponent<Image>().overrideSprite = sprite;
        }
    } 

答案 5 :(得分:0)

if (!apps[key].aemSchema || !isJQuery) { //code }