Hello guys.
I’m making a custom editor for my top down 2d game, that, as a part of its functionality, should determine which tile on the grid I click in the Scene view.
Converting click coordinates to grid position works flawlessly in my runtime Monobehavior scripts that use main camera.
However when I try to do it from custom editor, the values are off.
Here is my code (the CustomTileInspector script is a Monobehavior attached to the tilemap):
[CustomEditor(typeof(CustomTileInspector))]
public class MyTileEditor : Editor
{
void OnSceneGUI()
{
Event current = Event.current;
if (current.type == EventType.MouseDown && current.button == 1)
{
Vector3 mousePosition = current.mousePosition;
Camera sceneCamera = SceneView.currentDrawingSceneView.camera
;
mousePosition.y = sceneCamera.pixelHeight - mousePosition.y;
mousePosition = sceneCamera.ScreenToWorldPoint(mousePosition);
mousePosition.y = -mousePosition.y;
var Pos = new Vector3Int(Mathf.RoundToInt(mousePosition.x),
Mathf.RoundToInt(mousePosition.y), 0);
Debug.Log
("mouse clicked at "+ Pos.x+","+Pos.y);
}
}
The reported coordinates are off and don’t seem to correlate to the Grid properly.
Moreover, when I pan the camera the values change, even when I click on the same tile. I’m completely lost and would appreciate any insights on what I am doing wrong.