- Home /
Assiging an Image to a Button
I've been looking for hours for an example that explains how to do something I thought would be simply, adding a image file to be used as a button. The examples I've found talk about using Resources.Load("wheat.png") which I put inside a resources folder which I've put everywhere just in case I didn't have in located in the right folder inside my project. e.g. like:
private Texture wheatButton; void Start() { wheatButton = (Texture)Resources.Load("wheat.png"); } void OnGui(){ if (GUI.Button(new Rect(button.x, button.y, 100, 20), wheatButton)); }
Nothing gets displayed and there is no examples of C# code to show how one goes about loading a image apart from saying, use inspector. But I can't see what object or thing you can create to then access in the inspector to link a image, or any examples showing how to do it. I'm sure someone out there has needed to load an image as a texture to use in scripting code before and shouldn't be that complex, any help would be appreciated.
You have to use OnGUI method ins$$anonymous$$d of OnGui (GUI must be uppercase).
Yeah I typed that code in to cut out some of the other misc stuff, it is OnGUI(), it does work I had it as text before I changed it to an image. It now just comes up as a blank little box. Its just how does on add an images as a texture, I know I can do the button part, its loading that as a texture so I can use it which I'm having issues with
Answer by ArkaneX · Sep 05, 2013 at 01:52 PM
Please load the image using
wheatButton = (Texture)Resources.Load("wheat");
without .png extension. If you have Resources folder inside Assets folder, then this should work.
Who would have thought it would be that simple. Thanks for that, images at least shows and gives me something to work with. $$anonymous$$uch appreciated.
The funny thing was the forums I read with examples of Resources.Load had .png after it so thought it needed that. Fail
Your answer
Follow this Question
Related Questions
Empty spaces in buttons are detected by mouse 2 Answers
Making texture cover whole button 1 Answer
GuiTexture (Touch button) unfollow camera! 1 Answer
GUI Style Issue-- Texture not changing 2 Answers