using a graphical icon as a button

Started by
3 comments, last by ferrous 10 years ago

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

Advertisement

got it, attach a script with 'OnMouseDown' function

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)


OnMouseOver

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.
		scene.OnButtonSelect(gameObject.name);
	}
}

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.

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.

This topic is closed to new replies.

Advertisement