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

34 lines
1.1 KiB
C#

using System;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using GadGame.Network;
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 streamingData)
{
//Decode the Base64 string to a byte array
byte[] imageBytes = UdpSocket.Instance.DataReceived.GenerateImageSuccess ? File.ReadAllBytes(streamingData) : Convert.FromBase64String(streamingData);
_texture.LoadImage(imageBytes); // Automatically resizes the texture dimensions
// _texture.Apply();
var sprite = Sprite.Create(_texture, new Rect(0, 0, _texture.width, _texture.height), new Vector2(0.5f, 0.5f), 100);
_image.sprite = sprite;
}
}
}