11using System . Linq ;
22using UnityEngine ;
33using UnityEditor . EditorTools ;
4- using UnityEditor . IMGUI . Controls ;
54using UnityEngine . ProBuilder ;
65using UnityEngine . ProBuilder . Shapes ;
76using Math = UnityEngine . ProBuilder . Math ;
@@ -97,11 +96,13 @@ void OnEnable()
9796 for ( int i = 0 ; i < s_ArrowsLines . Length ; i ++ )
9897 s_ArrowsLines [ i ] = new Vector3 [ 3 ] ;
9998
99+ m_ShapeEditor = Editor . CreateEditor ( targets . ToArray ( ) , typeof ( ProBuilderShapeEditor ) ) ;
100+ EditorApplication . playModeStateChanged += PlaymodeStateChanged ;
101+
100102#if ! UNITY_2020_2_OR_NEWER
101103 ToolManager . activeToolChanging += ActiveToolChanging ;
102104 ProBuilderEditor . selectModeChanged += OnSelectModeChanged ;
103105#endif
104-
105106 }
106107
107108 void OnDisable ( )
@@ -110,8 +111,22 @@ void OnDisable()
110111 ToolManager . activeToolChanging -= ActiveToolChanging ;
111112 ProBuilderEditor . selectModeChanged -= OnSelectModeChanged ;
112113#endif
114+ EditorApplication . playModeStateChanged -= PlaymodeStateChanged ;
115+
116+ if ( m_ShapeEditor != null )
117+ DestroyImmediate ( m_ShapeEditor ) ;
118+ }
119+
120+ void PlaymodeStateChanged ( PlayModeStateChange stateChange )
121+ {
122+ if ( stateChange == PlayModeStateChange . ExitingEditMode
123+ || stateChange == PlayModeStateChange . ExitingPlayMode )
124+ return ;
125+
113126 if ( m_ShapeEditor != null )
114127 DestroyImmediate ( m_ShapeEditor ) ;
128+
129+ m_ShapeEditor = Editor . CreateEditor ( targets . ToArray ( ) , typeof ( ProBuilderShapeEditor ) ) ;
115130 }
116131
117132#if ! UNITY_2020_2_OR_NEWER
@@ -186,8 +201,6 @@ void OnOverlayGUI(Object obj, SceneView view)
186201 EditorSnapSettings . gridSnapEnabled = EditorGUILayout . Toggle ( "Grid Snapping" , EditorSnapSettings . gridSnapEnabled ) ;
187202 }
188203#endif
189- Editor . CreateCachedEditor ( targets . ToArray ( ) , typeof ( ProBuilderShapeEditor ) , ref m_ShapeEditor ) ;
190-
191204 using ( new EditorGUILayout . VerticalScope ( new GUIStyle ( EditorStyles . frameBox ) ) )
192205 {
193206 ( ( ProBuilderShapeEditor ) m_ShapeEditor ) . DrawShapeGUI ( null ) ;
0 commit comments