Search Unity

Notification For Mouse-Selection Inside Editor?

Discussion in 'Editor & General Support' started by ImogenPoot, Apr 25, 2013.

  1. ImogenPoot

    ImogenPoot

    Joined:
    Jul 2, 2012
    Posts:
    214
    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!
     
    Last edited: Apr 25, 2013
  2. ImogenPoot

    ImogenPoot

    Joined:
    Jul 2, 2012
    Posts:
    214
    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.

    Code (csharp):
    1. private static Vector3 mouseStart;
    2.     private static Rect mouseRect;
    3.  
    4.     public void OnSceneGUI()
    5.     {
    6.         var screenSize = Camera.current.ViewportToScreenPoint(new Vector3(1,1,0));
    7.         var current = Event.current;
    8.  
    9.         if (current.type == EventType.MouseDown)
    10.         {
    11.             current.Use();
    12.             mouseStart = current.mousePosition;
    13.         }
    14.  
    15.         if (current.type == EventType.MouseDrag)
    16.         {
    17.             current.Use();
    18.             var xy = current.mousePosition;
    19.             mouseRect = new Rect(mouseStart.x, mouseStart.y, Math.Max(50, xy.x - mouseStart.x), Math.Max(50, xy.y - mouseStart.y));
    20.         }
    21.  
    22.         if (current.type == EventType.MouseUp)
    23.         {
    24.             current.Use();
    25.             mouseRect = new Rect(0, 0, 0, 0);
    26.         }
    27.  
    28.         if ((mouseRect.width > 0)  (mouseRect.height > 0))
    29.         {
    30.             var r = mouseRect;
    31.  
    32.             if (current.type != EventType.Repaint)
    33.                 current.Use();
    34.  
    35.             Handles.ClearCamera(new Rect(0, 0, screenSize.x, r.yMin), Camera.current);
    36.             Handles.ClearCamera(new Rect(0, r.yMin, r.xMin, screenSize.y), Camera.current);
    37.             Handles.ClearCamera(new Rect(r.xMin, r.yMax, screenSize.x, screenSize.y), Camera.current);
    38.             Handles.ClearCamera(new Rect(r.xMax, r.yMin, screenSize.x, r.yMax), Camera.current);
    39.  
    40.             Camera.current.rect = new Rect(0.0f, 0.0f, 1f, 1f);
    41.         }
    42.  
    43.         if (GUI.changed)
    44.             EditorUtility.SetDirty(target);
    45.     }
     
    Last edited: Apr 25, 2013