Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by siberman · May 20, 2013 at 06:34 AM · editortexturematerialpostprocess

OnPostprocessTexture assign Texture to Material

Hello,

So i'm trying to create a material and assign a texture to it with OnPostprocessTexture, as i have seen done on other posts. Everything is created fine, but the material, although named after the texture i apply to it, has no texture applied.

This is what i'm calling from OnPostprocessTexture, it's the same as i've seen in other posts, i just can't see what's going wrong.

 void CreateMaterial ( string design, string panoNo, Texture2D texture, string filePath )
     {
         string materialPath = "Assets/Textures/Panos/" + design + "/" + panoNo + "/Materials/" + Path.GetFileNameWithoutExtension( assetPath ) + ".mat";
         Material material = (Material)AssetDatabase.LoadAssetAtPath( materialPath, typeof( Material ) );
         
         
         if( material == null )
         {
             material = new Material( Shader.Find( "Mobile/Diffuse" ) );
             
             material.mainTexture = texture;
             //material.mainTextureOffset = new Vector2( 30, 20 );
             AssetDatabase.CreateAsset( material, materialPath );
             Debug.Log( material.name );
             Debug.Log( material.mainTexture.name );
             
             
         }
         else
         {
             material.mainTexture = texture;
         }
     }

I've tried loading the texture from the asset database then applying it, different shader types, stabs in the dark now.

Any help would be greatly appreciated.

Cheers.

Comment
Add comment · Show 2
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 Mockarutan · Sep 23, 2013 at 07:50 PM 0
Share

I have the exact same problem. Have you found any solution?

avatar image CyberMew · Mar 04, 2014 at 12:36 PM 0
Share

Same issue here. texture (from the argument) seems to be valid, and Sprite.Create seems to be doing fine, but the final result is it doesn't point to any texture at all.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Dmitry Pyalov · Mar 04, 2014 at 02:15 PM

Try several things.

Firstly, mark material dirty with EditorUtility.SetDirty - this is the common problem when you modify an asset (or any component or game object) and Unity doesn't serialize its properties.

Don't forget to save assets also.

Secondly, try to load texture from AssetDatabase, not using the reference from the method arguments.

Thirdly, you can try to delay the call via EditorApplication.delayCall callback, just to make sure, Unity has done all the import stuff.

Comment
Add comment · Show 3 · 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 CyberMew · Mar 05, 2014 at 07:18 AM 0
Share

I'm not seeing the delayCall documentation, where can I find it?

I am only trying to create a Sprite from given texture for SpriteRenderer component. I tried loading from AssetDatabase, but it returns null, maybe because it hasn't been postprocessed finished yet(?). The gameobject still doesn't point to the correct sprite texture. :(

avatar image Dmitry Pyalov · Mar 05, 2014 at 12:26 PM 0
Share

http://docs.unity3d.com/412/Documentation/ScriptReference/EditorApplication-delayCall.html

Yeah, that's quite a bit of documentation :)

As far as I understand you just subscribe your delegate like

 EditorApplication.delayCall += $$anonymous$$y$$anonymous$$ethod;

And then it gets executed when Unity is ready to execute it (maybe after the postprocessor code is done)

I use this method to launch Build from OnGUI, because OnGUI becomes broken another way, as you can't normally run long running operation before correctly finishing GUI (like calling all necessary EditorGUILayout.EndSomething methods).

avatar image fruktprins · Aug 23, 2020 at 08:20 PM 0
Share

This solved it for me, thank you!

     public void OnPostprocessTexture (Texture2D texture) {
         string materialPath = assetPath.Substring (0, assetPath.LastIndexOf ("/") + 1);
         string fileName = assetPath.Substring (assetPath.LastIndexOf ("/") + 1);
         materialPath += fileName.Substring (0, fileName.LastIndexOf (".")) + ".mat";
         Debug.Log ($"Creating material {materialPath}");
 
         $$anonymous$$aterial material = ($$anonymous$$aterial) AssetDatabase.LoadAssetAtPath (materialPath, typeof ($$anonymous$$aterial));
         if (material == null) {
             material = new $$anonymous$$aterial (Shader.Find ("Toon/Rim Outline"));
             AssetDatabase.CreateAsset (material, materialPath);
         }
 
         UnityEditor.EditorApplication.delayCall += () => {
             material = ($$anonymous$$aterial) AssetDatabase.LoadAssetAtPath (materialPath, typeof ($$anonymous$$aterial));
             texture = (Texture2D) AssetDatabase.LoadAssetAtPath (assetPath, typeof (Texture2D));
             material.mainTexture = texture;
         };
     }

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mass Assigning Textures to Materials 0 Answers

Mipmap view not working in editor. 0 Answers

How to scale specific part of texture when game object scales? 0 Answers

What is the easiest way to import a large number of images into Unity? 2 Answers

How can I set texture tiling lower 1.0 ? 2 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