如何使用Unity从URL加载图像? [英] How to load an image from URL with Unity?

查看:73
本文介绍了如何使用Unity从URL加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请避免让我发疯。

无论我用Google多少次搜索,我总会得到以下代码的(通常不推荐使用)版本:

No matter how many times I google, I always end up with (usually deprecated) versions of the following code:

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。我试图使用的URL存在负载。
请照亮我。

I'm using Unity 5 not 4. The URL I'm trying to load exists. Please shine some light on me.

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

How do I load an image over HTTP and display it in a UnityEngine.UI.Image?

推荐答案

快速澄清:
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;
}

这篇关于如何使用Unity从URL加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆