- Home /
How to get mouse position in World Space when in Editor Mode
Hi! I've got an enemy that "patrols" left to right between two x coordinates (I'm working in 2D).
The two x coordinates are public variables that I have to set in the Editor.
Currently, I have to move a gameObject where I want my enemy to make a half-turn, copy the X position of the gameObject, and paste it in my enemy public variable.
Is there any easiest way to set coordinates in the editor?
I tried with ContextMenu but given the fact that Coroutines don't run in editor mode, I don't know how to get it to wait for mouse click.
[ContextMenu ("SetX1")]
void SetX1 () {
while (!Input.GetMouseButtonDown(0)) {
yield;
}
print ("Mouse Clicked");
}
Of course this doesn't work since it's a function and not a coroutine...
Thanks a lot in advance!
(Please forgive my english :) )
Answer by Souk21 · Jan 15, 2015 at 09:24 PM
I found the solution:
using UnityEngine;
using System.Collections;
using System.Reflection;
using UnityEditor;
[CustomEditor(typeof(testEditortoWorld))]
public class myEditor : Editor {
private static bool m_editMode = false;
private static bool m_editMode2 = false;
void OnSceneGUI()
{
testEditortoWorld test = (testEditortoWorld)target;
if (m_editMode)
{
if (Event.current.type == EventType.layout) {
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
}
if (Event.current.type == EventType.MouseDown)
{
Vector2 mousePos = Event.current.mousePosition;
mousePos.y = Camera.current.pixelHeight - mousePos.y;
Vector3 position = Camera.current.ScreenPointToRay(mousePos).origin;
test.bord1X = position.x;
m_editMode = false;
}
Event.current.Use();
}
if (m_editMode2)
{
if (Event.current.type == EventType.layout) {
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
}
if (Event.current.type == EventType.MouseDown)
{
Vector2 mousePos = Event.current.mousePosition;
mousePos.y = Camera.current.pixelHeight - mousePos.y;
Vector3 position = Camera.current.ScreenPointToRay(mousePos).origin;
test.bord2X = position.x;
m_editMode2 = false;
}
Event.current.Use();
}
}
public override void OnInspectorGUI()
{
DrawDefaultInspector ();
GUILayout.BeginHorizontal ("box");
if (m_editMode)
{
if (GUILayout.Button("stopen",GUILayout.Width(60)))
{
m_editMode = false;
}
}
else
{
if (GUILayout.Button("EditX1",GUILayout.Width(60)))
{
m_editMode = true;
m_editMode2 = false;
}
}
if (m_editMode2)
{
if (GUILayout.Button("stopen",GUILayout.Width(60)))
{
m_editMode2 = false;
}
}
else
{
if (GUILayout.Button("EditX2",GUILayout.Width(60)))
{
m_editMode2 = true;
m_editMode = false;
}
}
GUILayout.EndHorizontal();
}
}
testEditortoWorld being my enemyScript and bord1X and bord2X the coordinates variables
There's a lot of good stuff in this script. One thing to make it slightly cleaner is ins$$anonymous$$d of:
Camera.current.ScreenPointToRay(mousePos).origin;
You can do:
Camera.current.ScreenToWorldPoint(mousePos);
Your answer
Follow this Question
Related Questions
World position of a mouse click with a tilted camera 2 Answers
Event System value of mouse position is wrong. 0 Answers
Transform's position wherever the mouse is pointing. 2 Answers
getting world coordinates of mouse position 2 Answers
Position set way off / Different coordinate system problem 0 Answers