AssetPostprocessor help -

I thought I’d start a new post for this for people relatively new to C# like me who might specifically be searching for help with the AssetPostprocessor.

I’m pretty competent in programming logic but C# is new to me - precisely why I’d like to use it, to get more comfortable with it. In the editor, I am trying to procedurally create a texture which is saved to my Assets/Textures folder…then upon import, is automatically assigned to a newly created game object. Someone advised that I use AssetPostprocessor so that I wait until the asset is completely imported.

I would like to try this method, but I am at a loss as to how to use it in this way. This is how I am currently creating the new texture and trying to assign it:

	byte[] bytes = tempTexture.EncodeToPNG();
  	System.IO.File.WriteAllBytes(Application.dataPath+"/Textures/"+newTexName+".png", bytes);
        AssetDatabase.ImportAsset(Application.dataPath+"/Textures/"+newTexName"+".png");
	tempTexture = (Texture2D) Resources.LoadAssetAtPath ("Assets/Textures/"+newTexName+".png", typeof(Texture2D));
	Material myNewMat = new Material(Shader.Find ("Diffuse"));
	AssetDatabase.CreateAsset(myNewMat,"Assets/Materials/mat_"+newTexName+".mat");
        ///// This obj is the variable GameObject carried on from earlier in the code
        obj.renderer.material = Resources.LoadAssetAtPath ("Assets/Materials/mat"+newTexName+".mat", typeof(Material));
        obj.renderer.material.mainTexture = tempTexture;

This ‘works’ - it just takes a while for the importer to ‘refresh’ (I don’t know if that’s the correct terminology…the actual import takes about a tenth of a second, but it doesn’t START importing for a minute or two even though the file DOES appear immediately in the folder outside of Unity). So if I execute this code, everything happens correctly, except the new object is created with no texture…if I wait until the texture imports a minute or two later and execute it again, it creates the object with the correct texture, so I know that it IS working.

So, I’d like to either force the import immediately somehow, or attempt to utilize the post processor before assigning this texture to make sure it is loaded before the assign. I just have no idea how to go about this. How would I set up a post processor? I started with this, but can’t even make THIS work:

using UnityEngine;

using System.Collections;
using UnityEditor;

public class MyPostProcessor : AssetPostprocessor
	{
	public void OnPostprocessTexture()
		{
		Debug.Log("Test.");
		}
	}

What portion of the texture creation code would I then put in the post processor script (in the OnPostprocessTexture function, I would think?)? How would I then call the OnPostprocessTexture function from my main script?

I feel like I’m so close to having this working and just need a little bit of coaching through it, and my main trouble isn’t the logic behind it, but rather the specifics of classes and language of C#…but I feel like I know enough that this is right where I want to be to be pushing myself. I really appreciate everyone who is taking the time to give me a hand, and I do enjoy the challenge of learning another programming language!

Thanks again

…Solved with the one line of code I was looking for:

AssetDatabase.Refresh();

That’s all it took. I knew there had to be a function like that, but it sure took me long enough to find it! AND hours of trying to get the post processor to work right…when it was completely unnecessary! I hope this helps someone else having a similar problem.