0

I'm learning unity and I have successfully instantiated a new game object into my scene (a cube)

Now I'm playing with the Canvas UI and I'm trying to download an asset bundle with images and show them on the UI, but I can't find examples on Google

Can someone post me an example on how to load an image into the Canvas from an asset bundle?

Thanks!!!

Chico3001
  • 1,853
  • 1
  • 22
  • 43

2 Answers2

2

There are a few things to do to get this to work: You'll need to Create a UnityEngine.UI.Image (sprites don't work on Canvas on their own). Assign the Image.Sprite property by grabbing the Texture2D out of the bundle, and if needed, you can create a sprite using the Sprite.Create() method which takes a Texture2D.

In other words, an Image has a Sprite, and a Sprite is made from a Texture2D.

Texture2D tex = myAssetBundle.LoadAsset<Texture2D>("myTex");    
Sprite mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
raybarrera
  • 160
  • 1
  • 10
0

Select the image from the asset bundle.Set the texture type to Sprite(2D and UI).Then simply drag and drop your image into the canvas.

SynAck
  • 427
  • 5
  • 19