- Home /
OnSceneGUI event Use() ates too much :)
Hello!
I am painting on a 2D texture in Editor mode(tex.SetPixels(...)/tex.Apply()). I am painting by cells(15x15). The process is simple: I click on the cell and it changed its color. I could paint by click(one click paints on cell). But it is not comfortable. So I decided to make the following: when I hold the mouse button down the process of painting never stops until I release the mouse. Here are the code that do it:
void OnSceneGUI(SceneView sceneView)
{
EventType evType = Event.current.type;
if (Event.current.button == 0)
{
switch(evType)
{
case EventType.layout:
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
break;
case EventType.mouseDown:
lMouseBtnDown = true;
break;
case EventType.mouseUp:
lMouseBtnDown = false;
break;
case EventType.mouseDrag:
//Event.current.Use();
break;
}
}
if (lMouseBtnDown)
{
OnSceneViewMouseDown();
}
}
void OnSceneViewMouseDown()
{
Vector3 mousePosition = Event.current.mousePosition;
Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
mousePosition = ray.origin;
if (dg == null)
InitDebugGrid();
dg.GridCellPaint(mousePosition, passabilityRate);
Event.current.Use();
}
However, this Event.current.Use() ates too much. If I move the mouse across the scene with mouse button down I will not see any result on the screen until I release the button. If I remove .Use() the result of painting would be immediatelly drawn on the screen. But If I remove .Use() also I would select the texture in Scene View. I wonder if there is a possibility to do manual redraw of the SceneView ?
Thank you.
Answer by glad · May 23, 2017 at 02:09 PM
The solution is found, however I am not quite sure why it's working. So, if I do it like this everything is gonna be alright:
void OnSceneGUI(SceneView sceneView)
{
EventType evType = Event.current.type;
if (Event.current.button == 0)
{
switch(evType)
{
case EventType.layout:
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
break;
case EventType.mouseDown:
lMouseBtnDown = true;
break;
case EventType.mouseUp:
lMouseBtnDown = false;
OnSceneViewMouseDown();
break;
case EventType.mouseDrag:
if (lMouseBtnDown)
{
OnSceneViewMouseDown();
}
break;
}
}
}
The only thing I did is moved the if(lMouseButtonDown) block into the mouseDrag event handler.
I appreciate if anyone could make some comment on that solution.