How to get mouse click world position in the scene view in editor script?

I am trying to write an editor script for Unity Terrain that instantiates objects where I click on terrain in the scene view (pretty much like the tree generation of terrain). Input.GetMouseButtonDown(0); appears to be not working in scene view.

How can i get the mouse button down event and the position of click in scene view in world coordinates?


@Adam-Mechtley’s, pointed me in the right direction. Here’s what I came up with:

using UnityEngine;

using UnityEditor;
/// <summary>
/// </summary>
public class ClickSpawn : MonoBehaviour
    private void OnEnable()
        if (!Application.isEditor)
        SceneView.onSceneGUIDelegate += OnScene;

    void OnScene(SceneView scene)
        Event e = Event.current;

        if (e.type == EventType.MouseDown && e.button == 2)
            Debug.Log("Middle Mouse was pressed");

            Vector3 mousePos = e.mousePosition;
            float ppp = EditorGUIUtility.pixelsPerPoint;
            mousePos.y = - mousePos.y * ppp;
            mousePos.x *= ppp;

            Ray ray =;
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
                //Do something, ---Example---
                GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                go.transform.position = hit.point;
                Debug.Log("Instantiated Primitive " + hit.point);

if (Event.current.type != EventType.MouseDown || Event.current.button != 0)
// convert GUI coordinates to screen coordinates
Vector3 screenPosition = Event.current.mousePosition;
screenPosition.y = Camera.current.pixelHeight - screenPosition.y;
Ray ray = Camera.current.ScreenPointToRay (screenPosition);
RaycastHit hit;
// use a different Physics.Raycast() override if necessary
if (Physics.Raycast (ray, out hit))
// do stuff here using hit.point
// tell the event system you consumed the click
Event.current.Use ();

The same problem I have. Just found this question on Google