- Home /
How can I change the default max size of a texture from an editor script (in import settings)?
I'm importing a bunch of high resolution images. I need the images look as they do in photoshop. The way I found to do this is by going to the specific sprites import settings, turning on "Override for PC, Mac and Linux Standalone" (in the bottom section labeled default) and setting the "max size" property to 8192. This works pretty well.
Unfortunately I need to do this to many images, which will be tedious and eat up a lot of time. I know I can use the AssetPostprocessor to alter the import settings of a sprite when it is imported (using OnPostprocess set of functions) or before it is imported (using the OnPreprocess set of functions). However, I don't know how to access the "max size" property from a script. How can I do that?
Answer by monotoan · Nov 10, 2017 at 09:41 PM
Here's a way to do this that worked for me. It's a little awkward because it involves creating a temporary struct for import settings, reading values into that struct from the texture importer system, changing some of those values, then passing the changed struct back to the importer. But I've tried to comment everything clearly below!
using UnityEngine;
using UnityEditor;
// ^^^ Make sure you add the UnityEditor using directive! ^^^^
//Also make your script inherit from AssetPostprocessor, NOT Monobehaviour
public class SetTexImportSizeViaScript : AssetPostprocessor
{
//function that's called when a texture starts to be imported
void OnPreprocessTexture()
{
//get a reference to the built-in TextureImporter...
TextureImporter importer = (TextureImporter)assetImporter;
//create a new empty TextureImporterSettings struct...
TextureImporterSettings textureImporterSettings = new TextureImporterSettings();
//read the current import settings from the Texture Importer
//into our new importer settings struct (basically filling the empty struct with values)
importer.ReadTextureSettings(textureImporterSettings);
//change the maxTextureSize setting in our settings struct
textureImporterSettings.maxTextureSize = 32;
//pass the settings struct, with the changed maxTextureSize value, back into the importer
//(e.g. apply the changed settings to the importer)
importer.SetTextureSettings(textureImporterSettings);
}
}
Your answer
Follow this Question
Related Questions
Modify sprite geometry using AssetPostprocessor 0 Answers
2D sprite renders a small part of another one with "Sprite Mode : Multiple" 0 Answers
Does Unity decompress Truecolor RGBA format textures when loading? 0 Answers
Stop Texture2D Compression 0 Answers
Material.mainTexture not working outside of runtime? 1 Answer