- Home /
Event Mouse not working correctly
Im working on a custom editor, and the problem is, a simple Event.MouseDown and Event.MouseUp isn't working correctly at all. Code:
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
[CustomEditor(typeof(Thing)), Serializable, InitializeOnLoad]
public class ThingEditor : Editor {
void OnSceneGUI(){
Event current = Event.current;
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
if(current.type == EventType.MouseDown) {
Debug.Log ("HI");
}
if (current.type == EventType.MouseUp) {
Debug.Log ("BYE");
}
}
}
Its not even saying BYE when I want it to.
I don't know why your code isn't working because I never tried your approach, but I use
if(Input.Get$$anonymous$$ouseButtonDown(0))
and
Input.Get$$anonymous$$ouseButtonUp(0))
in one of my scripts and it works just fine. $$anonymous$$aybe try that? Then again I just saw you're doing something with OnSceneGUI, that was for OnGUI..
The "Input" class is only used In-Game. When wanting to get input In-Editor, you have to use Event. Its weird that way.
Answer by Bunny83 · Nov 24, 2013 at 10:33 PM
Are you sure your ThingEditor is active? So you have a Thing selected? Otherwise it ofcourse won't work.
If you want to handle all events you should subscribe an event to SceneView.onSceneGUIDelegate. Unfortunately the SceneView class is still not documented but is available since i can think back.
Next thing is InitializeOnLoad only loads the class at start. That will initialize the static members of the class. It won't create an instance of this editor. An editor is ment to "edit" an object (the target). Unless you have selected such an object (in your case a "Thing") there won't be an instance of this editor.
To handle generic SceneView events you should only use static things. This should work:
[InitializeOnLoad]
public class SceneViewEventHandler : Editor
{
// static constructor
static SceneViewEventHandler()
{
SceneView.onSceneGUIDelegate += OnSceneGUI;
}
static void OnSceneGUI(SceneView aView)
{
Event current = Event.current;
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
if(current.type == EventType.MouseDown)
{
Debug.Log ("HI");
}
if (current.type == EventType.MouseUp)
{
Debug.Log ("BYE");
}
}
}
Keep in mind that the onSceneGUIDelegate is called for each SceneView independently. If you want to refer to the scene view, use the parameter that is passed to the delegate.
If you plan more complicated editor extension you might want to download ILSpy and take a look into the UnityEditor.dll. It really helps to understand the system better.
http://docs.unity3d.com/Documentation/Manual/RunningEditorCodeOnLaunch.html
Nope this did not work. Everything i coded works fine except Event for the mouse. It says hi when i press my mouse down, but doesn't say bye when i let go.
If i use your code it works as i told you. The problem is when you click down and release you will deselect your object and your editor will be disabled. Also the sceneview eats the mouseup event in this case. Just test your middle or right mouse button. They will work just fine.
If you want to prevent deselecting of your object you have to register yourself as hotcontrol while your mouse is down. Again, if you take a look at UnityEditor library you can learn a lot. For example the TerrainInspector which implements the terrain-drawing-brush.
You have to do something like that:
int hash = "SomeUniqueStringForYourEditorType".GetHashCode();
void OnSceneGUI()
{
Event current = Event.current;
int ID = GUIUtility.GetControlID(hash, FocusType.Passive);
EventType type = current.GetTypeForControl(ID);
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
if (type == EventType.Layout)
{
HandleUtility.AddDefaultControl(ID);
}
if(type == EventType.$$anonymous$$ouseDown && HandleUtility.nearestControl == ID)
{
Debug.Log ("HI");
GUIUtility.hotControl = ID;
current.Use();
}
if (type == EventType.$$anonymous$$ouseUp)
{
Debug.Log ("BYE");
if (GUIUtility.hotControl == ID)
GUIUtility.hotControl = 0;
current.Use();
}
}
What's still missing in this example is that you should somehow disable "your" handling or a lot of the default features will be blocked.
So important is to only register your control id as default control when you actually want to get the mouse input. Also only "Use" the events when you are actually handling them. Also make sure you don't set the hotcontrol without resetting it, otherwise it would block the whole sceneView.
The newest code you posted worked beautifully! Thanks a bunch Bunny83!
Your answer
Follow this Question
Related Questions
Left Click Up Event in Editor 4 Answers
OnGUI Event 0 Answers
Detecting moment when GameObiect is created in hierarchy. 1 Answer
Adding Tab support to TextArea's TextEditor (almost done) 2 Answers
Persistent listeners for Button.onClick are removed at runtime, if buttons are instances of prefab 1 Answer