Reputation: 370
I'm having an issue when trying to draw a texture on the GUI. Here's the faulty snippet within OnGUI()
:
var e = Event.current;
if (e.type == EventType.mouseDown)
{
Debug.Log("Mouse pressed");
GUI.DrawTexture(new Rect(e.mousePosition.x, e.mousePosition.y, 100, 110), ButtonTexture);
}
I'm successfully getting the log output but no texture is drawn. However if I use this outside of the if
statement the texture follow my mouse without any issues. I've been messing around with this for two hours but can't seem to figure out what's wrong. Any help would be appreciated.
Edit: It seems any kind of drawing doesn't work if the condition is an event, however no issues with a standard if(1>0)
. I've also tried using Input.GetMouseDown
as an alternative but same issue.
Upvotes: 1
Views: 134
Reputation: 78
I think it is probably because mouseDown is fired once per mouse click.
Event.Current will show the current event that is processing, which could be many different things per frame, so between calls to OnGUI this will probably change.
A better approach would be to store when mousedown event occurs and keep track until it is released, you could approach this by inspecting Event.Current or this Input.GetMouseButtonDown.
Here is a short sample of how you could track whether the mouse is down using Event.Current.
bool isMouseDown = false;
void OnGUI()
{
EventType currentEventType = Event.current.type;
if (currentEventType == EventType.MouseDown)
{
isMouseDown = true;
}
else if (currentEventType == EventType.MouseUp)
{
isMouseDown = false;
}
//Use isMouseDown as appropriate...
}
Upvotes: 1