using System; using UnityEngine; using UnityEngine.UI; using System.IO; using GadGame.Network; namespace GadGame.Scripts { [RequireComponent(typeof(RawImage))] public class LoadImageEncoded : MonoBehaviour { [SerializeField] private bool _preserveAspect; private Image _image; private Texture2D _texture; private void Awake() { _image = GetComponent(); _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); // byte[] imageBytes = Convert.FromBase64String(encodeString); _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; } } }