Skip to content

Instantly share code, notes, and snippets.

@MartinGonzalez
Created June 20, 2019 01:34
Show Gist options
  • Save MartinGonzalez/9b200090340326ed4cf5fb1259cec555 to your computer and use it in GitHub Desktop.
Save MartinGonzalez/9b200090340326ed4cf5fb1259cec555 to your computer and use it in GitHub Desktop.
public static class Textures {
public static string AsBase64(Texture2D texture) {
SetTextureImporterFormat(texture, true);
var textureCopy = new Texture2D(texture.width, texture.height);
var colors = texture.GetPixels();
textureCopy.SetPixels(colors);
return Convert.ToBase64String(textureCopy.EncodeToPNG());
}
private static void SetTextureImporterFormat(Texture2D texture, bool isReadable) {
if (null == texture) return;
var assetPath = AssetDatabase.GetAssetPath(texture);
var tImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (tImporter == null) return;
tImporter.textureType = TextureImporterType.Default;
tImporter.isReadable = isReadable;
AssetDatabase.ImportAsset(assetPath);
AssetDatabase.Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment