Search Unity

Tricky: Wants Game window to receive mouse event in Editor and not in playing mode

Discussion in 'Editor & General Support' started by cybie, Aug 16, 2013.

  1. cybie

    cybie

    Joined:
    Sep 17, 2010
    Posts:
    91
    I want to move the game camera around with mouse-look type of behavior, in Editor and not during gameplay mode.

    I tried two ways:
    1) Attach a ExecuteinEditMode "MoveCamera" script to the camera, however, the Update doesn't get call regularly.
    2) Then I tried to put the camera code in OnGUI of the same script. It gets called every time I press the mouse button in the game window, but the Event.current.isMouse is never true, and the Event.current.button is always 0 (even if I pressed middle and right button).

    Anyone have ideas on how I can achieve this?

    Thanks
    David
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    I would just use normal scripting to do it, then remove said script before building finished game
     
  3. cybie

    cybie

    Joined:
    Sep 17, 2010
    Posts:
    91
    Ok I worked out the magic combination, although I don't fully understand it.

    The goal is to allow the Game View to received mouse event in the Editor, and not during playmode.

    First you have make your component [ExecuteInEditMode]. Then enter the follow for OnGUI:

    Code (csharp):
    1.    
    2. void OnGUI()
    3. {      
    4.     if (Event.current.type == EventType.Layout || Event.current.type == EventType.Repaint)
    5.     {
    6.         EditorUtility.SetDirty(this); // this is important, if omitted, "Mouse down" will not be display
    7.     }
    8.     else if (Event.current.type == EventType.MouseDown)
    9.     {
    10.         Debug.Log("Mouse down");
    11.     }
    12. }
    13.  
    Then you should be receiving mouse event in the Game View even if the game is not playing.

    David Lam
     
    Last edited: Feb 9, 2024