Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Happeloy · Feb 20, 2020 at 10:08 AM · svg files

Read SVG from disk, using the new Vector Graphics package.

Hi!

So, what I'm trying to do is to read an svg-file from disk with the new Vector Graphics package. Any one know how this might be done? Reading the raw bytes from disk is no problem.

Had it been a .png, I would have used the method LoadBytes. There is no such method in the SVGImage.

I can't find anything useful in the documentation.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Arkade · Apr 14, 2020 at 06:55 PM

Depends whether you're doing it at Edit time (in the Editor) or at runtime.

Editor use

In the Editor, you can simply drag the SVG into the project and under/within it in the Project window will appear 1 sprite that you can use. Selecting the SVG file in the Project window will show you details in the inspector for tweaking how it's converted etc. I don't believe anyone (at this time) knows how to access this at runtime though so for me the more useful is...

Runtime use

For runtime use, you need the text of the SVG. While you can go through normal C# I/O operations, the simplest way to achieve this is just adding a ".bytes" to the end of the SVG's filename outside Unity (Explorer oin Windows etc). Then you can reference it as a TextAsset which has a .text property which puls the whole text in. Here's some code I'm using successfully (hopefully all of it copied and pasted as successfully ;-) ) Oh btw this can only be done at runtime -- trying to do at other times causes an error relating to Sprite.OverrideGeometry() -- I'm still trying to figure this out.

 public class SVGInScene : MonoBehaviour {
     public TextAsset svgAsset;
     public SVGTesselationOptions tesselationOptions;
 
     public void Start() {
         initSVG();
     }
 
     private void initSVG() {
         // Dynamically import the SVG data, and tessellate the resulting vector scene.
         var sceneInfo = loadSVG();
         var geoms = VectorUtils.TessellateScene(sceneInfo.Scene, tesselationOptions.getAsTessellationOptions());
 
         // Build a sprite with the tessellated geometry.
         var sprite = VectorUtils.BuildSprite(geoms, 10.0f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);
         sprite.name = svgAsset.name;
         var spriteRenderer = gameObject.AddComponent<SpriteRenderer>(); // or get existing one
         spriteRenderer.sprite = sprite;
     }
 
     private SVGParser.SceneInfo loadSVG() {
         using (var reader = new StringReader(svgAsset.text)) { // not strictly needed but in case switch later.
             return SVGParser.ImportSVG(reader);
         }
     }
 }

HTH :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Happeloy · Apr 14, 2020 at 08:21 PM 0
Share

Thanks! This issue has since been resolved in my project in another way (not using SVGs), but this is a great response anyway.

avatar image jason-vreps · Nov 20, 2020 at 07:31 AM 0
Share

This is brilliant! Worked great for me. Thank you!

avatar image JonPQ · Feb 01, 2021 at 07:57 PM 0
Share

Thanks, very useful, though how do you reference 'SVGTesselationOptions' or getAsTessellationOptions please? I don't see those in unity scripting APi nor do my dev tools. is it now VectorUtils.TessellationOptions ? and what default values are recommended ? $$anonymous$$y objects come in at huge size compared to importing with unity.... and the gradients are not applied correctly. the import settings seem critical. how to automatically read in UNity's default settings ?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

199 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SVG to Mesh renderer with specalised anti-aliasing 0 Answers

Use vector lines made in illustrator as moving path 0 Answers

Error Unknown char: - UnitySVG 0 Answers

Unity 2020 Vector Graphics Package 1 Answer

Morph SVG in Unity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges