Search Unity

How to have Handles.Button always face the scene view?

Discussion in 'Editor & General Support' started by BeautifulRiver, Aug 25, 2018.

  1. BeautifulRiver

    BeautifulRiver

    Joined:
    Sep 19, 2016
    Posts:
    47
    I can use

    Code (CSharp):
    1. Handles.Button(pos, rot, 1,  1, Handles.RectangleHandleCap);
    inside OnSceneGUI to draw a clickable button in the scene view. What is the rotation 'rot' if I want the button to always face directly toward the scene view (not the camera) in design time?

    Thanks.
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    I'm not sure what you mean by this. By scene view do you mean the scene view camera? By camera do you mean the camera component?

    If that's the case, you can use
    Code (csharp):
    1. Quaternion.LookRotation( Camera.current.transform.forward, Camera.current.transform.up );
     
    BeautifulRiver likes this.
  3. BeautifulRiver

    BeautifulRiver

    Joined:
    Sep 19, 2016
    Posts:
    47
    Thanks @Madgvox.

    By scene view I mean what I actually see in the scene while I am dragging, rotating the scene. So I want the buttons to always looks head-on when I rotate the scene.

    Your code works beautifully! It is exactly what I need!
     
  4. collinpatrick15

    collinpatrick15

    Joined:
    Nov 7, 2018
    Posts:
    38
    For anyone else who stumbles upon this post, I believe this is the more "intended" way to do it.
    Code (CSharp):
    1. SceneView sceneView = SceneView.lastActiveSceneView;
    2. Quaternion.LookRotation( sceneView.camera.transform.forward, sceneView.camera.transform.up )
     
    BBIT-SOLUTIONS and SirLogray like this.
  5. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    I don't believe this is correct.
    Camera.current
    refers to the currently drawing camera.
    SceneView.lastActiveSceneView
    refers to the SceneView that was most recently in focus. If you have multiple scene tabs open, then all of the buttons being drawn (in each scene view) will all face towards the camera you moved most recently, rather than the camera that is currently drawing that scene.

    Is there an issue with
    Camera.current
    that prevents its use in more recent versions of Unity?
     
  6. collinpatrick15

    collinpatrick15

    Joined:
    Nov 7, 2018
    Posts:
    38
    Ah, if you want to do multiple scene views you can use
    SceneView.currentDrawingSceneView
    to make the direction local to the individual scene viewports. If we want to render to just one view, there are options to check the exact view before rendering with
    SceneView.sceneViews
    . I believe SceneView was added for better support with UI Elements back in 2018.2, but can be used anywhere. It also gives access to more of the scene camera features that such as focusing on a target and changing near/far planes.

    I've found
    Camera.current
    to be a bit finicky to work with, but if it works, it works. I just expect it may eventually become depreciated with IMGUI in the coming years as UI Toolkit becomes the standard workflow for editor tooling.
     
    BBIT-SOLUTIONS likes this.
  7. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Good to know, thanks for the explanation!