简体   繁体   中英

Unity - Set material from Downloaded image instead of resources

I can successfully load a material saved in my Unity project using the code below:

RenderSettings.mat1 = (Material)Resources.Load ("images/img1.jpg", typeof(Material));

However, I am now trying to load an external image by downloading it.

Texture2D imgDownloaded;
    string url = "http://www.intrawallpaper.com/static/images/1968081.jpg";

void Start()
    {
        StartCoroutine(getImg());
        fucntionx ();
    }

public void functionx()
{

    RenderSettings.mat1 = (Material)imgDownloaded;

}

IEnumerator getImg()
{
    yield return 0;

    WWW dl = new WWW(url);

    yield return dl;

    imgDownloaded = dl.texture;
}

However, I get the message that I cannot convert from Texture2D to Material .

Is there any way to fix this?

Try:

yourMaterial.mainTexture = yourTexture;

A material consists of many textures, so naturally you can't convert between them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM