As the question states I have a byte array for an SVG image that I want to get onto a Sprite Renderer.
But I have had no luck achieving this so I’m hoping someone has something to point me in the right direction.
Have a look over here. I haven’t used any svg images in Unity yet, so i can not verify that it works.
edit
Here’s my test example using the Unity logo from wikipedia
using System.Collections.Generic;
using UnityEngine;
using Unity.VectorGraphics;
using System.IO;
public class SVGTest : MonoBehaviour
{
public TextAsset svgAsset;
public VectorUtils.TessellationOptions tesselationOptions;
[SerializeField] private SpriteRenderer spriteRenderer;
private void Start()
{
tesselationOptions.MaxCordDeviation = float.MaxValue;
tesselationOptions.MaxTanAngleDeviation = float.MaxValue;
tesselationOptions.StepDistance = 1f;
tesselationOptions.SamplingStepSize = 0.1f;
LoadSVG(svgAsset.text);
}
private void LoadSVG(string svgBytes)
{
using (StringReader reader = new StringReader(svgBytes))
{
SVGParser.SceneInfo sceneInfo = SVGParser.ImportSVG(reader);
List<VectorUtils.Geometry> geoms = VectorUtils.TessellateScene(sceneInfo.Scene, tesselationOptions);
// Build a sprite with the tessellated geometry.
Sprite sprite = VectorUtils.BuildSprite(geoms, 100.0f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);
sprite.name = "SVGimage";
spriteRenderer.sprite = sprite;
}
}
}
I renamed the svg file and appended .bytes so I was able to assign the file as text asset to my script. the image loaded well as expected.
Note that the image is not base64 encoded as it is just a plain svg image which is just plain XML text. You could use base64 encoding on top, but it would just make the file larger for no reason and requires you to decode it first before passing it to the svg parser.