• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Gillissie · Jun 22, 2011 at 02:04 AM · assetdatabaseassetpostprocessorloadassetatpath

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gillissie · Jun 22, 2011 at 05:50 AM 0
Share

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!

avatar image fvhein · Feb 28, 2014 at 05:40 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to look for a material in Assets folder and sub-folders? 1 Answer

Only PostProcess/PreProcess on new assets 1 Answer

Could not load copied asset via AssetDatabase.LoadAssetAtPath 4 Answers

Is there a way to get a unique hash for an asset? 1 Answer

AssetPostprocessor - OnPostprocess* asset not in the AssetDatabase yet 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges