- Home /
Issues with setting TextureImporter isReadable in script
I am using the free version of unity and have a c# script that I wrote to auto save a displayed texture every N seconds. I am using TextureIMporter to set the assets isReadable to true so that I can call texture.EncodeToPNG(), see code segment:
string path = AssetDatabase.GetAssetPath(tex);
TextureImporter texImporter = AssetImporter.GetAtPath(path) as TextureImporter;
texImporter.isReadable = true;
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
if(texImporter.isReadable == true){
File.WriteAllBytes(Application.dataPath + "/Resources/backdrop.png",
tex.EncodeToPNG());
Debug.Log("This should have saved");
}else{
Debug.Log("FALSE");
}
The issue is that this does not always work? For testing I have this in a coroutine that is called every N seconds, but isReadable is not always set as true?
Suppose the texture (tex in above code) is the same then it works every other time. The first time it sets true the second false the third true etc.
Any insight would be helpful? Why shouldn't work every time?
Problem with texImporter.isReadable if already true. In this case when AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); is run it is actually set from true to false, even though I have assigned it to true?
This seems like a bug to me, unless you understand why it does that.