How to create texture at runtime (Without using TextureImporter)?
So, what I basically have is: I have a texture Q with some attributes t1,t2,t3... Texture Q is imported into the unity editor. I want to make a script, that will make a new texture P that has the same parameters as texture Q, but: 1. Change Read/Write Enabled to True, 2. Change Max Size to 32, 3. Change Format to RGBA32
My idea is something like:
But this obviously does not work. What can I do about this?
Texture2D Q;
Texture2D clone = new Texture2D(Q.width, Q.height);
clone.SetPixels(Q.GetPixels());
clone.format = TextureFormat.RGBA32;
//clone.isReadable(); //Says in the scriptingAPI docs that this is always true if created from script
clone.Apply();
Something like this? I don't quite understand the 'MaxSize' part though, where is that one com$$anonymous$$g from? :D (If you want to resize the texture though, use 'texture2D.Resize()')
Answer by The-Peaceful · May 10, 2021 at 07:54 AM
Ok, looking at it again I think you've understood something wrong. Settings like read/write and maxsize are Importer settings which tell the importer to what maxSize the texture will be compressed and read/write is, when the texture is made from code, of course always true (because otherwise you wouldn't be able to make one :D )
Texture2D Q;
Texture2D clone = new Texture2D(Q.width, Q.height);
clone.SetPixels(Q.GetPixels());
clone.format = TextureFormat.RGBA32;
clone.Apply();
This code should work for cloning a Texture and setting the format to RGBA32. If what you are meaning to do is then get that texture smaller use clone.Resize(int width, int height, TextureFormat format, bool hasMipMap);
. If you are trying to store it as an asset at runtime, that will only work in the editor. If that is what you are trying to do you can do it by using the AssetDatabase, but as I said this will only work for working inside the editor, so make sure to not include this in your build!
I can say this solved 75% of my problem. But about resize function. How can I know how much to resize the texture to be 32? That 32 u can find in: 1.Click on the texture 2.In the inspector, go to advanced settings 3.Default 4.MaxSize. There is 32, 1024,2048, ... 5. I need maxSize to be 32, so how much should I resize my texture?
Yes that's what I thought. So you want the compression MaxSize to be 32? That will not be possible without using the TextureImporter, because the compression stuff is only for assets of type texture. But you can resize it via Script like so:
//Either like this (though I don't know but there might be some details getting lost with this one and you call resize twice so a bit less efficient)
if(clone.width > 32){
clone.Resize(32, clone.height);
}
if(clone.height > 32){
clone.Resize(clone.width, 32);
}
//Or a bit less code but not as neat looking :D
if(clone.width > 32 || clone.height > 32){
clone.Resize(clone.width > 32 ? 32 : clone.width, clone.height > 32 ? 32 : clone.height);
}
https://docs.unity3d.com/ScriptReference/Texture2D.Resize.html https://docs.unity3d.com/2018.4/Documentation/Manual/class-TextureImporterOverride.html
Your answer
Follow this Question
Related Questions
Better way to edit RawImage texture data?,Better way to edit a RawImage texture? 0 Answers
How would I go about color correcting a Raw Image? 0 Answers
Unable to save generated/rendered Texture2D to use for billboard 3 Answers
How can I reset the score counter? 0 Answers
Material.SetTexture doesn't work if not "_MainTex" 0 Answers