The example explained in this chapter is contained in the plane scene. The example shows a simple plane object with an attached texture, generated from an SVG file. The example has been realized following these steps:
Create a new Unity project, AmanithSVG can be used for 2D and 3D projects; in this case we select the 2D setup.
Select the Game view, and make sure that “Scale” slider is completely on the left (i.e.
scale = 1x); switch back to the Scene view.
|Set a unitary scene scale|
Because the project is a new one, it is required to copy AmanithSVG binding for Unity (Editor, Plugins and Scripts folders) inside the new project’s
Assets folder; so the native AmanithSVG libraries and its C# interface will be available for the project.
|The project layout after copying all needed files|
Set a full white
Ambient Color light (menu:
Lighting Settings), in order to display SVG files with their original colors.
|Ensure a pure white ambient light|
Create a plane object (menu:
3D Object →
Plane) and make sure that the main camera is pointing it. To do so, place the plane object at
(0, 0, 0) and the camera object at
(0, 10, 0) with a rotation of
(90, 0, -180).
|The plane gameobject|
Copy an SVG file (e.g. girl.svg within the
Assets directory. If needed, such file will be renamed automatically by SVGRenamerImporter script, adding an additional
.txt extension (if not already included), so that Unity can recognize it as a TextAsset.
Select the plane object and attach the “SVG Texture Behaviour” script (menu
Scripts subsection). This script is really simple, it shows a possible usage of AmanithSVG library. In the detail the script:
Creates a drawing surface with the same dimensions
Creates and load the SVG document
Renders the SVG document to the drawing surface
Creates the texture with the specified dimensions
Copies the drawing surface pixels to the texture
Destroys SVG document and drawing surface
Returns the created texture
Drag&drop the girl SVG file from the Project window to the
SVG File property field of the script. Let other properties unchanged to their default values (
512 x 512 pixels texture dimensions, white background clear color).
Click play and see the result.
|This is the result after pressing Play|
AmanithSVG binding for Unity also includes another script called
SVGTextureAtlasBehaviour; in order to test it, stop the current execution and perform the following steps:
Select the plane and remove the attached
SVGTextureAtlasBehaviour script component: this script allows you to specify four different SVG files to be rendered on a single texture
Drag&drop four SVG files from the Project window to
1,2,3,4 property fields of the script.
As before, click play and see the result in the Game window.
|Four SVG files rendered on a single texture|