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.

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.