I need to download an image from a server, then transform it in a Cubemap and finally put this CubeMap in my Skybox.
I work with C#.
I came up with this code :
public string url = "image/url.jpg";
void Update() {
// When trigger, we start the process
if (Input.GetKeyDown("f")) {
// start Coroutine to handle the WWW asynchronous process
StartCoroutine("setImage");
}
}
IEnumerator setImage () {
Texture2D tex;
tex = new Texture2D(2048, 2048, TextureFormat.RGBA32, false);
WWW www = new WWW(url);
//Texture myGUITexture = Resources.Load("23") as Texture;
Debug.Log (www.bytesDownloaded);
Debug.Log (www.progress);
Debug.Log (www.texture);
yield return www;
// we put the downloaded image into the new texture
www.LoadImageIntoTexture(tex);
// new cubemap
Cubemap c = new Cubemap(2048, TextureFormat.RGBA32, false);
Color[] CubeMapColors;
CubeMapColors = tex.GetPixels();
c.SetPixels(CubeMapColors, CubemapFace.PositiveX);
// we set the cubemap from the texture pixel by pixel
c.Apply();
//NewTexture.isPowerOfTwo = true;
//Debug.Log (RenderSettings.skybox.GetTexture ("_Tex"));
// We change the Cubemap of the Skybox
RenderSettings.skybox.SetTexture("_Tex", c);
}
I commented all the code to explain what I think I am doing.
I made this "trick" of creating the Cubemap pixel by pixel because the Editor way to do it (which is incredibly simple by the way) seems to be not possible from other people posts I could read.
Eventually, the result was just a bunch of grey pixels.
I don't really know what screw up so much in my process, the only "shadow" point I see is the TextureFormat.
I chose RGBA32 because when I look at my Unity Editor, I see BC7 format, which is error logged as non possible for SetTexture and from the doc they explain it may be decompressed in RGBA32.
Of course there is no remaining error in the console.
I am really surprised there is not a easy way to do it. I mean not necessarily taking from http an image and put it in skybox, but just change the texture of a skybox. Am I missing something ?