Created
February 24, 2024 16:19
-
-
Save m-biernat/412d1e46aea74990aa90430fe57ab04d to your computer and use it in GitHub Desktop.
A class to encode/decode Texture2D and Base64 formats (Unity).
This file contains 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
// This is a class to encode/decode Texture2D and Base64 formats (Unity). | |
// Copyright (c) 2023 Michał Biernat @m-biernat, licensed under the MIT License. | |
using System; | |
using UnityEngine; | |
public static class TextureEncoder | |
{ | |
public static string Texture2DToBase64(Texture2D texture) | |
{ | |
var imageData = texture.EncodeToPNG(); | |
return Convert.ToBase64String(imageData, Base64FormattingOptions.InsertLineBreaks); | |
} | |
public static Texture2D Base64ToTexture2D(string encodedData) | |
{ | |
var imageData = Convert.FromBase64String(encodedData); | |
Texture2D texture = new Texture2D(0, 0, TextureFormat.ARGB32, false, true); | |
texture.LoadImage(imageData); | |
return texture; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment