如何使用C#在Unity中读取SVG文件样式属性

时间:2019-03-11 18:17:11

标签: c# unity3d svg

我需要将svg文件中对象的填充色写入变量。我正在使用下面的代码来获取节点ID:

FrameLayout playArea;
playArea = v.findViewById(R.id.exoplayer_frame);
playArea.addView(customPlayerView);

问题是我知道如何更改填充颜色,但不知道如何从文件中读取现有填充颜色。我尝试使用file_path = System.IO.Path.Combine(Application.dataPath + "/StreamingAssets", this.gameObject.name + ".svg"); if (File.Exists(file_path)) { file = File.ReadAllText(file_path); } scene = SVGParser.ImportSVG(new StringReader(file)); foreach (KeyValuePair<string, SceneNode> param in scene.NodeIDs) { names.Add(param.Key); } ,但是这种东西不存在。这是我的SVG文件路径的示例:

scene.NodeIDs[id].Shapes[0].Fill.Color

有什么建议怎么做?

1 个答案:

答案 0 :(得分:0)

您可以将Fill投射到SolidFill并像这样评估其Color属性

var currentColor = ((SolidFill)scene.NodeIDs[id].Shapes[0].Fill).Color;

示例:

public class SvgExample : MonoBehaviour
{
    public Color color;

    private void Start()
    {
        var file = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 100 100\">" +
                        "<path id =\"path5635\" " +
                            "d =\"m 256,342.5 c -2.988,-4.353 -4.224,-16.005 -4.224,-16.005 0,0 -12.516,12.865 -22.585,26.184 " +
                            "-10.07,13.318 -18.961,23.69 -32.018,32.656 -13.06,8.966 -55.413,44.449 -58.141,59.132 0,0 -2.532,8.533 " +
                            "6.468,11.533 9,3 22,-3 31.5,-9.5 9.5,-6.5 31.5,-22 38.374,-33.1 5.995,-9.682 20.626,-38.9 27.126,-49.4 " +
                            "5.843,-9.439 13.5,-21.5 13.5,-21.5 z\" " +
                            "style=\"fill:#aa0e0e\" />" +
                   "</svg>";

        var scene = SVGParser.ImportSVG(new StringReader(file));

        color = ((SolidFill)scene.NodeIDs["path5635"].Shapes[0].Fill).Color;
    }
}

enter image description here