How to set canvas image to a Texture2D created in a script at runtime?
I'm making a game with a map that only needs part of it shown at any time, so I have code that crops it to return a new Texture2D. This is done in its own static C# class. I have got this far but actually setting a canvas to have this cropped image is proving impossible for me.
All tutorials show dragging and dropping sprite assets into the image component but none show how to do this in code.
Answer by victorbisaev · Jan 29, 2018 at 12:28 AM
Here is the property to set a texture to a sprite in code: https://docs.unity3d.com/ScriptReference/UI.Image-sprite.html To assign "sprite" to an Image first you need to create the sprite and assign your cropped Texture2D to it probably using this method: https://docs.unity3d.com/ScriptReference/Sprite.Create.html
Your answer
Follow this Question
Related Questions
How to move a UI Image in the z axis? 2D 0 Answers
How to create a zoomable canvas image in Unity for mobile without scaling the main camera? 0 Answers
How to convert from RenderTexture to Texture2D to EncodeToPNG? 0 Answers
Programmatically "Stamping" an image on a Texture2D. 0 Answers
Problem rendering an image in a canvas 0 Answers