- Home /
Issues with mouse hovering over GUI element [SOLVED]
I am trying to implement some code that changes the cursor texture when the mouse hovers over a particular portion of the player's inventory. Also, when the player clicks the item with that space, the cursor changes to the item's texture. If none of these conditions are met the cursor simply defaults to the main cursor texture. Here's what I have so far:
if(main_bag[0] != "")
{
float posx = myBagWindowSize.width*(64f/1024f);
float posy = myBagWindowSize.height*(352f/640f);
float sizex = myBagWindowSize.width*(64f/1024f);
float sizey = myBagWindowSize.height*(64f/640f);
Rect slot = new Rect(posx, posy, sizex, sizey);
GUI.DrawTexture(slot, _Icons.GetIcon(main_bag[0]));
#region this does not work
if(slot.Contains(Input.mousePosition))
{
Cursor.SetCursor(grab_ico, new Vector2(grab_ico.width/2, grab_ico.height/2), CursorMode.Auto);
}
else Cursor.SetCursor(main_cursor, new Vector2(0,0), CursorMode.Auto);
if(slot.Contains(Input.mousePosition) && Input.GetMouseButtonUp(0))
{
main_bag_item_click = true;
}
if(main_bag_item_click)
{
Cursor.SetCursor(_Icons.GetIcon(main_bag[0]), new Vector2(sizex/2, sizey/2), CursorMode.Auto);
}
#endregion
}
What happens is that only the default cursor texture is shown. The code block with the region is the issue. Trying to do this in the Update function proves some what tedious because the rectangle I am using only seems to 'work' when in the window's function. Otherwise, the position of the rectangle changes if I move the window. Can anyone provide a possible solution?
EDIT: Use Event.current.mousePosition in place of Input.mousePosition
Call it from inside the window, that way position will stay local
It actually is being called within the window function. Sorry I should have mentioned this earlier. Here is the full code essentially:
void myBagWindow(int id)
{
if(GUI.Button(new Rect(myBagWindowSize.width*0.97f, 2, 20, 15), " "))
{
check$$anonymous$$yBag = false;
}
GUI.DrawTexture(new Rect(0, 0, myBagWindowSize.width, myBagWindowSize.height), bagTexture);
#region main bag display
if(main_bag[0] != "")
{
float posx = myBagWindowSize.width*(64f/1024f);
float posy = myBagWindowSize.height*(352f/640f);
float sizex = myBagWindowSize.width*(64f/1024f);
float sizey = myBagWindowSize.height*(64f/640f);
Rect slot = new Rect(posx, posy, sizex, sizey);
GUI.DrawTexture(slot, _Icons.GetIcon(main_bag[0]));
#region this does not work
if(slot.Contains(Input.mousePosition))
{
Cursor.SetCursor(grab_ico, new Vector2(grab_ico.width/2, grab_ico.height/2), Cursor$$anonymous$$ode.Auto);
}
else Cursor.SetCursor(main_cursor, new Vector2(0,0), Cursor$$anonymous$$ode.Auto);
if(slot.Contains(Input.mousePosition) && Input.Get$$anonymous$$ouseButtonUp(0))
{
main_bag_item_click = true;
}
if(main_bag_item_click)
{
Cursor.SetCursor(_Icons.GetIcon(main_bag[0]), new Vector2(sizex/2, sizey/2), Cursor$$anonymous$$ode.Auto);
}
#endregion
}
if(main_bag[1] != "")
{
float posx = myBagWindowSize.width*(128f/1024f);
float posy = myBagWindowSize.height*(352f/640f);
float sizex = myBagWindowSize.width*(64f/1024f);
float sizey = myBagWindowSize.height*(64f/640f);
GUI.DrawTexture(new Rect(posx, posy, sizex, sizey), _Icons.GetIcon(main_bag[1]));
}
}