3
\$\begingroup\$

I want to create a custom level editor for the level designers that should allow them to place predefined objects in Unity3d similar to the Terrain wizard.

How can I register Mouse Events in the Scene window of the Editor?

\$\endgroup\$
  • \$\begingroup\$ This does of course not answer your question, but in my current project I integrated the map editor into the game itself. So in order to edit a map, the mapper plays it in the game and then clicks on a "Map Editor" button which will be hidden in the release build. \$\endgroup\$ – Philipp May 27 '16 at 9:24
  • \$\begingroup\$ That's a nice concept. However I can't give up the full feature set of the Unity Editor and I sure as hell ain't gonna reimplement all of it! ;) \$\endgroup\$ – 000000000000000000000 May 27 '16 at 10:22
1
\$\begingroup\$

Perhaps this is something you are looking for. This allows you to register mouse events in the scene, when a game object with the specified type is selected in the hierarchy.

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(SomeScriptType))]
public class MyEditorPlayer : Editor 
{
    void OnSceneGUI()
    {
        int controlID = GUIUtility.GetControlID(FocusType.Passive);

        switch (Event.current.GetTypeForControl(controlID))
        {
            case EventType.mouseDown:
                GUIUtility.hotControl = controlID;
                Debug.Log("MouseDown");
                Event.current.Use();
                break;

            case EventType.MouseUp:
                GUIUtility.hotControl = 0;
                Event.current.Use();
                break;
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

You only need to add the [ExecuteInEditMode] attribute to your class to run events in edit mode:

[ExecuteInEditMode]
public class myclass: MonoBehaviour 
{
    void Update() 
    {

    }
}
\$\endgroup\$
  • \$\begingroup\$ I know, but I want to only process Mouse Events in the LevelEditor window. \$\endgroup\$ – 000000000000000000000 May 27 '16 at 13:16
  • \$\begingroup\$ How i tell you can do it with ExecuteInEditMode, add only your mouse event/s in the class and you got. \$\endgroup\$ – joreldraw May 30 '16 at 6:09
  • \$\begingroup\$ I can see what your trying to say, but it is clear that others do not. Perhaps you should expand upon your answer? \$\endgroup\$ – Gnemlock Nov 5 '16 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.