- Home /
Question by
groothewanderer · Mar 02 at 08:08 PM ·
svg files
Add SVG Image on codebehind
When I add an SVGImage to a scene and set the Source SVG Image on Inspector it works fine. But, I can not do this on code-behind. What is the proper way?
GameObject go = new GameObject("SVG Image");
RectTransform rectTransform = go.AddComponent<RectTransform>();
rectTransform.sizeDelta = new Vector2(100f, 100f);
go.AddComponent<SVGImage>();
Sprite si = go.GetComponent<SVGImage>().sprite;
si = Resources.Load<Sprite>("Images/Flags/ad.svg"); // How to set Source SVG Image ???
Comment
FYI: Unity doesnt have "codebehinds". Just source files.
var svgImage = go.GetComponent<SVGImage>();
svgImage.sprite = Resources.Load("Images/Flags/ad.svg");
Does this work?
Not working
error CS0266: Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Sprite'. An explicit conversion exists (are you missing a cast?)
Your answer
![](https://koobas.hobune.stream/wayback/20220613053720im_/https://answers.unity.com/themes/thub/images/avi.jpg)