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

Define the point to focus "f" on from script

Discussion in 'Scripting' started by Kragh, Jul 1, 2014.

  1. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    I have a tool where I want to be able to focus on a point of my choosing. When I push "f" it will by default focus the current object selected, but is it possible to define the center of focus myself from scripting?
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Everything is possible, but noone will be able to help you unless you post some code you already have.
     
  3. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Do you mean focusing at runtime or in editor?
     
  4. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657


    I think you misunderstand my question. My question could be completely unrelated to code in its form. It's a general question. It would be like if I asked if it is possible to set the position of a GameObject, and you then asked for code :) So I won't post any, as it would only confuse matters.

    Let me rephrase: When you work around in the editor, you can push "f" (not my code, a general function of the editor) while having an object selected, and the camera will focus on that. Also rotation wise...

    What I want to know is if it is possible to set the position by editor scripting. Pseudo: CurrentCenterOfView(Vector3 somePosition)
     
  5. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    In the editor :)
    It's an editor tool I'm building, and I need to be able to define the center of the view camera, to ease some user interaction.
     
  6. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    I haven't written many editor scripts, so this may be entirely wrong, but if you can get the current camera(Camera.current maybe?) you should be able to just use the LookAt function to get the effect you want.
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You may use this in an editor script:
    Code (csharp):
    1. Bounds OnGetFrameBounds () {
    2.    Bounds bounds = ...;
    3.    return (bounds);
    4. }
    5.  
    6. bool HasFrameBounds () {
    7.    return (true);
    8. }
     
  8. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    I'm trying to hook it up on the built-in "f" (Frame Selected) function. And I did get something to work, even though it became some of a hack due to some bug in Unity (ExecuteMenuItem("Edit/Frame Selected") along with some other Edit functions don't work, though they should)

    Here's my hack:

    Code (csharp):
    1.  
    2. void OnEnable()
    3. {
    4.      
    5.         SceneView.onSceneGUIDelegate += OnScene;
    6.      
    7. }
    8.  
    9. public void OnScene(SceneView sceneview)
    10. {
    11.  
    12.         if (Event.current.commandName == "FrameSelected")
    13.         {
    14.  
    15.             GameObject centerOfFrame = new GameObject("Framing"); //Create a GameObject which will act as target for firering the built-in menu functions of the editor
    16.  
    17.             centerOfFrame.transform.position = currentCenterOfFrame-Camera.current.transform.forward*10; //currentCenterOfFrame is a Vector3 i define in other parts of code. I then offset it using the view port forward vector reversed.
    18.  
    19.             centerOfFrame.transform.rotation = Camera.current.transform.rotation; //Rotate my helper object to have it equal to the viewport camera rotation          
    20.          
    21.             centerOfFrame.hideFlags = HideFlags.HideInHierarchy; //Hide the object
    22.  
    23.             GameObject[] selection = new GameObject[1];
    24.             selection[0] = centerOfFrame;
    25.             Selection.objects = selection;  //Set the helper object as my current selection in the editor
    26.          
    27. //Edit/Frame Selected doesn't work, so this is a hack by having rotated the selected object to the camera. It is by no means optimal, but it works. Kinda. Using "Align to Selected" instead
    28.          
    29.             EditorApplication.ExecuteMenuItem("GameObject/Align View to Selected");  //Align view to selected will not work as Frame Selected, because the viewport camera will also align its rotation to the object. That is why I have rotated the helper object to match the camera, before runing the menu function.
    30.        
    31.             Event.current.commandName = ""; //Clear the event so Unity won't do its own stuff          
    32.             selection = new GameObject[1];
    33.             selection[0] = initialSelection; //This is the object that has the tool
    34.             Selection.objects = selection; //Reselect the original object
    35.  
    36.             DestroyImmediate(centerOfFrame); //Destroy my helper object again
    37.  
    38.         }
    39. }
    40.  
    41.  
    Well. It works fine for this use.
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you try the code snippet I posted?
     
  10. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    Yeah... now I did. Works like a charm! Where is this functionality documented? That is perfect for my use!!!

    I'm sorry I didn't reply to your post, overlooked it somehow :)