Mass texture importer

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");
		}
	}
}

104075–3998–$massimporttexture_199.unitypackage (2.61 KB)

thank you for sharing this script

you’re welcome :slight_smile: !

Wonderful!

Awsum! Put it on the wiki? :smile:

Cool…I was going to write something like this but now I guess I don’t have to. :slight_smile:

–Eric

When I got time™… aka Gods know when :lol:
No but seriously, I’m glad you guys find it useful, I’ll probably extend it a little to add more options, then throw it on the wiki.

fantasic work just wondering has some one created a script like this to edit multiple texture in Unity (non iPhone)

can some one add a addition function to change the max texture size as well?

Here’s my latest version*… adds an Editor/Texture submenu with multiple default options, and a wizard.
Doesn’t play nice if a texture is selected in the inspector, better to work on a folder.

TODO:

  • make it a window instead of a wizard (cooler formatting)
  • maybe move/copy the menu to Assets menu so it’s accessible on right-click in project view?
  • make a postprocessor version for automating per-folder settings.
  • I have multiple versions of this script lying around , every time I have a new need I add it in the project it’s in, and I always fail to merge the changes… has anyone come up with a silver bullet for synching?

178768–6384–$masstextureimporter_191.unitypackage (2.74 KB)

quite usefull package… thank you for sharing…
learning all the way… :wink:

can you make something similar for mass mesh/object importer and then one can manage them all as a sequence?

thank you so much for sharing. this script has saved us a lot of time.

Using Unity 3.4.0 (free) here… imported the last package, but nothing shows up in Menu or elsewhere…
Compiler reports

Assets/Editor/MassTextureImporter.cs(11,76): error CS0117: `UnityEditor.TextureImporterFormat' does not contain a definition for `Automatic'

Ok, there is an update on the wiki for Unity 3.0 (this works with 3.4 also!)