- Home /
Change texture non-power of 2 setting at runtime?
We have an app that downloads images and creates textures from them using Texture2d.LoadImage() while you are using the app (runtime). These textures are used in the GUI.
Unfortunately, the textures created from these images look like crud, because Unity automatically scales them for power of 2 dimensions.
I know you can change a texture to a gui texture in the inspector, or you can use TextureImporter.npotScale = TextureImporterNPOTScale.None in an editor script, and the problem will be solved.
My question is, is there any way to accomplish this or any similar solution or workaround at runtime?
I would like to know this as well!
Since it automatically scales it to the nearest power of 2, the current workaround is to manually create the images with those dimensions in $$anonymous$$d, which is reallllly bad.
I did not try this myself but out of curiosity, doing something like this does not work? : Texture2D test = new Texture2D(100,100); test.LoadImage(myData);
Another solution might be to load the image using LoadImage and THEN creating another texture of the wanted size (say, 100x100) and using GetPixels() and SetPixels() to fill it correctly.
Just thinking out loud, did not try myself.
$$anonymous$$aybe I'm missing something, but loading image bytes into texture after downloading it with WWW class work just fine for me. $$anonymous$$y image is 1920x1200, and the resulting texture has the same dimensions. Nothing gets scaled to POT. Displaying it in GUI works ok as well.
Answer by cowlinator · Jun 21, 2014 at 01:40 AM
I forgot about this question. Anyway, I figured this out.
new Texture2D(...)
does not, in fact, coerce a texture to be a square power of 2. The reason my textures looked like crud is that mip-mapping is turned on by default, and for whatever reason, unity sometimes uses smaller mip-maps for ui elements.
to turn off mip-mapping, use
new Texture2D(w, h, textureFormat, false)