- Home /
Input.mouse position on GUI Textures
I have 2 gui textures.
According to screen width and height i kept it in gui.
One for joystick and another for shooter.
Now touching on shooter joystick moves to that specific portion.
i used rect.Contains.
if (rect.Contains(Input.mousePosition)){
shootBool = true;
print("shooter gui Inside");
alert.text="shooter gui Inside";
}
Not working properly for me. Think space coordinates are different from gui coordinates. How can fix this problem.
Answer by robertbu · Mar 18, 2013 at 05:35 AM
I'm guessing you are using Input.mousePosition. This variable is in Screen coordinates. GUI lives in GUI coordinates. You can convert between the two by:
Pos.y = Screen.height - Pos.y;
But a better solution is to use GUI events. See Event.mousePosition.
Don't bother with that, just use Event.current.mousePosition in OnGUI. Then it will be correct. Don't use Input.mousePosition in OnGUI.
@Eric5h5: i used
void Start () {
xx = Screen.width - Screen.width/12;
yy = Screen.height - Screen.height/8;
lb = Screen.width/10;
rect = new Rect(-xx/2, -yy/2, lb, lb);
shooter.pixelInset = rect;
shooter.enabled = false;
}
void OnGUI(){
if(characterScript.playbool){
shooter.enabled = true;
}
if (rect.Contains(Event.current.mousePosition)){
shootBool = true;
print("shoot");
alert.text="shoot";
}
}
Still its not working. can someone help me to solve this issue.
Do a debug.Log() on your 'rect'. It is way off the screen, so there is no way for the rect.Contains() to return true.
In Start(), just after you assign the 'rect', do:
Debug.Log(rect);
When I execute your code on my machine I get the following values for the rect:
(x:-625.50, y:-591.00, width:136.00, height:136.00)
GUI coordinates begin in the upper left corner of the screen at 0,0 and go to screen width and screen height in the lower right corner. So your 'rect' as specified in this code is not visible.
As for your texture being visible, you don't have anything in the code above that displays a texture, so there is no way for me to check how you setup or use your rect in your GUI.DrawTexture() call.
If the code above is what you are talking about for generating for different devices, then it is flawed, and you need to change it. The 'rect' you specify above is off the screen and therefore 1) will not display a texture, and 2) can never be hit by a mouse event. Here is some more code that works. I've specified a rectangle in the lower left of the device.
public class Bug6 : $$anonymous$$onoBehaviour {
float xx, yy, lb;
Rect rect;
public Texture tex;
void Start () {
xx = Screen.width/12;
yy = Screen.height - Screen.height/8 - Screen.width/10;
lb = Screen.width/10;
rect = new Rect(xx, yy, lb, lb);
Debug.Log(rect);
}
void OnGUI() {
GUI.DrawTexture (rect, tex);
if (rect.Contains(Event.current.mousePosition)) {
Debug.Log ("Point in Rect");
}
}
}
Answer by programmrzinc · Mar 18, 2013 at 11:54 AM
Since it is GUI,like a GUITexture, You can attach a script to the GUITexture and call OnMouseDown, or OnMouseOver.
var hovertex : Texture2D;
var normaltex : Texture2D;
function OnMouseDown(){
print("Pressed");
}
function OnMouseOver(){
print("HOvering");
}
this is not only just button. its joystick and shooter.