Jump to content

  • Log In with Google      Sign In   
  • Create Account

using a graphical icon as a button


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 welly_59   Members   -  Reputation: 230

Like
0Likes
Like

Posted 07 April 2014 - 10:51 AM

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



Sponsor:

#2 welly_59   Members   -  Reputation: 230

Like
1Likes
Like

Posted 07 April 2014 - 10:54 AM

got it, attach a script with 'OnMouseDown' function



#3 ferrous   Members   -  Reputation: 2146

Like
1Likes
Like

Posted 07 April 2014 - 01:51 PM

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)



#4 NotYourAverageUser   Members   -  Reputation: 500

Like
2Likes
Like

Posted 08 April 2014 - 07:46 AM


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.


Edited by DeafManNoEars, 08 April 2014 - 08:02 AM.


#5 ferrous   Members   -  Reputation: 2146

Like
0Likes
Like

Posted 11 April 2014 - 09:35 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS