Framing objects via script in the Unity Editor

Hi everyone,

Could anyone guide me to frame an object (like we do by pressing the F key in the Unity Editor) via a script? If not, then are there any other ways to do the same (ie. changing the position of the editor (scene view) camera via script)? I need this to frame the newly created object created by the user when he clicks inside the editor scene view.

Thanks!

When an object is selected you should be able to call

SceneView.FrameLastActiveSceneView();

Unfortunately Unity doesn’t have a method to directly frame a certain object. when the above mentioned method is executed there are several internal methods involved. It first uses InternalEditorUtility.CalculateSelectionBounds to get the bounds of the selected objects. Then it searches all active Editors (custom inspectors) if they provide a custom bounds value for this object and finally call the internal Frame method which takes a Bounds value. The Frame method calculates the required size and calls the internal LookAt method.

The the easiest way is to use

Selection.activeGameObject = yourObject;
SceneView.FrameLastActiveSceneView();

Hi, I don’t know if there is such a call and I’m sure there are ways of calling exactly that using reflection etc. But, seeing as it is in the menu, why not call the menu item from a script. I hope that helps =)