Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Focusing the editor's Scene camera on an object/position

Discussion in 'Editor & General Support' started by MattCarr, Jul 15, 2010.

  1. MattCarr

    MattCarr

    Joined:
    Jul 5, 2009
    Posts:
    339
    I'm working on some custom editors with various development/debugging visuals rendered in the Scene window in the editor and I'm trying to find a way to modify the camera in the Scene window to focus on certain points in space like how hitting the 'F' key works for focusing on GameObjects typically.

    Is this possible?
     
  2. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    You can get Camera's Transform component, there is LookAt method. Actually in scene view it is done in slightly more convoluted way - but this will give you a head start ;-)
     
  3. MattCarr

    MattCarr

    Joined:
    Jul 5, 2009
    Posts:
    339
    Thanks for your reply. I'm aware of the LookAt method and had thought if I could get the scene view's camera then I could use that method, but it's getting the Scene view camera that I don't know how to do (plus I don't know if it has all the same functions if it is accessible).

    Just to clarify, I'm not trying to affect any cameras in my hierarchy/game, I'm trying to focus the scene view camera on an object/position. If the editor function that Unity would be calling when you hit the 'F' key was accessible then I could utilise that.
     
  4. Xsnip3rX

    Xsnip3rX

    Joined:
    Aug 29, 2009
    Posts:
    197
    Click the object in the hierarchy tab and press F
     
    PoisonedPog likes this.
  5. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    ah, i got it - seems like i slightly misunderstood your question.
    In case you want that scene behavior, why not just execute menu item for that.
    Select needed objects and then
    EditorApplication.ExecuteMenuItem("Edit/Frame Selected");
     
  6. MattCarr

    MattCarr

    Joined:
    Jul 5, 2009
    Posts:
    339
    Thanks Alexey, I'll use that to do what I need. I'll make a function to create an empty GameObject at the world position that I want to focus on, select that object, use the ExecuteMenuItem method, delete the temporary GameObject I've focused on and reselect my original object.
     
    dval likes this.
  7. Slem

    Slem

    Joined:
    Jan 28, 2009
    Posts:
    191
    This might be a stupid notion, but I cannot seem to find a method called ExecuteMenuItem in the EditorApplication class...

    I really need this functionality or something else that would override the focus("F") in sceneview.
     
  8. Slem

    Slem

    Joined:
    Jan 28, 2009
    Posts:
    191
    Just checked and it is of course a part of Unity 3.
     
  9. hencz

    hencz

    Joined:
    Aug 19, 2010
    Posts:
    86
    SceneView.lastActiveSceneView.FrameSelected()
     
    eDmitriy and AnomalusUndrdog like this.
  10. ThePilgrim

    ThePilgrim

    Joined:
    Apr 25, 2013
    Posts:
    17
    Adding to this, you can use the Frame function if you want to frame a bounds instead of what is selected.

    Code (CSharp):
    1. public void FocusPosition (Vector3 pos)
    2. {
    3.     SceneView.lastActiveSceneView.Frame(new Bounds(pos, Vector3.one), false);
    4. }