How can you change the vector image icon programmatically

+1 vote
asked Mar 29, 2016 in Help by anonymous

1 Answer

0 votes
answered Mar 30, 2016 by admin (31,720 points)
selected Aug 22, 2016 by admin
 
Best answer

Hello,

It should be as easy as doing this:

// With the icon name
m_VectorImage.SetImage(MaterialIconHelper.GetIcon("exact_name_of_the_icon"));

// Using the enum
m_VectorImage.SetImage(vectorImageData = MaterialIconHelper.GetIcon(MaterialIconEnum.CONTACT_PHONE));

// Random icon
m_VectorImage.SetImage(vectorImageData = MaterialIconHelper.GetRandomIcon());

I've just added that in the Vector Images example scene (#10), it will be available with the next update ;)

------------------------------------------------------------------------------------------------------------

If you want to set an icon from a different set than the Material Design Icons, just use the method below (here's an example using the FontAwesome Icon Font):

m_VectorImage.SetImage(GetIconFromIconFont("FontAwesome", "gift"));
private ImageData GetIconFromIconFont(string fontName, string iconName)
{
    VectorImageSet iconSet = VectorImageManager.GetIconSet(fontName);
    Glyph glyph = iconSet.iconGlyphList.Where(x => x.name.ToLower().Equals(iconName.ToLower())).FirstOrDefault();
    if (glyph == null)
    {
        Debug.LogError("Could not find an icon with the name: " + iconName + " inside the " + fontName + " icon font");
        return null;
    }

    Font font = VectorImageManager.GetIconFont(fontName);
    return new ImageData(new VectorImageData(glyph, font));
}

~ Yohan

commented Mar 31, 2016 by anonymous
I want from A different set, not the main package but the weather icons package, How can I do that?
commented Apr 4, 2016 by admin (31,720 points)
Hey, I've just updated the answer with more info ;)
commented Aug 26, 2016 by hroland (380 points)
The Debug.LogError line is incorrect. You log the GameObject's name, not the iconName :)
commented Aug 26, 2016 by admin (31,720 points)
Thx for that!

I'm editing the answer.
Welcome to MaterialUI support! Ask us anything :)
...