I’m trying to load a PNG file from the internet using a url then setting it as a texture of a material (Cut-Out Shader). I tried using www.LoadImageIntoTexture and www.texture, but I cant seem to get it right. This current code will freeze during the start of the game. I use Debug.Log(renderer.material.mainTexture.GetType() to see what type I need and it is a Texture2D.
using UnityEngine;
using System.Collections;
public class getSkin : MonoBehaviour {
public string skin;
public string url; //http://www.minecraftskins.com/newuploaded_skins/skin_20140518235659161371.png
//Texture2D tex = renderer.material.mainTexture as Texture2D;
// Use this for initialization
void Start () {
skin = GameObject.Find ("PlayerPrefs").GetComponent<SingleP>().skin;
url = GameObject.Find("PlayerPrefs").GetComponent<SingleP>().url;
//tex = new Texture2D(16,16,TextureFormat.DXT1,false);
WWW www = new WWW(url);
while (true){
if(www.isDone){
Debug.Log("True");
renderer.material.mainTexture = www.texture;
}
}
//www.LoadImageIntoTexture(renderer.material.mainTexture);
}
// Update is called once per frame
void Update () {
if(skin == "Link")
{
this.renderer.material.mainTexture = Resources.Load("Link") as Texture;
}
else if (skin == "Vegeta")
{
this.renderer.material.mainTexture = Resources.Load("Vegeta") as Texture;
}
}
}
Thanks for the help.
EDIT:
I figured it out. Seems pretty simple now ;). Here it is if anyone needs the same help. Of course, you got to set url and/or skin by yourself.
using UnityEngine;
using System.Collections;
public class getSkin : MonoBehaviour {
public string skin;
public string url; //http://www.minecraftskins.com/newuploaded_skins/skin_20140518235659161371.png
// Use this for initialization
IEnumerator Start() {
url = GameObject.Find("PlayerPrefs").GetComponent<SingleP>().url;
skin = GameObject.Find("PlayerPrefs").GetComponent<SingleP>().skin;
if(url != ""){
WWW www = new WWW(url);
yield return www;
renderer.material.mainTexture = www.texture;
}
if(url == "")
{
renderer.material.mainTexture = Resources.Load("Skin") as Texture; //set default texture if none choosen
}
}
// Update is called once per frame
void Update () {
renderer.material.mainTexture.filterMode = FilterMode.Point;
renderer.material.mainTexture.wrapMode = TextureWrapMode.Repeat;
if(skin == "Link")
{
this.renderer.material.mainTexture = Resources.Load("Link") as Texture; //get from Resources Folder
}
else if (skin == "Vegeta")
{
this.renderer.material.mainTexture = Resources.Load("Vegeta") as Texture;
}
}
}