- Home /
AssetDatabase.ImportAsset fails on generated textures
I'm trying to generate a number of PNG icons from 3d objects in Unity. Generating and saving the images works fine, but I can't seem to finish the process of saving the new assets to the database. When I attempt to use AssetDatabase.ImportAsset()
, it fails with the following error:
Could not create asset from Assets/Artwork/Resources/Icons/Insertions/Carnation - Light Blue.png: Texture could not be created.
UnityEditor.AssetDatabase:ImportAsset(String, ImportAssetOptions)
Editor.IconGenerator:SaveTextureToPNG(String, Texture2D) (at Assets/Code/Editor/IconGenerator.cs:95)
Editor.IconGenerator:GenerateInsertionIcons() (at Assets/Code/Editor/IconGenerator.cs:38)
Editor.IconGenerator:GenerateAllIcons() (at Assets/Code/Editor/IconGenerator.cs:17)
That path is directly from a previous reference to the asset, so I don't understand why it fails. When I check in the inspector, the new assets exist, and the settings are actually changed, but they haven't been applied.
Here's a snippet of the larger script:
TextureImporterSettings settings = new TextureImporterSettings();
settings.alphaIsTransparency = true;
settings.alphaSource = TextureImporterAlphaSource.FromInput;
settings.sRGBTexture = true;
textureImporter.SetTextureSettings(settings);
EditorUtility.SetDirty(textureImporter);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
Did you resolve this? Having the same issue. I suspect an incomplete assignment of the texture importer settings as I did not have any problems when editing the texture importer directly.
Answer by AtheMathmo · Jun 04, 2021 at 06:37 PM
@Forberg is right! You need to initialize the rest of the settings in the TextureImportSettings
object. The easiest way I found to do this was to first use: textureImporter.ReadTextureSettings(settings);
, then you can make your changes, and set it again.
Your answer
Follow this Question
Related Questions
is it possible to have a global/static variable in the AssetPostprocessor script 1 Answer
How can I change the default max size of a texture from an editor script (in import settings)? 1 Answer
Failed to import survival shooter 0 Answers
White glow around character instead of black 0 Answers
Editor script to slice sprites 4 Answers