- Home /
How to set a texture resource to texture type "GUI" by script?
Using the editor, I can select a texture asset (PNG file) and set its "texture type" to "GUI" manually. This allows it to be displayed pixel-perfect in the UI.
My game generates PNG files automatically (saved in the resources folder) that are then displayed in the UI. So it needs to load these at runtime with texture type "GUI" instead of the default "texture".
So how can you set the texture type of assets through scripting?
FYI, saving textures to the resources folder only works in the editor. It will not work in a build.
I was sort-of aware of that. Shouldn't be an issue tho, when running the game as stand-alone build it should save this stuff to some user data folder, not resources.
For pixel perfect, have you looked at GUITextures using pixelInset? That's what they are advertised as being for.
Answer by FlyingOstriche · Jun 14, 2013 at 03:59 PM
You can set the guitexture directly without saving as png doing this ...
var tex = new Texture2D(1, 1);
tex.SetPixels(new Color[] {Color.white});
tex.Apply();
style.normal.background=tex;
Or you can change import settings
var bytes=tex.EncodeToPNG();
string path="Assets/Resources/... .png";
File.WriteAllBytes(path,bytes);
AssetDatabase.Refresh();
AssetDatabase.ImportAsset(path);
TextureImporter importer = AssetImporter.GetAtPath(path)as TextureImporter;
importer.textureType=TextureImporterType.GUI;
AssetDatabase.WriteImportSettingsIfDirty(path);
Thanks this was more or less what I was looking for. Only didn't expect it would be editor only. It's for a map editor that generates map preview thumbnails. Since it turns out I need to rescale the final thumbnail textures anyway I'm just importing them as the standard texture type now.
Your answer
Follow this Question
Related Questions
Load texture and shaders at runtime on iPhone 2 Answers
How to properly load textures? 2 Answers
Loadimage Texture at runtime 1 Answer
Altering prefab at runtime. Works in editor, not in build. 2 Answers
Rotate Texture2D 1 Answer