- Home /
Inventory smoothing
Hi, i wrote this drag and drop code for my inventory, it's working but not smooth. I'm stuck because I have no idea how to put this party in the Update() function.
The the "for" make the game laggy when it's in a onGUI function.
//Drag/Drop
var column:int = 6;
for(x = 0; x < column; x++){
for(y = 0; y < 3; y++){
box[x+(y*column)] = Rect(-5 + (x * 36),5 + (y * 36), 36, 36);
GUI.Box(box[x+y*column], Slot[x+(y*column)]);
if(Event.current.type == EventType.MouseDown){
if(box[x+(y*column)].Contains(Event.current.mousePosition)){
UI.toggle = true;
dragging = true;
first_Number = x+(y*column);
first_Name = inv.Name[first_Number];
first_Quantity = inv.Quantity[first_Number];
}
}
if(box[x+(y*column)].Contains(Event.current.mousePosition)){
over_Number = x+(y*column);
}
if(Event.current.type == EventType.MouseUp){
dragging = false;
inv.Name[first_Number] = inv.Name[over_Number];
inv.Quantity[first_Number] = inv.Quantity[over_Number];
Change();
}
}
}
Here is the [[Full Code]][1]
If you have any idea, thank you.
[1]: http://hastebin.com/pakonicifo.avrasm
Don't dubble post please. If your question is in moderation, wait for it to get moderated, no need to repost.
The problem of OnGUI is that it is called multiple times per frame. So it rapidly gets very heavy to process.
If you really have to use OnGUI inGame (which is not at all recommended) You'd be better of checking all this in an update function and just use the OnGUI for the actual interface rendering. (just boolean checks, no mouse checks and events, computing, array manipulations, etc.)
The problem is that "Contains(Input.mousePosition)" seems to be not working:
if(box[x+(y*column)].Contains(Input.mousePosition)){
over_Number = x+(y*column);
Debug.Log("test");
}
And i don't how to retrieve the "for" internal variable to draw the Box in my GUI :
GUI.Box(box[x+y*column], Slot[x+(y*column)]);
It should work, but if I'm not mistaken mousePosition and OnGUI drawing have an inverted use of the Y coordinate.
So maybe ins$$anonymous$$d of checking for Input.mousePosition, you should try
new Vector3(Input.mousePosition.x,Screen.height-Input.mousePosition.y,Input.mousePosition.z)
I've resolved my problem, i don't need to do this. I've reduced the "for" number in the Upddate function and it's smooth now. Thanks for your help :)
Your answer
Follow this Question
Related Questions
How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers
Limit on GUI Components? 0 Answers
Trying to view paper object on button press GUI Texture 3 Answers
Floating Enemy Health Bars? 1 Answer
Setting Scroll View Width GUILayout 1 Answer