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

37 lines
1.2 KiB
C#
Raw Normal View History

2024-06-19 20:25:06 -07:00
using System;
using UnityEngine;
using UnityEngine.UI;
2024-06-21 01:20:01 -07:00
using System.IO;
using GadGame.Network;
2024-06-19 20:25:06 -07:00
namespace GadGame.Scripts
{
2024-06-21 01:20:01 -07:00
[RequireComponent(typeof(RawImage))]
2024-06-19 20:25:06 -07:00
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);
}
2024-06-21 01:20:01 -07:00
public void LoadImage(string streamingData)
2024-06-19 20:25:06 -07:00
{
//Decode the Base64 string to a byte array
2024-06-21 01:20:01 -07:00
byte[] imageBytes = UdpSocket.Instance.DataReceived.GenerateImageSuccess ? File.ReadAllBytes(streamingData) : Convert.FromBase64String(streamingData);
// byte[] imageBytes = Convert.FromBase64String(encodeString);
2024-06-19 20:25:06 -07:00
_texture.LoadImage(imageBytes); // Automatically resizes the texture dimensions
2024-06-21 01:20:01 -07:00
// _texture.Apply();
2024-06-19 20:25:06 -07:00
var sprite = Sprite.Create(_texture, new Rect(0, 0, _texture.width, _texture.height), new Vector2(0.5f, 0.5f), 100);
_image.sprite = sprite;
}
}
}