Skip to content

Instantly share code, notes, and snippets.

@m-biernat
Created February 24, 2024 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-biernat/412d1e46aea74990aa90430fe57ab04d to your computer and use it in GitHub Desktop.
Save m-biernat/412d1e46aea74990aa90430fe57ab04d to your computer and use it in GitHub Desktop.
A class to encode/decode Texture2D and Base64 formats (Unity).
// 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