- Home /
Elements in draggable window do not remain fixed [SOLVED]
I have the following code implemented in the draggable window function. The idea is that a player can open their inventory and move around the textures in various "slots" that are defined int he function. However, when the window is shifted the slots move along the y-axis. The x-axis seems to be fine and remains fixed. The code block is for the first inventory slot:
if(GUI.Button(new Rect(myBagWindowSize.width*0.97f, 2, 20, 15), " "))
{
checkMyBag = false;
}
GUI.DrawTexture(new Rect(0, 0, myBagWindowSize.width, myBagWindowSize.height), bagTexture);
#region main bag display
float sizex = myBagWindowSize.width*(64f/1024f);
float sizey = myBagWindowSize.height*(64f/640f);
Vector2 invertedMousePos = GUIUtility.ScreenToGUIPoint(Input.mousePosition);
invertedMousePos.y = myBagWindowSize.height - invertedMousePos.y;
float pos0x = myBagWindowSize.width*(64f/1024f);
float pos0y = myBagWindowSize.height*(352f/640f);
Rect slot0 = new Rect(pos0x, pos0y, sizex, sizey);
if(main_bag[0] != "")
{
#region ...code
GUI.DrawTexture(slot0, _Icons.GetIcon(main_bag[0]));
if(!global_item_selected && slot0.Contains(invertedMousePos))
{
main_bag_item_hover[0] = true;
Cursor.SetCursor(grab_ico, new Vector2(grab_ico.width/2, grab_ico.height/2), CursorMode.Auto);
}
else if(!global_item_selected && !slot0.Contains(invertedMousePos) && main_bag_item_hover[0])
{
main_bag_item_hover[0] = false;
Cursor.SetCursor(main_cursor, new Vector2(0,0), CursorMode.Auto);
}
if(!global_item_selected && slot0.Contains(invertedMousePos) && Input.GetMouseButtonUp(0))
{
whileSelecting = true;
Cursor.SetCursor(_Icons.GetIcon(main_bag[0]), new Vector2(sizex/2, sizey/2), CursorMode.Auto);
main_bag[0] = "";
global_index_from = 0;
global_item_selected = true;
}
#endregion
}
else if(global_item_selected && slot0.Contains(invertedMousePos) && Input.GetMouseButtonUp(0) && !whileSelecting)
{
#region ...code
global_item_selected = false;
item_replaced_bagType = "main";
global_index_to = 0;
if(main_bag[0] != "")
{
Cursor.SetCursor(_Icons.GetIcon(main_bag[0]), new Vector2(sizex/2, sizey/2), CursorMode.Auto);
}
item_replaced = true;
#endregion
}
Answer by jaja1 · Dec 03, 2014 at 05:55 PM
Solved:
Answer is to use Event.current.mousePosition in place of Input.mousePosition. Using that I can avoid the lines : Vector2 invertedMousePos = GUIUtility.ScreenToGUIPoint(Input.mousePosition); invertedMousePos.y = myBagWindowSize.height - invertedMousePos.y;
and simply use: Vector2 invertedMousePos = Event.current.mousePosition;
Answer by unimechanic · Dec 03, 2014 at 02:45 PM
Make its coordinates relative to the window, currently you only do that for the X axis:
new Rect(myBagWindowSize.width*0.97f, 2, 20, 15)
Got excited for a second there...sorry the button is actually to close the window and I'm not having problems with that. The "slot" rect is what moves along the y-axis when the window is shifted.
Your answer
Follow this Question
Related Questions
HOW TO: Sliding GUI Window? 1 Answer
GUI.Window function not called anymore 1 Answer
using c# script to open/close GUI window 1 Answer
Distribute terrain in zones 3 Answers