- Home /
Scrollview with Input.MousePosition?
How do you make the Input.MousePosition fit with a GUI.ScrollView? Right now the drag/drop system does not work in my game, I found the problem, but I cannot seem to find a solution. I just can't. The problem is that the Input.MousePosition does not work with GUI group things(even worse with scrollviews). So how would I fix this? P.S: InputManagerClass.mousePosition is just a getter with Input.MousePosition.x, and Input.MousePosition.y - Screen.height.
Thanks in advance!
Code:
private void RenderInventory(int id){
//GUI.BeginGroup (inventoryWindow);
ScrollPosition = GUI.BeginScrollView (new Rect (inventoryWindow.x + 0,inventoryWindow.y + 19,395,225), ScrollPosition, new Rect (0,0,370,1161));//Scrollfield.
int cnt = 0;//Location of the current item in loops.
for(int y = 0; y < inventoryCols; y++){//Y
for(int x = 0; x < inventoryRows; x++){//X
if(cnt < myItems.Count){//If the item exists.
if(myItems[cnt].itname != null && myItems[cnt].itname.Length > 1){//If not null/bugged.
Rect rect = new Rect(3+x * 46.5f,y * 46.5f,45,45);
GUI.Label(rect,"" + myItems[cnt].itname,inventoryButton);
if (rect.Contains(InputManagerClass.mousePosition))
{
Debug.Log("Contains MOUSE!!!");
if (Event.current.type == EventType.MouseDown)
{
OnCurrentDragStart(myItems[cnt]);
}
}
//if(Event.current.type == EventType.MouseUp && currentDrag != null){
// OnCurrentDragDrop(myItems[cnt]);
//}
}
//If bugged/null.
else{
//If this is null/bugged, Remove.
myItems.Remove(myItems[cnt]);
}
}
//If does not exist.
else{
GUI.Label(new Rect(3+x * 46.5f,y * 46.5f,45,45),"",inventoryButton);
}
//Increase the current loop item.
cnt++;
}
}
GUI.EndScrollView ();//End the scrollfield
//GUI.EndGroup ();
}
Answer by zharik86 · Mar 18, 2014 at 06:55 PM
If you want to use coordinates of a mouse in the OnGUI function, you shall use Event.current.mousePosition, instead of Input.mousePosition. But don't forget that Event.current will be bound on coordinates to BeginGroup or to Scroll. I.e. for computation of a real line item it will be necessary to add still coordinates of the upper left corner of the container if, of course, it is.
Your answer
Follow this Question
Related Questions
unity 5.2 Input.GetTouch(0).position incorrect? 3 Answers
GUI.TextField - Android: Unfocusing with click instead of Ok-button causes critical error 0 Answers
v4.6 UI Element's Positioning Incorrect 1 Answer
Coordinates of Input.GetTouch(0).position vs Screen coordinates? 1 Answer
Input.mousePosition returns coordinates as integers. 1 Answer