1

I'm building a custom asset importer that reads asset settings from a JSON file then applies those settings to the asset in the Unity project. I need to apply those settings on a range of assets.

I have created an editor window with a button click to test my code before implementing it into the main project.

The button click event:

private void ButtonClick()
    {
        string path = "Assets\\Audio";

        var assetPaths = HelperFunctions.FindAssetsByType<AudioClip>(path, false);

        for (int i = 0; i < assetPaths.Count; i++)
        {
            AudioClip audioClip = AudioClip.Create("", 128, 2, 8000, false);

            Preset preset = new Preset(audioClip);

            var importer = AssetImporter.GetAtPath(assetPaths[i]);

            bool result = preset.ApplyTo(importer);

            Debug.Log(result);
        }
    }

As you can see I have created a blank audio clip in which I am using to model the preset. I am then loading the AssetImporter for the targeted asset using the path returned by FindAssetByType which returns the path of my asset, for example: "Assets/Audio/myaudiofile.mp3"

The debug.log returns False, ie a failure to apply.

I am also not able to access certain properties of the AssetImporter programmatically, as see below:

AssetImporter variable

Why is the AssetImporter failing to apply my new asset preset, and why am I not able to access the properties detailed in the screenshot in code?

'AssetImporter' does not contain a definition for 'defaultSampleSettings'

I have also tried modifying FindAssetsByType to return the object returned by the AssetDatabase as follows:

T currentAsset = AssetDatabase.LoadAssetAtPath<T>(currentPath);

This results in the Debug.Log returning true, but the preset is never applied. You can see this code commented out below

Here is how I search for assets:

public static /*List<T>*/ List<string> FindAssetsByType<T>(string path) where T : UnityEngine.Object
    {
        List<string> assets = new List<string>();
        //List<T> assets = new List<T>();

        string searchString = string.Format("t:{0}", typeof(T));
        searchString = searchString.Replace("UnityEngine.", "");

        string[] guids = AssetDatabase.FindAssets(searchString, new string[] { path });

        List<string> paths = AssetGuidToPath(guids);

        foreach (var currentPath in paths)
        {
            T currentAsset = AssetDatabase.LoadAssetAtPath<T>(currentPath);

            //assets.Add(currentAsset);
            assets.Add(currentPath);
        }

        return assets;
    }

Surely there needs to be a way to accomplish this? Any help would be appreciated!

1 Answer 1

1

Well, the AssetImporter class does not have a property called "defaultSampleSettings" since the AssetImporter class is just the base class for all asset importers. As you can see in your screenshot the actual class is "UnityEngine.AudioImporter". So you just need to downcast your asset importer in order to access this property.

Don't forget to call SaveAndReimport after you applied some changes to the importer.

Note that applying custom settings to asset importers is usually done with an AssetPostProcessor. Though I think your approach should work as well if you call SaveAndReimport

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.