- Home /
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.
I have the exact same problem. Have you found any solution?
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.
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.
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. :(
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).
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;
};
}