33// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
44
55using System ;
6+ using UnityEditor . ShortcutManagement ;
67using UnityEngine ;
78
89namespace UnityEditor
@@ -11,17 +12,17 @@ public class SceneViewCameraWindow : PopupWindowContent
1112 {
1213 static class Styles
1314 {
14- static bool s_Initialized ;
15- public static GUIStyle settingsArea ;
15+ public static readonly GUIContent copyPlacementLabel = EditorGUIUtility . TrTextContent ( "Copy Placement" ) ;
16+ public static readonly GUIContent pastePlacementLabel = EditorGUIUtility . TrTextContent ( "Paste Placement" ) ;
17+ public static readonly GUIContent copySettingsLabel = EditorGUIUtility . TrTextContent ( "Copy Settings" ) ;
18+ public static readonly GUIContent pasteSettingsLabel = EditorGUIUtility . TrTextContent ( "Paste Settings" ) ;
19+ public static readonly GUIContent resetSettingsLabel = EditorGUIUtility . TrTextContent ( "Reset Settings" ) ;
1620
17- public static void Init ( )
18- {
19- if ( s_Initialized )
20- return ;
21-
22- s_Initialized = true ;
21+ public static readonly GUIStyle settingsArea ;
2322
24- settingsArea = new GUIStyle ( )
23+ static Styles ( )
24+ {
25+ settingsArea = new GUIStyle
2526 {
2627 border = new RectOffset ( 4 , 4 , 4 , 4 ) ,
2728 } ;
@@ -39,11 +40,6 @@ public static void Init()
3940 GUIContent m_EasingEnabled ;
4041 GUIContent m_SceneCameraLabel = EditorGUIUtility . TrTextContent ( "Scene Camera" ) ;
4142 GUIContent m_NavigationLabel = EditorGUIUtility . TrTextContent ( "Navigation" ) ;
42- GUIContent m_CopyPlacementLabel = EditorGUIUtility . TrTextContent ( "Copy Placement" ) ;
43- GUIContent m_PastePlacementLabel = EditorGUIUtility . TrTextContent ( "Paste Placement" ) ;
44- GUIContent m_CopySettingsLabel = EditorGUIUtility . TrTextContent ( "Copy Settings" ) ;
45- GUIContent m_PasteSettingsLabel = EditorGUIUtility . TrTextContent ( "Paste Settings" ) ;
46- GUIContent m_ResetSettingsLabel = EditorGUIUtility . TrTextContent ( "Reset Settings" ) ;
4743
4844 const int kFieldCount = 12 ;
4945 const int kWindowWidth = 290 ;
@@ -102,8 +98,6 @@ public override void OnGUI(Rect rect)
10298
10399 void Draw ( )
104100 {
105- Styles . Init ( ) ;
106-
107101 var settings = m_SceneView . cameraSettings ;
108102
109103 m_Scroll = GUILayout . BeginScrollView ( m_Scroll ) ;
@@ -117,7 +111,7 @@ void Draw()
117111 GUILayout . Label ( m_SceneCameraLabel , EditorStyles . boldLabel ) ;
118112 GUILayout . FlexibleSpace ( ) ;
119113 if ( GUILayout . Button ( EditorGUI . GUIContents . titleSettingsIcon , EditorStyles . iconButton ) )
120- ShowContextMenu ( ) ;
114+ ShowContextMenu ( m_SceneView ) ;
121115 GUILayout . EndHorizontal ( ) ;
122116
123117 EditorGUIUtility . labelWidth = kPrefixLabelWidth ;
@@ -212,55 +206,97 @@ void DrawClipPlanesField(GUIContent label, ref float near, ref float far, float
212206 EditorGUIUtility . labelWidth = oldLabelWidth ;
213207 }
214208
215- void ShowContextMenu ( )
209+ internal static void ShowContextMenu ( SceneView view )
216210 {
217211 var menu = new GenericMenu ( ) ;
218- menu . AddItem ( m_CopyPlacementLabel , false , CopyPlacement ) ;
219- if ( Clipboard . HasCustomValue < TransformWorldPlacement > ( ) )
220- menu . AddItem ( m_PastePlacementLabel , false , PastePlacement ) ;
212+ menu . AddItem ( Styles . copyPlacementLabel , false , ( ) => CopyPlacement ( view ) ) ;
213+ if ( CanPastePlacement ( ) )
214+ menu . AddItem ( Styles . pastePlacementLabel , false , ( ) => PastePlacement ( view ) ) ;
221215 else
222- menu . AddDisabledItem ( m_PastePlacementLabel ) ;
223- menu . AddItem ( m_CopySettingsLabel , false , CopySettings ) ;
216+ menu . AddDisabledItem ( Styles . pastePlacementLabel ) ;
217+ menu . AddItem ( Styles . copySettingsLabel , false , ( ) => CopySettings ( view ) ) ;
224218 if ( Clipboard . HasCustomValue < SceneView . CameraSettings > ( ) )
225- menu . AddItem ( m_PasteSettingsLabel , false , PasteSettings ) ;
219+ menu . AddItem ( Styles . pasteSettingsLabel , false , ( ) => PasteSettings ( view ) ) ;
226220 else
227- menu . AddDisabledItem ( m_PasteSettingsLabel ) ;
228- menu . AddItem ( m_ResetSettingsLabel , false , ResetSettings ) ;
221+ menu . AddDisabledItem ( Styles . pasteSettingsLabel ) ;
222+ menu . AddItem ( Styles . resetSettingsLabel , false , ( ) => ResetSettings ( view ) ) ;
229223
230224 menu . ShowAsContext ( ) ;
231225 }
232226
233- void CopyPlacement ( )
227+ // ReSharper disable once UnusedMember.Local - called by a shortcut
228+ [ Shortcut ( "Camera/Copy Placement" ) ]
229+ static void CopyPlacementShortcut ( )
230+ {
231+ // if we are interacting with a game view, copy the main camera placement
232+ var playView = PlayModeView . GetLastFocusedPlayModeView ( ) ;
233+ if ( playView != null && ( EditorWindow . focusedWindow == playView || EditorWindow . mouseOverWindow == playView ) )
234+ {
235+ var cam = Camera . main ;
236+ if ( cam != null )
237+ Clipboard . SetCustomValue ( new TransformWorldPlacement ( cam . transform ) ) ;
238+ }
239+ // otherwise copy the last active scene view placement
240+ else
241+ {
242+ var sceneView = SceneView . lastActiveSceneView ;
243+ if ( sceneView != null )
244+ CopyPlacement ( sceneView ) ;
245+ }
246+ }
247+
248+ static void CopyPlacement ( SceneView view )
249+ {
250+ Clipboard . SetCustomValue ( new TransformWorldPlacement ( view . camera . transform ) ) ;
251+ }
252+
253+ // ReSharper disable once UnusedMember.Local - called by a shortcut
254+ [ Shortcut ( "Camera/Paste Placement" ) ]
255+ static void PastePlacementShortcut ( )
234256 {
235- Clipboard . SetCustomValue ( new TransformWorldPlacement ( m_SceneView . camera . transform ) ) ;
257+ var sceneView = SceneView . lastActiveSceneView ;
258+ if ( sceneView == null ) return ;
259+ if ( CanPastePlacement ( ) )
260+ PastePlacement ( sceneView ) ;
236261 }
237262
238- void PastePlacement ( )
263+ static bool CanPastePlacement ( )
239264 {
240- var tr = m_SceneView . camera . transform ;
265+ return Clipboard . HasCustomValue < TransformWorldPlacement > ( ) ;
266+ }
267+
268+ static void PastePlacement ( SceneView view )
269+ {
270+ var tr = view . camera . transform ;
241271 var placement = Clipboard . GetCustomValue < TransformWorldPlacement > ( ) ;
242272 tr . position = placement . position ;
243273 tr . rotation = placement . rotation ;
244274 tr . localScale = placement . scale ;
245- m_SceneView . AlignViewToObject ( tr ) ;
246- m_SceneView . Repaint ( ) ;
275+
276+ // Similar to what AlignViewToObject does, except we need to do that instantly
277+ // in case the shortcut key was pressed while FPS camera controls (right click drag)
278+ // were active.
279+ view . size = 10 ;
280+ view . LookAt ( tr . position + tr . forward * view . cameraDistance , tr . rotation , view . size , view . orthographic , true ) ;
281+
282+ view . Repaint ( ) ;
247283 }
248284
249- void CopySettings ( )
285+ static void CopySettings ( SceneView view )
250286 {
251- Clipboard . SetCustomValue ( m_SceneView . cameraSettings ) ;
287+ Clipboard . SetCustomValue ( view . cameraSettings ) ;
252288 }
253289
254- void PasteSettings ( )
290+ static void PasteSettings ( SceneView view )
255291 {
256- m_SceneView . cameraSettings = Clipboard . GetCustomValue < SceneView . CameraSettings > ( ) ;
257- m_SceneView . Repaint ( ) ;
292+ view . cameraSettings = Clipboard . GetCustomValue < SceneView . CameraSettings > ( ) ;
293+ view . Repaint ( ) ;
258294 }
259295
260- void ResetSettings ( )
296+ static void ResetSettings ( SceneView view )
261297 {
262- m_SceneView . ResetCameraSettings ( ) ;
263- m_SceneView . Repaint ( ) ;
298+ view . ResetCameraSettings ( ) ;
299+ view . Repaint ( ) ;
264300 }
265301 }
266302}
0 commit comments