Unity Tip: Importing Textures

Usually when working with textures in Unity you may have a few specific requirements on how textures should be imported. Compression, mip-maps, filter mode, and wrap are usually my most commonly set flags. I’ve taken to writing a small chunk of editor code to help me set the properties of my images without requiring manually flipping the bits. This means I can select then right-click on all my images and set the texture settings to exactly what I want with one click:

unity_import_texture

The full script is below. I iterate over the currently selected objects in the editor and if it is a Texture2D ( Sprites exist within Texture2Ds so you can select them as well). I then create a TextureImporter object for the texture, which lets you set the ‘bake’ settings. You can see that I have some non-standard things I’m doing, partially due to this project being an 8-bit style. I want the filter mode set to ‘Point’. And since the images are tiny I do not think texture compression is worthwhile so I set it to true color. Among a few other details.

 


using UnityEngine;
using UnityEditor;
public static class ProjectMenuUtil
{
[MenuItem ("Assets/Import For PixelWave")]
public static void ImportForWave()
{
foreach(Object o in Selection.objects)
{
if(o is Texture2D)
{
string path = AssetDatabase.GetAssetPath(o);
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
importer.filterMode = FilterMode.Point;
importer.isReadable = true;
importer.textureType = TextureImporterType.Advanced;
importer.mipmapEnabled = false;
importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
importer.spriteImportMode = SpriteImportMode.None;
importer.SaveAndReimport();
}
}
}
[MenuItem ("Assets/Import For UI")]
public static void ImportForUI()
{
foreach(Object o in Selection.objects)
{
if(o is Texture2D)
{
string path = AssetDatabase.GetAssetPath(o);
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
importer.filterMode = FilterMode.Point;
importer.textureType = TextureImporterType.Advanced;
importer.mipmapEnabled = false;
importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
importer.SaveAndReimport();
}
}
}
}