- Home /
texture click detection is off
im trying to setup a texture to be clickable. Before anyone says it, i know that there are button for this. But I'm using texture.
my code looks like this:
#pragma strict
var background: Texture;
var tweak: Texture;
var level: Texture;
var jam: Texture;
var gear: Texture;
var toTheMax: Texture;
var gearButton: Rect;
var tweakButton: Rect;
function Start ()
{
gearButton = Rect(20,Screen.height/2,Screen.width/6.5,Screen.height/8);
tweakButton = Rect(Screen.width/6.5+40,Screen.height/2,Screen.width/6.5,Screen.height/8);
}
function Update ()
{
if(Input.GetMouseButtonUp)
{
if(gearButton.Contains(Input.mousePosition))
{
Debug.Log("GEAR");
}
if(tweakButton.Contains(Input.mousePosition))
{
Debug.Log("TWEAK");
}
}
}
function FixedUpdate()
{
}
function OnGUI()
{
GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),background);
GUI.DrawTexture(Rect(20,20,Screen.width/3,Screen.height/3),toTheMax);
GUI.DrawTexture(Rect(20,Screen.height/2,Screen.width/6.5,Screen.height/8),gear);
GUI.DrawTexture(Rect(Screen.width/6.5+40,Screen.height/2,Screen.width/6.5,Screen.height/8),tweak);
}
with this code i have 2 errors that i cant figure out: 1: the Rects that I'm assigning the 2 public Rect variables to in start seems to be above the actual texture and i cant figure out why, because I'm using the exact same dimensions.
2: when im hovering my mouse over those 2 invisible Rects it does the debug.log even when im not touching the mouse button at all. (i also tried it with GetMouseButtonDown and it still runs the line regardless of the mouse button state)
Answer by robertbu · May 27, 2013 at 06:33 PM
GUI drawing uses GUI coordinates which start in the upper left corner. Screen coordinates (which are returned by Input.mousePosition) start in the lower right. You can convert between the two by using the GUIUtility class. But a better solution is to use GUI events and detect the click inside of OnGUI():
#pragma strict
var background: Texture;
var tweak: Texture;
var level: Texture;
var jam: Texture;
var gear: Texture;
var toTheMax: Texture;
var gearButton: Rect;
var tweakButton: Rect;
function Start ()
{
gearButton = Rect(20,Screen.height/2,Screen.width/6.5,Screen.height/8);
tweakButton = Rect(Screen.width/6.5+40,Screen.height/2,Screen.width/6.5,Screen.height/8);
}
function OnGUI()
{
var e = Event.current;
if (e.type == EventType.MouseUp) {
if(gearButton.Contains(e.mousePosition)) {
Debug.Log("GEAR");
}
if(tweakButton.Contains(e.mousePosition)) {
Debug.Log("TWEAK");
}
}
GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),background);
GUI.DrawTexture(Rect(20,20,Screen.width/3,Screen.height/3),toTheMax);
GUI.DrawTexture(Rect(20,Screen.height/2,Screen.width/6.5,Screen.height/8),gear);
GUI.DrawTexture(Rect(Screen.width/6.5+40,Screen.height/2,Screen.width/6.5,Screen.height/8),tweak);
}
Your answer
Follow this Question
Related Questions
2 Bugs in my game: mouse control takes some time to load and Menu not working. 0 Answers
Button being pressed but an other button gets the effect. 0 Answers
Overlapping GUI Button priority 3 Answers
How can I stop the middle mouse button from zooming out when I single click it inside the editor? 2 Answers