- Home /
How to "paint" objects in the editor?
I've been wasting a lot of time trying to figure out how functionality was made similar to what's in Tidy TileMapper: http://tools.dopplerinteractive.com/search/label/Tidy%20TileMapper%3A%20Videos
I would just buy it, but it doesn't work precisely for my needs, unfortunately. The main thing I can't figure out is how he is able to drag that UI around in the scene. I've made an EditorWindow and I set myself up as a scene GUI delegate like this:
SceneView.onSceneGUIDelegate += this.OnSceneGUI;
private void OnSceneGUI( SceneView sceneview ) { //blah }
And I get mouse movements and the like all fine in OnSceneGUI using Event.current.mousePosition etc, which is great. But there are major issues using the mouse, mainly that for no reason I can discern you can only get MousePressed and MouseMoved events. Without a MouseReleased or MouseDragged event there's no real way to do this. Similarly, I can't figure out a way to get the mouse to not behave in its usual way, even with event.Use() being called. If my mouse is within my editable grid, I don't want to be selecting objects and the like.
I'd love to have this work but lots of searching hasn't turned up much. And seeing as the Unity Editor functionality is document so pitifully I'm not sure if I'll ever get this exact solution. Instead, I'm debating just pressing and holding a key button as those events seem to work in entirety, and the mouse position can be update with mouseMoved events.
So any pointers here?
Answer by demonpants · Aug 17, 2012 at 05:21 PM
I did a lot more Googling and found the solution. The issue is that the scene uses the touch before it can get to my OnSceneGUI function. You can have a look at this post:
http://forum.unity3d.com/threads/34137-SOLVED-Custom-Editor-OnSceneGUI-Scripting
Here is my working code:
private void OnSceneGUI( SceneView sceneview )
{
Event e = Event.current;
if ( e.type == EventType.MouseDown )
{
//if we're clicking within our desired area, block regular input
if ( isInArea( e.mousePosition )
{
blockingMouseInput = true;
}
}
else if ( e.type == EventType.MouseDrag )
{
}
else if ( e.type == EventType.MouseMove )
{
}
else if ( e.type == EventType.MouseUp )
{
if ( blockingMouseInput )
{
e.Use();
}
blockingMouseInput = false;
}
else if ( e.type == EventType.Layout )
{
//somehow this allows e.Use() to actually function and block mouse input
HandleUtility.AddDefaultControl( GUIUtility.GetControlID( GetHashCode(), FocusType.Passive ) );
}
}
Answer by ScroodgeM · Aug 17, 2012 at 06:44 AM
http://docs.unity3d.com/Documentation/ScriptReference/EventType.MouseDown.html http://docs.unity3d.com/Documentation/ScriptReference/EventType.MouseUp.html http://docs.unity3d.com/Documentation/ScriptReference/EventType.MouseMove.html http://docs.unity3d.com/Documentation/ScriptReference/EventType.MouseDrag.html
all these events works nice in GUI, so just try to handle it.
Nope, they don't work. Specifically, drag and up do not work.
Answer by ArthurianRedux · Sep 26, 2014 at 11:27 AM
If you want reliable mouse events then you could use the rawType of the current event. The event's regular type gets filtered out in some contexts e.g. when you move the mouse outside of the Scene Window.
I use this code to manage my custom editor gizmos when the left-mouse button is raised. Best of luck with your system.
if(Event.current.rawType == EventType.MouseUp && Event.current.button == 0)
{
// Your disabling logic for gizmos etc
}