- Home /
AssetPostprocessor reference to asset being processed?
I wrote this class to streamline import settings on a bunch of UI textures. In addition to setting import settings, I want to create a material for each texture. This works great if I use "Reimport" from Unity after the textures have been imported, but upon the first automatic import, the material's texture is never set. It's as if:
(Texture2D)AssetDatabase.LoadAssetAtPath(assetPath,typeof(Texture2D));
returns null. Note that this is the only technique I could figure out in order to get a reference to the current asset being imported. It seems like there would be a built-in pointer to it, but I found nothing in the docs. Does anyone know why that would return null upon first import? Or if there is a better way to get a reference to the Texture2D that is being imported so I don't have to look it up using LoadAssetAtPath() ?
[Edited the code to reflect the answer below.]
using UnityEngine; using System.IO; using System.Collections; using UnityEditor;
public class UITextureImport : AssetPostprocessor { void OnPreprocessTexture() { if (assetPath.Contains("Assets/Textures/UI/") && assetPath.Contains(".png")) { // Set specific properties for UI textures. TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.textureType = TextureImporterType.Advanced; textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor; textureImporter.mipmapEnabled = false; textureImporter.filterMode = FilterMode.Point; textureImporter.wrapMode = TextureWrapMode.Clamp; } }
void OnPostprocessTexture(Texture2D texture)
{
if (assetPath.Contains("Assets/Textures/UI/") && assetPath.Contains(".png"))
{
// Create a matching material if one doesn't exist.
string materialPath = assetPath.Replace("Assets/Textures/UI/","Assets/Materials/UI/");
materialPath = materialPath.Substring(0,assetPath.LastIndexOf("/") + 1);
if (!Directory.Exists(materialPath))
{
Directory.CreateDirectory(materialPath);
}
materialPath += "/" + Path.GetFileNameWithoutExtension(assetPath) + ".mat";
Material material = (Material)AssetDatabase.LoadAssetAtPath(materialPath,typeof(Material));
if (material == null)
{
// Material doesn't exist, so create it now.
material = new Material(Shader.Find("UnlitAlphaImage"));
material.mainTexture = texture;
AssetDatabase.CreateAsset(material, materialPath);
}
else
{
// Material exists, so only assign the new texture to it.
material.mainTexture = texture;
}
}
}
}
Answer by Molix · Jun 22, 2011 at 03:11 AM
If it is the first time importing, it sounds reasonable that LoadAssetAtPath would not find it yet. Perhaps you could leave the importer settings in OnPreprocessTexture, but move the material creation part to OnPostprocessTexture, since you'll be passed the Texture2D in question.
Ah yes, perfect. I didn't look far enough to see OnPostProcessTexture. This function not only works for the material creation, but the processed texture is passed in as an argument. I knew there had to be an easier way to get a reference to that. Thanks!
Have you used this script in Unity 4.3? I tried to write a script with your script as a base, but the texture won't be applied on material.mainTexture= texture. So for tests I almost copied this script word for word, and it still won't work. The material will be created, and I can modify it in other ways during OnPostProcessTexture (change the color or offset the main texture), but the texture which is imported just won't get connected to the material no matter what. See also my question on this topic: $$anonymous$$aterial.mainTexture not working outside of runtime?
If you could confirm that it works on your side, it would be nice to know.
Your answer
Follow this Question
Related Questions
Change UI Image through a script with AssetDatabase.LoadAssetAtPath 0 Answers
How to update import settings for newly created asset during OnPostprocessAllAssets 3 Answers
Change material with EditorScript 3 Answers
LoadAssetAtPath 1 Answer
AssetDatabase.LoadAssetAtPath() not working after any sort of project change. 0 Answers