- Home /
Editor Scripting Input Problem OnGUI()
I'm trying to use mouse imputs in editor scripting by using "Event" class in OnGUI funcion. But as OnGUI is called multiple times in one frame I can't do what I want to. Here is the code I use. Is there a way to limit the input count in a frame or use Update function?
private void CheckInput()
{
_e = Event.current;
if (_e.type == EventType.MouseDown && _e.button == 0)
_isMouseDown = true;
else if (_e.type == EventType.MouseUp && _e.button == 0)
_isMouseDown = false;
_prevMousePos = _curMousePos;
_curMousePos.x = _e.mousePosition.x;
_curMousePos.y = _e.mousePosition.y;
}
I can't. It gives NullReferenceException if I use event in Update function. I think events can be used only in OnGUI function.
Answer by numberkruncher · May 28, 2013 at 08:52 AM
The Mouse down event should only occur once each time the mouse button is pressed down. You are, however, assigning your mouse data too frequently:
Note: If you do not use GUIUtility.hotControl
then there is no guarantee that EventType.MouseUp
will be triggered.
Example along lines of your question:
private void CheckInput() {
// Get a unique ID for your control.
int controlID = GUIUtility.GetControlID(FocusType.Passive);
switch (Event.current.GetTypeForControl(controlID)) {
case EventType.MouseDown:
// If you have a rectangle for your control then you should
// perform bounds check here...
if (Event.current.button == 0) {
// Lock on your custom control to prevent interaction with
// other controls in your GUI.
GUIUtility.hotControl = controlID;
Event.current.Use();
_isMouseDown = true;
}
break;
case EventType.MouseMove:
case EventType.MouseDrag:
// Only update mouse position on move or drag.
_prevMousePos = _curMousePos;
_curMousePos.x = Event.current.mousePosition.x;
_curMousePos.y = Event.current.mousePosition.y;
break;
case EventType.MouseUp:
// If this control is currently active.
if (GUIUtility.hotControl == controlID) {
// Release lock on it :)
GUIUtility.hotControl = 0;
Event.current.Use();
_isMouseDown = false;
}
break;
}
}
Example with GUILayout:
private static readonly GUIStyle _customStyle;
private void CheckInput() {
// Initialise custom style which defines control area of 300x300.
if (_customStyle == null) {
_customStyle = new GUIStyle();
_customStyle.fixedWidth = 300;
_customStyle.fixedHeight = 300;
}
// Get position of control area using layout.
Rect position = GUILayoutUtility.GetRect(GUIContent.none, _customStyle);
// Get a unique ID for your control.
int controlID = GUIUtility.GetControlID(FocusType.Passive, position);
switch (Event.current.GetTypeForControl(controlID)) {
case EventType.MouseDown:
// If you have a rectangle for your control then you should
// perform bounds check here...
if (Event.current.button == 0
&& position.Contains(Event.current.mousePosition)) {
// Lock on your custom control to prevent interaction with
// other controls in your GUI.
GUIUtility.hotControl = controlID;
Event.current.Use();
_isMouseDown = true;
}
break;
case EventType.MouseMove:
case EventType.MouseDrag:
// Only update mouse position on move or drag.
_prevMousePos = _curMousePos;
_curMousePos.x = Event.current.mousePosition.x;
_curMousePos.y = Event.current.mousePosition.y;
break;
case EventType.MouseUp:
// If this control is currently active.
if (GUIUtility.hotControl == controlID) {
// Release lock on it :)
GUIUtility.hotControl = 0;
Event.current.Use();
_isMouseDown = false;
}
break;
}
}
Your answer
Follow this Question
Related Questions
Create Multiple Foldouts. 1 Answer
Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers
Script work in editor but not in build 0 Answers
How to fix ArgumentException? 3 Answers
Mouse and Oculus Touch Controller Inputs 0 Answers