Cen Yao
Cen Yao

Reputation: 3

Changing sprite image to GameObject

How do I convert or change sprite image to a GameObject. I want to use 3D model instead of image.

Here's the code:

public Sprite icon;

if (imageIcon != null)
{
    var iconSprite = attribute == null ? null : attribute.icon;
    imageIcon.gameObject.SetActive(iconSprite != null);
    imageIcon.sprite = iconSprite;

Upvotes: 0

Views: 1311

Answers (1)

Bin4ry
Bin4ry

Reputation: 732

A GameObject is a container for components. Sprite is an object which is applicable to a component's property, like the sprite property on the image component. Or in your case, the texture property in a material (conversion from sprite to texture is required). Thus, if you want to apply a sprite image to a gameObject, simply apply the sprite to the texture property of the attached material.

Code sample to apply a sprite onto the current material

GetComponent().material.mainTexture = Resources.Load<Sprite>("Sprites/MySprite").texture;

In the editor simply drag and drop the sprite onto the gameObject.

Upvotes: 1

Related Questions