How can I save a texture and edit its importer options with TextureImporter in the same pass?

I’m attempting to create a drag-and-drop editor script for a certain filetype that will ultimately make a sprite renderer prefab with a dynamically created material and textures with specific import settings.


Where I’m stuck is between creating the image and setting the import settings. I can create the texture and save it to a png just fine, and I can load from a png and edit its import settings using TextureImporter.GetAtPath(), but I can’t do both in the same pass. When the texture is created and saved in the same script, TextureImporter.GetAtPath() returns null and crashes the script. However if the texture existed since the beginning of the script, everything works just fine.
What seems to me to be happening is that there is some kind of importer event in Unity’s editor loop that is populating some metadata the TextureImporter needs, but I can’t seem to find the right call to make this happen.


Code is as follows:

private static string type = "LBM.json";

    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        foreach (var path in importedAssets)
        {
            if (path.LastIndexOf('.') - 3 == path.ToLower().IndexOf(type.ToLower()))
            {
                ImportLBM(path);
            }
        }
    }

    private static void ImportLBM(string assetPath)
    {
        //get folder location
        string assetPathLower = assetPath.ToLower();
        string folder = assetPathLower.Substring(0,assetPathLower.IndexOf(type.ToLower()) - 1);

        LBM_Asset lbm = JsonUtility.FromJson<LBM_Asset>(File.ReadAllText(assetPathLower));
        if(lbm.width == 0 || lbm.height == 0 || lbm.pixels.Count != lbm.width*lbm.height)
        {
            throw new System.Exception("Bad LBM file!");
        }

        //Get the name
        string filename = lbm.filename.Substring(0, lbm.filename.LastIndexOf('.')-1);

        //---------------
        //Make the indexed image
        var indexedTexture = new Texture2D(lbm.width, lbm.height, TextureFormat.ARGB32, false);
        for (int i = 0; i < lbm.height; i++)
        {
            for (int j = 0; j < lbm.width; j++)
            {
                int index = i * lbm.width + j ;
                indexedTexture.SetPixel(j, lbm.height -i-1, new Color(lbm.pixels[index] / 255f, lbm.pixels[index] / 255f, lbm.pixels[index] / 255f, 1.0f));
            }
        }
        indexedTexture.filterMode = FilterMode.Point;
        indexedTexture.Apply();
        byte[] Ibytes = indexedTexture.EncodeToPNG();
        File.WriteAllBytes(folder + "_indices.png", Ibytes); //.png saves just fine
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        AssetDatabase.ImportAsset(folder + "_indices.png", ImportAssetOptions.ForceUpdate);
        //!!Missing something here?
        
        //Set indices importer info
        TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(folder + "_indices.png"); //returns null
        importer.spritePixelsPerUnit = 1; //null pointer crash
        importer.textureCompression = TextureImporterCompression.Uncompressed;
        importer.filterMode = FilterMode.Point;
        importer.SaveAndReimport();

I’ve also tried using AssetDatabase.CreateAsset(indexedTexture, folder + “_indices.asset”); with the .asset extension and the .png extension. The former creates an correct-looking asset file whose import properties I can’t edit, and the latter makes a corrupted png but doesn’t crash and allows me to modify settings in the same pass.


As you can see this is done by hooking into the OnPostprocessAllAssets because I’m currently overriding json import behavior (lbm.json), though a more complete version of this will probably override the extension .lbm itself, from which I’m generating an lbm.json file.

Cheers!

So as far as I can tell, AssetDatabase.Refresh(); won’t work as long as your function is being called from OnPostprocessAllAssets().

The solution is to call ImportLBM() or whatever your import funciton is called via EditorApplication.update CallBackFunction. Since I need the path preserved between calls, I need a static reservoir for those paths:

    private static EditorApplication.CallbackFunction _importDelegate;
    private static List<string> _assetsMarkedForImport = new List<string>();

Inside OnPostprocessAllAssets, I set the delegate up and add some new paths if new paths to import are found:

if (_importDelegate == null)
                {
                    _importDelegate = new EditorApplication.CallbackFunction(ImportLBM);
                }
                _assetsMarkedForImport.Add(path);
                EditorApplication.update = Delegate.Combine(EditorApplication.update, _importDelegate) as EditorApplication.CallbackFunction;

Finally, the inside of the actual import function is wrapped up thus:

EditorApplication.update = Delegate.Remove(EditorApplication.update, _importDelegate as EditorApplication.CallbackFunction) as EditorApplication.CallbackFunction;

        try
        {

            foreach (string assetPath in _assetsMarkedForImport)
            {
                    <import code here>
             }
        catch (Exception e)
        {
            Debug.Log(e);
        }
        finally
        {
            _assetsMarkedForImport.Clear();
        }