Notification For Mouse-Selection Inside Editor?

Hi,

I need a feature like this (PS this will contribute to an OpenSource project I will publish in a month and is the Unity-Integration part :)!..

  1. User clicks on a button inside a custom inspector
  2. Mouse pointer is changed to a “camera” symbol or something
  3. User selects a rectangle inside the current editor view of the scene, just like you can drag a rectangle for selecting objects right now
    3.5) Preferably, the standard “select objects” functionality that normally happens now, should also be disabled during this process.
  4. The coordinates&dimensions of this rectangle need to be intercepted by my script
  5. Mouse cursor changes back to normal
  6. I also need the editor view camera, especially the transformation matricies. Right now I can only get the ones for the Game-View, but that is insufficent, I need the one from the editor window!

Feature 3, 4 and 6 are crucial. The rest would be nice to have. Right now I can’t figure out anything but the first one LOL.

Some pointers would be nice. I don’t need code in particular (would be nice though), API docs or whatever is normally sufficient, I just have no idea how to implement these features right now, since its Editor, I only now how to do that in Game-Mode.

Thanks!

Pheww! I found something that seems to work… Still there are some artifacts. How could I disable all gizmos for the currently selected scene object? Because right now they are distorted while selecting the camera region.

Also the selected camera region is not 100% aligned with the scene view. If I end the drag&drop process you can see that it is a few pixels larger in all directions because it kinds “snaps” back to the original scene view. You need to try it out to see what I mean ;). The code just needs to be added to a custom editor script for any monobehaviour and then you can select a subregion of the view screen by dragging with the right mouse button.

PS: I dislike the fact that to create code like this, you are basically forced to skim through Unity using a decompiler… Maybe you could provide at least some major portion of the front-end source code along with the engine. After all, decompilation yields enough results for the “evil” guys anyway. So this only holds back honest users.

private static Vector3 mouseStart;
    private static Rect mouseRect;

    public void OnSceneGUI()
    {
        var screenSize = Camera.current.ViewportToScreenPoint(new Vector3(1,1,0));
        var current = Event.current;

        if (current.type == EventType.MouseDown)
        {
            current.Use();
            mouseStart = current.mousePosition;
        }

        if (current.type == EventType.MouseDrag)
        {
            current.Use();
            var xy = current.mousePosition;
            mouseRect = new Rect(mouseStart.x, mouseStart.y, Math.Max(50, xy.x - mouseStart.x), Math.Max(50, xy.y - mouseStart.y));
        }

        if (current.type == EventType.MouseUp)
        {
            current.Use();
            mouseRect = new Rect(0, 0, 0, 0);
        }

        if ((mouseRect.width > 0)  (mouseRect.height > 0))
        {
            var r = mouseRect;

            if (current.type != EventType.Repaint)
                current.Use();

            Handles.ClearCamera(new Rect(0, 0, screenSize.x, r.yMin), Camera.current);
            Handles.ClearCamera(new Rect(0, r.yMin, r.xMin, screenSize.y), Camera.current);
            Handles.ClearCamera(new Rect(r.xMin, r.yMax, screenSize.x, screenSize.y), Camera.current);
            Handles.ClearCamera(new Rect(r.xMax, r.yMin, screenSize.x, r.yMax), Camera.current);

            Camera.current.rect = new Rect(0.0f, 0.0f, 1f, 1f);
        }

        if (GUI.changed)
            EditorUtility.SetDirty(target);
    }