smart-interactive-display/Assets/GadGame/Scripts/LoadImageEncoded.cs

33 lines
956 B
C#

using System;
using UnityEngine;
using UnityEngine.UI;
namespace GadGame.Scripts
{
[RequireComponent(typeof(Image))]
public class LoadImageEncoded : MonoBehaviour
{
[SerializeField] private bool _preserveAspect;
private Image _image;
private Texture2D _texture;
private void Awake()
{
_image = GetComponent<Image>();
_image.preserveAspect = _preserveAspect;
_texture = new Texture2D(1, 1);
}
public void LoadImage(string encodeString)
{
//Decode the Base64 string to a byte array
byte[] imageBytes = Convert.FromBase64String(encodeString);
_texture.LoadImage(imageBytes); // Automatically resizes the texture dimensions
var sprite = Sprite.Create(_texture, new Rect(0, 0, _texture.width, _texture.height), new Vector2(0.5f, 0.5f), 100);
_image.sprite = sprite;
}
}
}