- Home /
Other
What are the best practices to draw images from script?
I have a simple 2D game and in a click of a button I want to draw a random image. Is there a simple way to draw sprites from script? I don't want to use OnGUI (with DrawTexture) and I'd rather not create prefabs from all of my images.
Answer by robertbu · Nov 03, 2014 at 09:05 PM
This question is too general as it stands. The 'best' way to display the images will depend on a number of factors including how you want to handle changing resolutions and if the images have the same aspect ratio. Primary ways to display an image are:
OnGUI.DrawTexture()
GUITexture()
Using a Quad with a material and a texture
For your set of images, put them in an array and initialize the array either through drag and drop or Resources.LoadAll(). Select the image and asign using Random.Range().
As I've written, I don't want to use OnGUI because from what I understand it's bad performance wise.
I'm new to unity and I just wanted to know when to use each way.
Thank you
I don't want to use OnGUI because from what I understand it's bad performance wise.
This is not necessarily true. The way most beginners display textures, the three methods are roughly equal. You get performance improvements when you start using a texture atlas. Note that discussion questions should be posted on Unity Forums.
Follow this Question
Related Questions
Make bones visible when playing 2 Answers
Getting Error "DrawGUITexture: Texture is Null" when using Graphics.DrawTexture in OnGUI() 2 Answers
Mapping 2d texture onto another 2d texture 0 Answers
Drawing new mesh not working 1 Answer
Draw a line to a new pontential character position/target 1 Answer