- Home /
Finding and changing the import settings on specific textures.
Hi there!
I have a number of texture assets in my project with their "Texture Type" in the inspector set to GUI. I would like to create a script that finds any texture, anywhere in my assets folder that's marked as "GUI", set it to be marked as "Advanced", and switch "Bypass sRGB Sampling" to off.
Here's what I have so far:
using UnityEngine;
using UnityEditor;
public class UpdateGUITextures : EditorWindow {
[MenuItem ("Custom/Update GUI Textures")]
static void Init () {
Object[] tex = Resources.FindObjectsOfTypeAll(typeof(Texture2D)) as Object[];
foreach(Texture2D t in tex){
string path = AssetDatabase.GetAssetPath(t);
if(path != "" && !path.Contains(".ttf")){
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if(textureImporter.textureType == TextureImporterType.GUI){
textureImporter.textureType = TextureImporterType.Advanced;
textureImporter.linearTexture = false;
}
}
}
}
}
I'm getting some odd behaviour. First, it's grabbing a LOT of assets, not just Texture2Ds. It looks like it's grabbing everything in the scene as well, which is why I'm checking if the path != "". Also, if I get it to Debug.Log out a list of assets if their import type is set to GUI, it doesn't look like it's getting them all - textures that have their type set to GUI but their compression format set to something other than Compressed, for example, don't appear to be in the list (after a superficial check)
Any advice here would be greatly appreciated!
EDIT: a bit of an update here -- It does seem to be working in some places, but I often have to run it several times and It's hard to determine which texture it'll fix and which it won't. It may also only load textures that are referenced in the currently loaded scene as well ????? Enlighten me someone!
Answer by flamy · Feb 20, 2014 at 04:46 AM
public class AutoTextureSettingChanger : AssetPostprocessor {
void OnPreprocessTexture () {
TextureImporter importer= (TextureImporter)assetImporter;
if( importer.textureType== TextureImporterType.GUI)
{
importer.textureFormat=TextureImporterFormat.AutomaticTruecolor;
importer.textureType=TextureImporterType.Advanced;
importer.mipmapEnabled = false;
// importer.ClearPlatformTextureSettings("Android");
// importer.ClearPlatformTextureSettings("Web");
// importer.ClearPlatformTextureSettings("Standalone");
// importer.ClearPlatformTextureSettings("iOS");
Debug.Log(importer.assetPath+ " Settings changed");
}
}
}
Keep this file inside the Editor Folder and re import the folder containing all the textures, the textures with GUI selected will be converted to Advanced...
Note: After doing that make sure you comment out the function or delete the class, otherwise when ever you set a texture to GUI, it will revert back to advanced.
Extra note: If you want this to be an utility, have menu that sets an EditorPrefs key to certain values and check for the key too while remporting
if( importer.textureType== TextureImporterType.GUI && EditorPrefs.GetBool("GUIUpdateFlag")== true)
{
}
[MenuItem ("Custom/Update GUI Textures Flag")]
static void Init () {
if(EditorPrefs.GetBool("GUIUpdateFlag"))
{
EditorPrefs.SetBool("GUIUpdateFlag",false);
}
else
{
EditorPrefs.SetBool("GUIUpdateFlag",true);
}
}
Awesome, thanks for the assist. Oddly, it never occurred to me to check the textureType inside OnPreprocessTexture. Good idea. thanks again!