44using UnityEngine ;
55using UnityEngine . ProBuilder ;
66using UnityEngine . ProBuilder . Shapes ;
7+ using Math = UnityEngine . ProBuilder . Math ;
78using UObject = UnityEngine . Object ;
89#if UNITY_2020_2_OR_NEWER
910using ToolManager = UnityEditor . EditorTools . ToolManager ;
@@ -238,22 +239,32 @@ internal void SetBounds(Vector3 size)
238239 m_BB_HeightCorner = m_BB_Origin + size ;
239240 }
240241
241- internal void DuplicatePreview ( Vector3 position )
242+ internal void DoDuplicateShapePreviewHandle ( Vector3 position )
242243 {
243- if ( position . Equals ( Vector3 . positiveInfinity ) || ! Event . current . isMouse )
244+ var evt = Event . current ;
245+
246+ if ( evt . type != EventType . Repaint )
247+ return ;
248+
249+ bool previewShortcutActive = evt . shift && ! ( evt . control || evt . command ) ;
250+
251+ if ( HandleUtility . nearestControl != m_ControlID || ! previewShortcutActive )
252+ {
253+ DestroyImmediate ( m_DuplicateGO ) ;
244254 return ;
255+ }
245256
246257 var pivotLocation = ( PivotLocation ) s_LastPivotLocation . value ;
247258 var size = currentShapeInOverlay . size ;
248259
249260 m_Bounds . size = size ;
250-
251261 Vector3 cornerPosition ;
252- switch ( pivotLocation )
262+
263+ switch ( pivotLocation )
253264 {
254265 case PivotLocation . FirstCorner :
255266 cornerPosition = GetPoint ( position ) ;
256- m_PlaneRotation = Quaternion . LookRotation ( m_PlaneForward , m_Plane . normal ) ;
267+ m_PlaneRotation = Quaternion . LookRotation ( m_PlaneForward , m_Plane . normal ) ;
257268 m_Bounds . center = cornerPosition + m_PlaneRotation * size / 2f ;
258269
259270 m_BB_Origin = cornerPosition ;
@@ -266,32 +277,37 @@ internal void DuplicatePreview(Vector3 position)
266277 position = GetPoint ( position ) ;
267278 cornerPosition = position - size / 2f ;
268279 cornerPosition . y = position . y ;
269- m_Bounds . center = cornerPosition + new Vector3 ( size . x / 2f , 0 , size . z / 2f ) + ( size . y / 2f ) * m_Plane . normal ;
270- m_PlaneRotation = Quaternion . LookRotation ( m_PlaneForward , m_Plane . normal ) ;
280+ m_Bounds . center = cornerPosition + new Vector3 ( size . x / 2f , 0 , size . z / 2f ) + ( size . y / 2f ) * m_Plane . normal ;
281+ m_PlaneRotation = Quaternion . LookRotation ( m_PlaneForward , m_Plane . normal ) ;
271282
272283 m_BB_Origin = m_Bounds . center - m_PlaneRotation * ( size / 2f ) ;
273284 m_BB_HeightCorner = m_Bounds . center + m_PlaneRotation * ( size / 2f ) ;
274285 m_BB_OppositeCorner = m_BB_HeightCorner - m_PlaneRotation * new Vector3 ( 0 , size . y , 0 ) ;
275286 break ;
276287 }
277288
278- ProBuilderShape proBuilderShape ;
279-
280- if ( m_DuplicateGO == null )
289+ if ( m_DuplicateGO == null )
281290 {
282291 var instantiated = ShapeFactory . Instantiate ( activeShapeType , ( ( PivotLocation ) s_LastPivotLocation . value ) ) ;
283- proBuilderShape = instantiated . GetComponent < ProBuilderShape > ( ) ;
284- m_DuplicateGO = proBuilderShape . gameObject ;
292+ var shape = instantiated . GetComponent < ProBuilderShape > ( ) ;
293+ m_DuplicateGO = shape . gameObject ;
285294 m_DuplicateGO . hideFlags = HideFlags . DontSave | HideFlags . HideInHierarchy ;
286- ApplyPrefsSettings ( proBuilderShape ) ;
287- proBuilderShape . GetComponent < MeshRenderer > ( ) . sharedMaterial = m_ShapePreviewMaterial ;
295+ ApplyPrefsSettings ( shape ) ;
296+ shape . GetComponent < MeshRenderer > ( ) . sharedMaterial = m_ShapePreviewMaterial ;
297+
298+ EditorShapeUtility . CopyLastParams ( shape . shape , shape . shape . GetType ( ) ) ;
299+ shape . Rebuild ( m_Bounds , m_PlaneRotation , m_BB_Origin ) ;
300+ ProBuilderEditor . Refresh ( false ) ;
288301 }
289- else
290- proBuilderShape = m_DuplicateGO . GetComponent < ProBuilderShape > ( ) ;
291302
292- EditorShapeUtility . CopyLastParams ( proBuilderShape . shape , proBuilderShape . shape . GetType ( ) ) ;
293- proBuilderShape . Rebuild ( m_Bounds , m_PlaneRotation ) ;
294- ProBuilderEditor . Refresh ( false ) ;
303+ var pivot = GetPoint ( position ) ;
304+ if ( pivotLocation == PivotLocation . Center )
305+ pivot += m_Plane . normal * size . y * .5f ;
306+
307+ m_DuplicateGO . transform . position = pivot ;
308+ m_DuplicateGO . transform . rotation = Quaternion . LookRotation ( m_PlaneForward , m_Plane . normal ) ;
309+
310+ DrawBoundingBox ( false ) ;
295311 }
296312
297313 void RecalculateBounds ( )
@@ -343,7 +359,7 @@ internal void RebuildShape()
343359 m_IsShapeInit = true ;
344360 }
345361
346- m_ProBuilderShape . Rebuild ( m_Bounds , m_PlaneRotation ) ;
362+ m_ProBuilderShape . Rebuild ( m_Bounds , m_PlaneRotation , m_BB_Origin ) ;
347363 ProBuilderEditor . Refresh ( false ) ;
348364
349365 SceneView . RepaintAll ( ) ;
@@ -390,8 +406,6 @@ internal void DrawBoundingBox(bool drawCorners = true)
390406
391407 void OnOverlayGUI ( UObject overlayTarget , SceneView view )
392408 {
393- EditorGUILayout . HelpBox ( L10n . Tr ( "Click and drag to place and scale the shape, or SHIFT+click once to duplicate last size settings." ) , MessageType . Info ) ;
394-
395409 DrawShapeGUI ( ) ;
396410
397411#if ! UNITY_2021_1_OR_NEWER
@@ -463,6 +477,7 @@ void DrawShapeGUI()
463477 m_LastShapeCreated = null ;
464478
465479 UndoUtility . RegisterCompleteObjectUndo ( currentShapeInOverlay , "Change Shape" ) ;
480+ Selection . activeObject = null ;
466481 currentShapeInOverlay . SetShape ( EditorShapeUtility . CreateShape ( type ) , currentShapeInOverlay . pivotLocation ) ;
467482 SetBounds ( currentShapeInOverlay . size ) ;
468483
0 commit comments