A while ago I made an editor script to batch-import a set of textures at once (I used it to automatically remove mipmaps and clamp GUI textures).
I figured it would work well to automatically set texture format to PVRTC for iPhone compatibility… except TextureImporterFormat doesn’t expose iPhone-specific formats (logged as bug 51814).
Never mind, leaving the format to “Automatic” reimports textures as PVRTC 4-bit RGBA by default so it’s a start. I guess it could be useful to other people so here it is…
Just importe the attached package, or copy-paste the follwing script in an Editor folder, and an Editor/Texture menu will appear. Select the textures you want to reimport (individual ones or whole folders), and run the wizard or just the quick options… voilà!
using UnityEditor;
using UnityEngine;
class MassImportTexture : ScriptableWizard
{
public bool changeFormat;
public TextureImporterFormat textureFormat = TextureImporterFormat.Automatic;
public bool changeMipmaps;
public bool mipmapsEnabled;
public bool changeWrapMode;
public TextureWrapMode wrapMode;
void OnWizardUpdate()
{
// are the properties valid?
bool propertiesValid = changeFormat || changeMipmaps || changeWrapMode;
// is the selection valid?
Object[] textures = GetSelectedTextures();
bool selectionValid = textures.Length > 0;
// help
isValid = propertiesValid selectionValid;
errorString = !propertiesValid ? "Select at least one thing to change..." :
!selectionValid ? "Select at least one texture" : "";
helpString = textures.Length + " texture(s) will be reimported.";
}
void OnWizardCreate()
{
// code when first button is clicked
apply();
}
void OnWizardOtherButton()
{
// code when second button is clicked
apply();
}
void apply()
{
ProcessSelection(
changeFormat, textureFormat,
changeMipmaps, mipmapsEnabled,
changeWrapMode, wrapMode);
}
[MenuItem("Editor/Texture/Mass import...")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard(
"Mass import textures", typeof(MassImportTexture),
"Apply Close", "Apply"
);
}
[MenuItem("Editor/Texture/Remove mipmaps + clamp")]
static void RemoveMipmapsAndClamp()
{
ProcessSelection(
false, TextureImporterFormat.Automatic,
true, false,
true, TextureWrapMode.Clamp);
}
[MenuItem("Editor/Texture/Remove mipmaps")]
static void RemoveMipmaps()
{
ProcessSelection(
false, TextureImporterFormat.Automatic,
true, false,
false, 0);
}
[MenuItem("Editor/Texture/Clamp")]
static void Clamp()
{
ProcessSelection(
false, TextureImporterFormat.Automatic,
false, false,
true, TextureWrapMode.Clamp);
}
[MenuItem("Editor/Texture/Convert to iPhone (PVRTC 4-bits RGBA)")]
static void ConvertToIPhone()
{
ProcessSelection(
true, TextureImporterFormat.Automatic,
false, false,
false, 0);
}
static Object[] GetSelectedTextures()
{
return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
}
static void ProcessSelection(bool changeFormat, TextureImporterFormat textureFormat, bool changeMipmaps, bool mipmapsEnabled, bool changeWrapMode, TextureWrapMode wrapMode)
{
if (Selection.objects.Length > 0)
{
Object[] textures = GetSelectedTextures();
if (textures.Length > 0)
{
foreach (Texture2D texture in textures)
{
if (changeFormat || changeMipmaps)
{
string path = AssetDatabase.GetAssetPath(texture);
//Debug.Log("RemoveMipmaps : " + path);
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if (changeFormat)
textureImporter.textureFormat = textureFormat;
if (changeMipmaps)
textureImporter.mipmapEnabled = mipmapsEnabled;
AssetDatabase.ImportAsset(path);
}
if (changeWrapMode)
{
texture.wrapMode = wrapMode;
}
}
Debug.Log(string.Format(
"{0} texture(s) reimported.{1}{2}{3}",
textures.Length,
changeFormat ? "\nformat set to " + textureFormat : "",
changeMipmaps ? "\nmipmaps turned " + (mipmapsEnabled ? "ON" : "OFF") : "",
changeWrapMode ? "\nwrap mode set to " + wrapMode : ""
));
}
else
{
EditorUtility.DisplayDialog("No textures selected!", "No textures found in selected objects.", "OK");
}
}
else
{
EditorUtility.DisplayDialog("No object selected!", "Select at least one object.", "OK");
}
}
}