x


How To Get Current Mouse Position and have a GUI Box on right clicked

hey i was thinking about the mouse position

 var e : Event = Event.current;
 var xMousePosition = e.mousePosition.x;
 var yMousePosition = e.mousePosition.y;

        function OnGUI () {
            if (Input.GetMouseButton(1))
            GUI.Box (Rect (xMousePosition,yMousePosition,120,120), "");
        }

i want when i right click it gets the current mouse position at which right clicked and show the GUI box there ..can any one help me

more ▼

asked Aug 04 '11 at 04:10 PM

Blitzerine (suspended)
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You've almost got it:

function OnGUI () {
    if (Input.GetMouseButton(1))
        GUI.Box (Rect (Input.mousePosition.x,Input.mousePosition.y,120,120), "");
}

But the box will be open only while you're holding the right button down - and it will follow the mouse pointer if it moves. If you want the box to appear and remain openned, do the following:

var boxOpen: boolean = false;
var boxPos: Vector3;

function Update () {
    if (Input.GetMouseButtonDown(1)){
        boxPos.x = Input.mousePosition.x;
        // Y runs up to down in GUI, so let's invert it
        boxPos.y = Screen.height - Input.mousePosition.y;
        boxOpen = true;
    }
}
    
function OnGUI () {
    if (boxOpen)
        GUI.Box (Rect (boxPos.x,boxPos.y,120,120), "");
}

You must set boxOpen to false to close it.

EDITED: Y runs upside down in GUI system, so it must be inverted (0 is Screen.height)

more ▼

answered Aug 04 '11 at 04:22 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

var boxOpen: boolean = false;
var boxPos: Vector3;

function Update () {
    if (Input.GetMouseButtonDown(1)){
        boxPos = Input.mousePosition;
        boxOpen = true;
    }
}

function OnGUI () {
    if (boxOpen)
        GUI.Box (Rect (boxPos.x,boxPos.y,120,120), "");
}

this all i wanted but its open the gui at correct X position ..Y position is notcorect i mean it should open where i clicked the Xpos is correct

Aug 05 '11 at 07:22 AM Blitzerine

@Blitzerine, you're right! I forgot this: Y is inverted in the GUI system - 0 is at the top, while the bottom is Screen.height. I've edited my answer to correct this (only changed the line where boxPos was set to mousePosition)

Aug 05 '11 at 10:15 AM aldonaletto

Yay thanks now it opens at correct position

Aug 05 '11 at 11:07 AM Blitzerine
(comments are locked)
10|3000 characters needed characters left

and to close with Left click i made this for others ^^

var boxOpen: boolean = false;
var boxPos: Vector3;

function Update () {
    if (Input.GetMouseButtonDown(1)){
        boxPos.x = Input.mousePosition.x;
        boxPos.y = Screen.height - Input.mousePosition.y;
        boxOpen = true;
    }
    if (Input.GetMouseButtonDown(0)){
        boxOpen = false;
    }
}

function OnGUI () {
    if (boxOpen)
        GUI.Box (Rect (boxPos.x,boxPos.y,140,180), "");
}
more ▼

answered Aug 05 '11 at 12:16 PM

Blitzerine (suspended)

This Question is COMPLETE NOW ^^

Aug 05 '11 at 12:17 PM Blitzerine
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3694
x984
x886
x161
x74

asked: Aug 04 '11 at 04:10 PM

Seen: 2734 times

Last Updated: Aug 05 '11 at 12:19 PM