- Home /
Rect.contains with mouse position in editorwindow
I have custom editor window, and I'm trying to detect when my mouse is over a GUI texture that I can in the window.
I thought it seemed pretty straightforward, set wantsMouseMove to true, and then get the current event mouse position:
void Update () {
this.wantsMouseMove = true;
Vector2 mouse = Input.mousePosition;
Debug.Log ("MousePos: " + mouse);
}
Not so. For the mouse position, it always just prints (0.0,0.0) .
I also tried it this way, which I saw in another answer on here:
void OnGUI () {
wantsMouseMove = true;
EditorGUILayout.LabelField ("Mouse Position: ", Event.current.mousePosition.ToString ());
}
It sorta works, but it only changes the mouse postion when I click on the title bar of the window, or switch to another window/application and click back on the editorwindow.
I though this might have been an issue with my version of Unity, so I updated to the latest version (13GB download o_o) but no change.
Any ideas of whats happening here? I just need the get the current mouse position so I can use it in a Rect.Contains() in a GUI texture that's is added to the window when you click the "add building" button.
I thought this would be a simple issue that I would get help on fast, but apparently not :P
Answer by Yword · Sep 23, 2015 at 04:11 AM
Maybe you should Repaint the window OnInspectorUpdate.
using UnityEngine;
using UnityEditor;
public class MyWindow : EditorWindow
{
[MenuItem ("Window/My Window")]
static void Init()
{
MyWindow window = (MyWindow)EditorWindow.GetWindow(typeof(MyWindow));
window.Show();
}
void OnGUI()
{
Event e = Event.current;
GUILayout.Label("Mouse pos: " + e.mousePosition);
}
void OnInspectorUpdate()
{
if (EditorWindow.focusedWindow == this &&
EditorWindow.mouseOverWindow == this)
{
this.Repaint();
}
}
}
Your answer
Follow this Question
Related Questions
mouseposition and clicks in editor sceneview 0 Answers
EditorWindow, Utility Popup, not using custom PropertyDrawers 0 Answers
EditorWindow reference to other scripts functions 0 Answers
Weird bug when dragging object onto EditorGUILayout.ObjectField 0 Answers
Creating Editor 2D Animation Preview 2 Answers