Add image util
This commit is contained in:
33
Assets/Scripts/ImageUtil.cs
Normal file
33
Assets/Scripts/ImageUtil.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
|
||||||
|
using System;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
|
||||||
|
public class ImageUtil
|
||||||
|
{
|
||||||
|
public void RenderFromBase64(string base64, Image targetImage)
|
||||||
|
{
|
||||||
|
byte[] imageData = Convert.FromBase64String(base64);
|
||||||
|
Texture2D tex = new(2, 2, TextureFormat.ARGB32, false);
|
||||||
|
if (!tex.LoadImage(imageData)) return;
|
||||||
|
|
||||||
|
tex.filterMode = FilterMode.Point;
|
||||||
|
tex.Apply(false, false);
|
||||||
|
|
||||||
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
||||||
|
targetImage.sprite = sprite;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RenderFromBase64(string base64, SpriteRenderer targetImage)
|
||||||
|
{
|
||||||
|
byte[] imageData = Convert.FromBase64String(base64);
|
||||||
|
Texture2D tex = new(2, 2, TextureFormat.ARGB32, false);
|
||||||
|
if (!tex.LoadImage(imageData)) return;
|
||||||
|
|
||||||
|
tex.filterMode = FilterMode.Point;
|
||||||
|
tex.Apply(false, false);
|
||||||
|
|
||||||
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
||||||
|
targetImage.sprite = sprite;
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Assets/Scripts/ImageUtil.cs.meta
Normal file
2
Assets/Scripts/ImageUtil.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: f608a791036150d12852f910d572dbc4
|
||||||
Reference in New Issue
Block a user