# Unity Tutorial: Learn how to use SVG in Unity

- Link: https://saguiitay.com/unity-tutorial-learn-how-to-use-svg-in-unity/
- Published: 2022-10-10T13:39:36+03:00
- Author: Itay

Support for SVG in Unity is quite limited, and the [documentation](https://docs.unity3d.com/Packages/com.unity.vectorgraphics@2.0/manual/index.html)
is not particularly useful. In this tutorial, I’ll guide you thru the process of
importing SVG in Unity projects, both from the Unity Editor and dynamically via 
code**.**

 1. [Importing the SVG package](https://saguiitay.com/unity-tutorial-learn-how-to-use-svg-in-unity/#importing-the-svg-package)
 2. [Importing SVG from the Unity Editor](https://saguiitay.com/unity-tutorial-learn-how-to-use-svg-in-unity/#importing-svg-from-the-unity-editor)
 3. [Importing SVG file by code](https://saguiitay.com/unity-tutorial-learn-how-to-use-svg-in-unity/#importing-svg-file-by-code)
 4.  a. [Import as a Sprite](https://saguiitay.com/unity-tutorial-learn-how-to-use-svg-in-unity/#import-as-a-sprite)
     b. [Importing as a Mesh](https://saguiitay.com/unity-tutorial-learn-how-to-use-svg-in-unity/#importing-as-a-mesh)
 5. [Recommended packages in the Asset Store](https://saguiitay.com/unity-tutorial-learn-how-to-use-svg-in-unity/#recommended-packages-in-the-asset-store)
 6. [SVG FAQ](https://saguiitay.com/unity-tutorial-learn-how-to-use-svg-in-unity/#svg-faq)

## Importing the SVG package

The first thing we need to do to be able to work with SVG files in Unity, is to 
import the **Unity Vector Graphics** package from the Package Manager. In the Unity
Editor, **Windows** -> **Package Manager**, and make sure to select “_Unity Registry_”
from the “_Packages_” dropdown:

![Unity Tutorial - Importing SVG - Import Vector Graphics package](https://i0.wp.
com/saguiitay.com/wp-content/uploads/Unity-Tutorial-Importing-SVG-Import-Vector-
Graphics-package.webp?resize=825%2C266&ssl=1 "Unity Tutorial: Learn how to use SVG
in Unity 1")

**Notice** that at the time of writing this post, the Vector Graphics package is
still marked as **Experimental**, which means that it’s not code-complete, might
contain bugs, and there’s even a chance that it will be scrapped.

If the “Vector Graphics” package is not listed, make sure you’ve enabled support
for pre-released packages in your project settings. In the Unity Editor, **Edit**-
> **Project Settings**, select the “_Package Manager_” item on the left, and check
the “_Enable Pre-release Packages_” checkbox:

![Unity Tutorial - Importing SVG- Enable pre-release packages](https://i0.wp.com/
saguiitay.com/wp-content/uploads/Unity-Tutorial-Importing-SVG-Enable-pre-release-
packages.webp?resize=800%2C260&ssl=1 "Unity Tutorial: Learn how to use SVG in Unity
2")

## Importing SVG from the Unity Editor

Importing an SVG file to Unity is extremely simple – just add the SVG file to your
assets folder and let the Vector Graphics package do its work. You can select how
the graphics will be imported based on its usage:

## Importing SVG file by code

When importing by code, we first need to retrieve the SVG content. For the sake 
of simplicity, we’ll use a `string` property.

    ```notranslate
    public string svg = "...";
    ```

To load the content of the SVG, we’ll create a `StringReader` to read our string(
we can also use a `StreamReader` if we have the SVG content as a `Stream` object).
Then, we’ll pass the reader to `SVGParser.ImportSVG` method:

    ```notranslate
    using StringReader textReader = new StringReader(svg);
    var sceneInfo = SVGParser.ImportSVG(textReader);
    ```

This gives us a `SceneInfo` object which contains all the graphic details, and which
can start working with. To render the vector graphics elements on screen, first 
get a tessellated (triangulated) version of the scene. We can tessellate our `SceneInfo`
object using the `VectorUtils.TessellateScene` method:

    ```notranslate
    var geometries = VectorUtils.TessellateScene(sceneInfo.Scene, new VectorUtils.TessellationOptions
            {
                StepDistance = 10,
                SamplingStepSize = 100,
                MaxCordDeviation = 0.5f,
                MaxTanAngleDeviation = 0.1f
            });
    ```

Now that we’re finished importing the SVG to Unity, and we have our graphics as 
a collection of geometries, we can decide how we’d like to work with them – as a
Sprite or as a Mesh. Let’s create a base script to allow us to demonstrate the various
methods:

    ```notranslate
    public class BaseSvgScript : MonoBehaviour
    {
        [Multiline()]
        public string svg = "";
        public int pixelsPerUnit;
        public bool flipYAxis;

        protected List<VectorUtils.Geometry> GetGeometries()
        {
            using var textReader = new StringReader(svg);
            var sceneInfo = SVGParser.ImportSVG(textReader);

            return VectorUtils.TessellateScene(sceneInfo.Scene, new VectorUtils.TessellationOptions
            {
                StepDistance = 10,
                SamplingStepSize = 100,
                MaxCordDeviation = 0.5f,
                MaxTanAngleDeviation = 0.1f
            });
        }
    }
    ```

### Import as a Sprite

When importing as a Sprite, we can just call the `VectorUtils.BuildSprite` method
to build our `Sprite` object from our geometries. We can then use a SpriteRenderer
component to render the graphics. Here’s our sample script, our GameObject components,
and the expected result:

    ```notranslate
    [RequireComponent(typeof(SpriteRenderer))]
    public class SpriteSvgScript : BaseSvgScript
    {
        void Start()
        {
            List<VectorUtils.Geometry> geometries = GetGeometries();

            var sprite = VectorUtils.BuildSprite(geometries, pixelsPerUnit, VectorUtils.Alignment.Center, Vector2.zero, 128, flipYAxis);
            GetComponent<SpriteRenderer>().sprite = sprite;
        }
    }
    ```

![Unity Tutorial: Learn how to use SVG in Unity 1](https://i0.wp.com/www.saguiitay.
com/wp-content/uploads/Unity-Tutorial-Importing-SVG-Import-as-Sprite-Components.
webp?ssl=1 "Unity Tutorial: Learn how to use SVG in Unity 3")

![Unity Tutorial - Importing SVG - Import as Sprite Results](https://i1.wp.com/www.
saguiitay.com/wp-content/uploads/Unity-Tutorial-Importing-SVG-Import-as-Sprite-Results.
png?ssl=1 "Unity Tutorial: Learn how to use SVG in Unity 4")

### Importing as a Mesh

An alternative to importing the SVG as a Sprite, is to load is as a Mesh. For that,
we’ll need a `MeshFilter` and a `MeshRenderer`. We can then use the `VectorUtils.
FillMesh` method to create the mesh from the geometries. Once again, here’s our 
sample script and GameObject components (the results are expected to be the same):

    ```notranslate
    [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    public class MeshSvgScript : BaseSvgScript
    {
        void Start()
        {
            List<VectorUtils.Geometry> geometries = GetGeometries();

            var meshFilter = GetComponent<MeshFilter>();
            Mesh mesh = meshFilter.mesh;

            VectorUtils.FillMesh(mesh, geometries, pixelsPerUnit, flipYAxis);

            var meshRenderer = GetComponent<MeshRenderer>();
            meshRenderer.materials = new Material[] { new Material(Shader.Find("UI/Default")) };
        }
    }
    ```

![Unity Tutorial - Importing SVG - Import as Sprite Components](https://i0.wp.com/
saguiitay.com/wp-content/uploads/Unity-Tutorial-Importing-SVG-Import-as-Sprite-Components-
1.webp?resize=423%2C568&ssl=1 "Unity Tutorial: Learn how to use SVG in Unity 5")

You can download the source code for this tutorial from [https://gist.github.com/saguiitay/fdc86365ffb4ee7aa9c7b5e27afc9707](https://gist.github.com/saguiitay/fdc86365ffb4ee7aa9c7b5e27afc9707)

## Recommended packages in the Asset Store

While you can use the above code in your project, you might want to use one of the
assets below from the Unity Asset Store to speed-up your work, and enjoy enhanced
features:

## SVG FAQ

### What is SVG?

SVG stands for Scalable Vector Graphics. It is a vector graphics format intended
primarily for the Web, though it is getting wider deployment on a variety of devices.
Vector graphics are a way of describing an image not as rows and columns of pixels,
but as a set of instructions on how to create the image. A raster image, such as
a bitmap, PNG, JPEG, or GIF, is merely a series of unconnected pixels (or colored
dots), while a vector image represents various parts of the image as discrete objects,
so it allows for better interactivity. When you zoom in on a raster image, it becomes
blocky and fuzzy (or “pixelated”), but when you zoom in on a vector image, it remains
crisp and clear at any resolution.
Also, vector graphics can often have a smaller
file size (especially when compressed) than the equivalent raster image. Note that
for photographic images, however, raster images are called for; vector graphics 
are better for line art, drawings, charts and graphs, maps, and text.

### Why use vector images instead of bitmap images?

Because vector images are made up of points and lines, they can be scaled without
any loss of image quality. This is because your computer is using the information
in each geometric primitive to draw the image, instead of displaying a set pixel
map. When bitmap images are scaled up, they become fuzzy, but when vector images
are scaled up they remain crisp.

### How are vector images constructed?

A vector image is generated from discrete points and vector data attached to those
points. These vectors, in combination with their relationship to other points and
their vectors, define a path. The path follows a curve defined by the magnitude 
and direction of the vectors of its two endpoints. This path is given a stroke that
acts as a border or a fill, or both.

### Does Unity Support SVG

Unity has native support for SVG (Scalable Vector Graphics) files via the **Unity
Vector Graphics** package available from the Unity Package Manager. It can be used
to import SVG to Unity as sprites, or as vectors.

### _Related_
