I needed to load some basic simple polygon shapes into Unity at runtime and get it there in form of the meshes. There are already some resources on Unity Community Wiki on import of svg files, but they regard to rendering those files to textures. I was wondering whether I could rex make the scripts by making them just to generate the sequences of points instead of rendering the whole images, but they seemed too complicated and to make things worse I got discouraged, because they were written in C#. On the other hand svg polygon specification seemed very simple, so my final choice was to write it from sctratch. The next step was to triangulate the polygons i.e. to fill them with triangles. I didn’t intend to go beyond the simple basic shapes so I have chosen the ear clipping method of triangulation which supports supports concave polygons, but not polygons with holes, or multiple polygons combined. This method is followed by the Triangulator script from Unify Community Wiki.
I have learned soon that a simple polygon shapes of identical appearance can take forms of various svg elements like “polygon”, “polyline”, “rect” (as rectangle) and “path”. From the above the “path” is most complicated, because can contain a number of internal commands.
All the elements can have attributes like name (“id”), color, opacity, transform, style, etc. I have selected to support in my simple importer only name, color, opacity and transform on rectangle element only. The color and opacity of the element transforms into properties of material in Unity.
You can download the sources from here:
I have tested it with files saved from Inkscape, Coreldraw and Illustrator. Please let me know if you get any errors or have some suggestions for improvement. The download contains the complete Unity3d project folder.
The tree image used in this example come from here: http://www.allfreevectors.com
Because my simple importer doesn’t support multiple/combined paths, the closed shape letters in the lettering example had to be opened by cutting them.
Recently I have added some interaction to this example, mouse clicking at the front side ot the graphic toggles the fill/outline display.