Problem Converting GUI to World Coords in Editor

Hi everyone,

I’m making a type of board game that I wish to design myself in the editor (there are many types of boards). I’m trying to write a level editor to make this easier.

What I want is to be able to select a type of tile from a list and then click in the scene view to make it appear where I clicked (while snapping into a grid).

I have been able to make it put a tile when I click, however it’s putting the tile at an offset (30-40 pixels above where I click). And I can’t seem to figure out why.

My code looks like this (didn’t get the grid part in yet):

if (Event.current.type == EventType.MouseUp)
    // load the correct prefab and create an instance of it
    GameObject space = Resources.LoadAssetAtPath(pathString, typeof(GameObject)) as GameObject;
    GameObject spaceInstance = PrefabUtility.InstantiatePrefab(space) as GameObject; = current;

    // set its position to where we click
    Vector2 mousePos = Event.current.mousePosition;
    mousePos.y = Screen.height - mousePos.y;

    Vector3 position = Camera.current.ScreenToWorldPoint(mousePos);
    position.z = 0;
    spaceInstance.transform.position = position;


I’m currently working around it simply by subtracting an additional 35 from mousePos.y, but there has to be a better fix than that.

You should use the Handles and HandleUtility class for anything related to the SceneView. The problem is that Screen.height returns the windows device context height which includes the tab-bar and the header bar.


The green area is the GUI area while the red one is the actual screen buffer. btw: the green one is enclosed by the red area.

The Handles class also let you draw cameras, handles, gui and other things in your own editor window. Just play around a bit. The documentation is your friend. Just switch to “editor classes” and browse the classes :wink:

In the editor I have found Screen.height is often inaccurate,
where you have…

mousePos.y = Screen.height - mousePos.y;

Try this instead:

mousePos.y = Camera.current.pixelHeight - mousePos.y;

The camera pixelHeight property should be the real height of the scene view.

Hope this helps