Hi
I am trying to make the gui box appear wherever I click as a sort of contextual menu, but I can’t seem to position it properly. Seems like the x axis is following the mouse position, but the y axis is reversed. Also, ideally I don’t want the menu to follow the mouse, but just appear where I clicked. Is it possible?
GUI.Box ( Rect ((Input.mousePosition.x),(Input.mousePosition.y),100,100), "CONTEXTUAL MENU");
Thanks
Okay, so the first part was easy, just added Screen.height - to the y axis.
Now the GUI box follows the cursor around the screen. But I need it to just be created and then stay in the initial position. Any ideas?
GUI.Box ( Rect ((Input.mousePosition.x),(Screen.height - Input.mousePosition.y),100,100), "CONTEXTUAL MENU");
You just need to use a boolean variable to note the fact that the mouse has been clicked once:-
var alreadyClicked = false;
var box: Rect;
function OnGUI() {
if (!alreadyClicked Input.GetMouseButtonDown(0)) {
alreadyClicked = true;
// Set the position of the box...
}
GUI.Box(box, ...);
}
Hi Andeee
Thank you, but just as you posted I managed to make it work! Haven’t tried your method, I’m sure it’s more elegant. But for anyone who is interested, this is how I have the code at the moment. Cheers
var target: Transform;
var MenuActive:boolean = false;
var posx: int;
var posy: int;
function OnGUI (){
if (MenuActive == true){
if (MenuActive == true){
// Make a background box
GUI.Box (Rect((posx),(Screen.height - posy),110,110), "CONTEXT MENU");
//First button
if (GUI.Button (Rect((posx+10),(Screen.height - posy+30),90,20), "SOMETHING")) {
}
//Second button
if (GUI.Button (Rect((posx+10),(Screen.height - posy+55),90,20), "SOMETHING")) {
}
//Third button
if (GUI.Button (Rect((posx+10),(Screen.height - posy+80),90,20), "CLOSE")) {
MenuActive=!MenuActive;
}
}
}
}
function Update () {
if (Input.GetKey(KeyCode.LeftAlt))
{
}
else
{
if (Input.GetMouseButtonDown(1))
{
posx = Input.mousePosition.x;
posy = Input.mousePosition.y;
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
if (hit.transform == target) {
MenuActive=!MenuActive;
}
}
}
}
}
Hi, I’ve made another little code, this time for a window to appear where I click the right mouse button. It works, but after I create it, I would like it to be draggable. At the moment it is not. Is it because I’m explicitly setting the rect position? If so, how do I override it when a drag takes place? Thanks
var target: Transform;
var posx: int;
var posy: int;
var windowRect: Rect;
var ObjectName : String;
var visible;
function OnGUI()
{
if( visible )
{
windowRect = Rect (posx, Screen.height - posy, 110, 110);
mywindow = GUI.Window (0, windowRect, WindowFunction, ObjectName);
}
}
function Update () {
if (Input.GetKey(KeyCode.LeftAlt))
{
}
else
{
if (Input.GetMouseButtonDown(1))
{
posx = Input.mousePosition.x;
posy = Input.mousePosition.y;
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
if (hit.transform == target) {
visible=!visible;
}
}
}
}
}
function WindowFunction (windowID : int) {
if (GUI.Button(Rect(90,0,14,14),"", "toggle")) {
visible = false;
}
GUI.DragWindow ();
}
GetMouseButtonDown only returns true on the frame where the mouse button was first pressed down. You can use GetMouseButton to test if the button is currently down or up.
Hi Andee
I tried it, but the window is still not dragable. Besides if I do “GetMouseButton” the creation of the window is a bit jittery. “GetMouseButtonUp” and “GetMouseButtonDown” both work well, it’s only the drag of the window that doesn’t work.
Thanks
EDIT
Sorry, forgot to mention that I’ve updated the code a little and removed reference to the rect. Seemed a little pointless to me. But the drag doesn’t work with it or without it either. Below is new code.
var target: Transform;
var posx: int;
var posy: int;
var ObjectName: String;
var visible;
function OnGUI()
{
if( visible )
{
mywindow = GUI.Window (0, (Rect (posx, Screen.height - posy, 110, 120)), WindowFunction, ObjectName);
}
}
function Update () {
if (Input.GetKey(KeyCode.LeftAlt))
{
}
else
{
if (Input.GetMouseButtonDown(1))
{
posx = Input.mousePosition.x;
posy = Input.mousePosition.y;
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
if (hit.transform == target) {
visible=!visible;
}
}
}
}
}
function WindowFunction (windowID : int) {
//On-Off button
if (GUI.Button(Rect(90,0,14,14),"", "toggle")) {
visible = false;
}
GUI.DragWindow ();
}
The Window() function itself actually updates the Rect. Hence by passing a newly initialized Rect to the function every frame, you reset any drag operation that may have occurred during the rendering of the previous frame. You need to pass the Rect the function gives you without altering it.
Try something like this:
function Start()
{
myRect = Rect(posx, Screen.height - posy, 110, 120);
}
function OnGUI()
{
if( visible )
{
myRect = GUI.Window (0, myRect, WindowFunction, ObjectName);
}
}
About the y-axis: UnityGUI uses a y-coordinate system that increases as you go down while Input, GUIElements and 3D objects an inverted y-axis (increases as you go up). You’ll have to do the conversion yourself.
It seems an awkward design choice to me, too.
Hi Mat
myRect = GUI.Window (0, myRect, WindowFunction, ObjectName);
The “myRect” in the brackets returns an error. Do I need to set it as a var?
I tried to mess around with the code, but the drag still doesn’t work. At best it registers clicks outside of the “hit area” after the initial window is created, and positions it anywhere on the screen. But it doesn’t drag, just reappears on the next click.
Any more ideas? Desperate to solve it.[/code]
Of course! It was only an example…
myRect is meant to be a variable of yours that you must declare and initialize, it was obvious to me but sorry for not having been clear enough.
The bottom line is: for the drag to work properly, at frame n+1 pass the rectangle the window gave you at frame n. Do not modify it except at initialization.