- Home /
How can i instantiate an object in edit mode by clicking the mouse?
I want to have a script, that is executed in edit mode and that instantiates an object by clicking a mouse button.
If i am not wrong, then Input is only called in Play mode, but i am looking for a mouse click detection in Edit mode (and yes my script uses "script executeineditmode()"). I know how to instantiate an object and how to detect mouse event during play mode, but i want to do this in edit mode with a script.
Answer by benjaminwharton · May 29, 2014 at 02:16 PM
You can add a delegate to SceneView to be called whenever a GUI event happens in the scene view - note that it can be swallowed up by Unity if it already handles what you are trying to do (ie. clicking on an existing object):
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
class MyClass
{
static MyClass()
{
SceneView.onSceneGUIDelegate += OnSceneGooey;
}
static void OnSceneGooey(SceneView sceneview)
{
Event e = Event.current;
if (e.type == EventType.MouseDown && e.button == 0)
Debug.Log("LMB was pressed");
}
}
Hope it helps. :)
Thank you for your fast answer :). I will try it and I will post if how it worked. Thank you very much :)
Answer by fafase · May 29, 2014 at 05:43 PM
Try this video http://unity3d.com/learn/tutorials/modules/intermediate/editor/adding-buttons-to-inspector
it shows how to create a button in the Inspector to create a new object at the given position.