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;
spaceInstance.name = 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;
EditorUtility.SetDirty(spaceInstance);
}
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.