1
\$\begingroup\$

I want to instantiate a prefab at the mouse position in the editor. All the answers I found till now are for instatiate a prefab at runtime but i want to instantiate in the editor. Currently I am trying to use input.GetMouseButtonDown but that doesn't seem to work ? Also is it fine to use raycasting in the editor itself?

EDIT: Alright I found that we need to use eventtype.MouseDown but now the script is not executing? there are no errors in the console window

using UnityEngine;
using UnityEditor;
[ExecuteInEditMode]
public class mapMaker : Editor
{
    public GameObject rock;
    private void OnSceneGUI()
    {
        if (Event.current.type == EventType.MouseDown)
        {
            Ray ray = Camera.current.ScreenPointToRay(Event.current.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                Debug.Log(Event.current.mousePosition);
                Vector3 newTilePosition = hit.point;
                Instantiate(rock, newTilePosition, Quaternion.identity);
            }
        }
    }
}

```
\$\endgroup\$
2
  • \$\begingroup\$ What have you tried so far? When you try using the runtime method at edit time, do the results differ from what you want in some way? \$\endgroup\$
    – DMGregory
    Commented Nov 6, 2020 at 13:06
  • \$\begingroup\$ So is your problem with instantiating, as stated in your title, or with detecting mouse clicks, which isn't mentioned in your question at all? It looks like you should edit your question to describe in full what you've tried, including the script you're using. \$\endgroup\$
    – DMGregory
    Commented Nov 6, 2020 at 13:31

1 Answer 1

0
\$\begingroup\$

You may have to listen to scene events.

Here is an example which creates an Editor Window which you can open from Window menu (in Unity menu bar). I think you can use pretty much the same code if you want to have similar functionality from a Custom Editor.

How to use: Open this window from Window menu (Raycast in editor test), then click 'Enable Raycasting' in Editor Window and click on some Collider in Scene View to spawn Default Cubes.

using UnityEditor;
using UnityEngine;

public class Raycast : EditorWindow
{
    static bool active;

    // Open this from Window menu
    [MenuItem("Window/Raycast in editor test")]
    static void Init()
    {
        var window = (Raycast)EditorWindow.GetWindow(typeof(Raycast));
        window.Show();
    }

    // Listen to scene event
    void OnEnable() => SceneView.duringSceneGui += OnSceneGUI;
    void OnDisable() => SceneView.duringSceneGui -= OnSceneGUI;

    // Receives scene events
    // Use event mouse click for raycasting
    void OnSceneGUI(SceneView view)
    {
        if (!active)
        {
            return;
        }

        if (Event.current.type == EventType.MouseDown)
        {
            Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
            RaycastHit hit;

            // Spawn cube on hit location
            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log("Hit: " + hit.collider.gameObject.name);

                var obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                obj.transform.position = hit.point;
            }
        }

        Event.current.Use();
    }

    // Creates a editor window with button 
    // to toggle raycasting on/off
    void OnGUI()
    {
        if (GUILayout.Button("Enable Raycasting"))
        {
            active = !active;
        }

        GUILayout.Label("Active:" + active);
    }

}
\$\endgroup\$
1
  • \$\begingroup\$ I spent hours making this script on GenAI but all were rubbish. Thanks \$\endgroup\$ Commented May 4 at 10:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .