Hi.
I have an editor script that generates .PNG images based on set parameters.
I then have a Scriptable Object as a data-container for my generated .PNG’s, that object accepts sprites.
I want my script to automatically generate the Scriptable Object after generating the PNG’s.
Here is the thing, i need to change the PNG’s import settings to sprite for the Scriptable object to accept them.
I found a way to change import settings through script. But that only works as Editor extension, if i try to do the same not using Selection to find the PNG’s get a null reference exception.
Snippets:
How i save the Texture:
foreach (RawSpriteData sprite in rawSprites)
{
CheckFolder(Application.dataPath + basePath + spriteName + "/" + postFix + "/"); //Function that creates the folder if it doesn't exist
File.WriteAllBytes(Application.dataPath + basePath + spriteName + "/" + postFix + "/" + sprite.Name + ".PNG", sprite.EncodeToPNG);
AssetDatabase.SaveAssets(); //In hopes forcing a save would remove the null reference exceprion
}
How i assign the textures to the scriptable object:
List<Sprite> textures = new List<Sprite>();
foreach (RawSpriteData sprite in rawSprites)
{
Sprite s = (Sprite)AssetDatabase.LoadAssetAtPath("Assets" + basePath + spriteName + "/" + postFix + "/" + sprite.Name + ".PNG", typeof(Sprite));
textures.Add(s);
}
scrObject.sprites = textures.ToArray();
How I try to change Import settings: (not working)
public static void ChangeTextureTypeSettings(Texture2D asset, TextureImporterType newType)
{
string path = AssetDatabase.GetAssetPath(asset);
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
//Debug.Log("path: " + path);
textureImporter.textureType = newType;
AssetDatabase.ImportAsset(path);
}
The editor extension for changing import settings that i know works:
static void SelectedChangeTextureTypeSettings(TextureImporterType newType)
{
Object[] textures = Selection.GetFiltered<Texture2D>(SelectionMode.Assets);
Selection.objects = new Object[0];
foreach (Texture2D texture in textures)
{
string path = AssetDatabase.GetAssetPath(texture);
Debug.Log("path: " + path);
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
textureImporter.textureType = newType;
AssetDatabase.ImportAsset(path);
}
}