Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges