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:
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!
