Created
June 20, 2019 01:34
-
-
Save MartinGonzalez/9b200090340326ed4cf5fb1259cec555 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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