- Home /
Dynamic texture resizing or switching between texture sets of different sizes at runtime
Just wondering if there is any native function in Unity to dynamically resize textures at runtime? Alternatively a simple way to swap out texture assets at runtime?
I need to be able to provide a single build that can work for someone with say 128mb, 256 or the target 512mb+. So depending upon the gpu ram, one of 3 different texture sizes will be used, however I certainly don't want to include all 3 versions of each texture in a single build. Essentially its just like how commercial games run, in which the textures are dynamically/pre-created in scaled down versions, so the game can easily run on different configurations of available ram.
For example back in good old Director I had two methods available. The first was to use a function called 'scaledown' which simply halved the size each time it was called on a texture. The second option was to store all the texture images in a single external cast library and then make two versions, one full size, the other all half size. Then at runtime you could just load the appropriate castlibrary. Actually there is a third, which would be to have all texture images imported as 'linked' and simply change the pathname at runtime.
I can't find a similar function to 'scaledown' and suspect that since Unity doesn't appear to include the source images in a build, it wouldn't be possible?
I guess I could import the textures at runtime, but not sure if I lose some of the benefits of having them in the editor, such as setting values in the inspector?
AssetBundles may work for this, though i'm still looking into them so not sure of their requirements or restrictions.
So i'm wondering how do other developers approach this situation?
Answer by Noise crime · Jun 11, 2010 at 09:59 PM
Stumbled upon the answer (at least it will do for now) in Edit->Project Settings->Quality inspector, where you can set the 'Texture Quality' to 'full res', 'half-res' etc, with each Quality mode having its own setting.
Yet to test it fully, but should suffice for my needs.
Answer by Mike 3 · Jun 10, 2010 at 05:06 PM
If you let unity calculate mipmaps, you can just use GetPixels(mipmapLevel) and it'll give you pixels back relating to a texture which is the next power of two down for each level
That way you can easily make a new texture from those pixels
Edit:
You could do something like this to half the original texture, as long as the texture is readable:
Texture2D temp = new Texture2D(originalTexture.width / 2, originalTexture.height / 2);
temp.SetPixels(originalTexture.GetPixels(1));
temp.Apply();
byte[] png = temp.EncodeToPng();
Destroy(temp);
originalTexture.LoadImage(png);
It's a little bit of a roundabout method, but you could wrap it in a function and make it half any texture
Nice answer, though probably impractical as that would mean having all the textures already loaded. I'm looking at possibly in excess of 512mb of textures that are all in view, so on a 256mb card that method would run out of memory? Though its definitely something i'll keep in $$anonymous$$d.
Edited the post with an example way to do it - The alternative method would be to just destroy the original texture and use the new one, which wouldn't fill the graphics card up
Your answer
Follow this Question
Related Questions
Swapping Textures vs Swapping Meshes 1 Answer
Uploading textures to be used for 3d model at runtime? 0 Answers
How to change Textures after build the game? 2 Answers
How to load multiple textures and sounds 0 Answers
Change size of Mesh at runtime 3 Answers