Advertisement Jump to content
Sign in to follow this  

using a graphical icon as a button

This topic is 1741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a .png on the start scene of my unity project that i want to use as a button to start the game. How do i make it happen? i know how to load scenes but am confused on how to register a mouse click on an object

Share this post

Link to post
Share on other sites

You might also want to do something on mouse over as well (I think it's OnMouseOver, but I can't recall), as really flat buttons that don't look like buttons and don't change context on mouse over can be confusing.  People don't know they can click on them.


(As a quick example, when replying to a topic, mouse over the various button images, you'll see the background of the button darken slightly, and if you mouse over Post, the mouse icon changes)

Share this post

Link to post
Share on other sites



OnMouseOver works but is called every frame that the mouse is over the object.  There is also OnMouseEnter and OnMouseExit to simulate Hover events (each only called one time when the action is observed).  You could do something like this in C#.

using UnityEnging;
using System.Collections;

public class ButtonController : MonoBehaviour{

	// set via the inspector
	public Texture2D hoverTexture = null;
	// cached components
	private GUITexture myGUITexture = null;
	private Texture2D defaultTexture = null;
	// SceneManager is a singleton - see Scripting Reference for
	// FindObjectOfType for a hint on how to implement
	private SceneManager scene = null;	
	void Start(){
		scene = SceneManager.Instance;
		myGUITexture = GetComponent<GUITexture>();
		defaultTexture = (Texture2D) myGUITexture.texture;
	void OnMouseEnter(){
		myGUITexture.texture = hoverTexture;
	void OnMouseExit(){
		myGUITexture.texture = defaultTexture;
	void OnMouseUpAsButton(){
		// SceneManager defines a funciton that takes a string and depending on 
		// which button is pressed it takes a different action.
		// this way all of your button logifc is in one component and you can have
		// a single button script instead of each button needing it's own script logic.

Also, you should note that you are not limited to using GuiTextures/GuiText as buttons.  This is considered the "Legacy" GUI system by Unity.  Check the references for GUI.  I must admit that I do happen to like the legacy system more for things just like simple image buttons and labels, but if you need more complex controls (sliders, dialogboxes, input controls, etc) then you should use the GUI class.  Check the scripting reference.


Believe it or not, you don't need to use either of those if you don't want to.  Any object in the game that has a collider can override the functions in the script above so you can use 3DText, a character, etc as a button.   Instead of changing the texture you could offset it towards the camera to make it bigger, you could have it enter an animation sequence, you could have it change pose, etc.  The script is almost the same other than that.


Hope this helps.

Edited by DeafManNoEars

Share this post

Link to post
Share on other sites

DeafManNoEars is totally correct, MouseEnter/Exit is the way to go, thanks for pointing that out.  And I like his fun ideas of using 3d models as gui.  I might have to play around with that at some point.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!