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 ;
@@ -96,23 +95,39 @@ void OnEnable()
9695 for ( int i = 0 ; i < s_ArrowsLines . Length ; i ++ )
9796 s_ArrowsLines [ i ] = new Vector3 [ 3 ] ;
9897
98+ m_ShapeEditor = Editor . CreateEditor ( targets . ToArray ( ) , typeof ( ProBuilderShapeEditor ) ) ;
99+ EditorApplication . playModeStateChanged += PlaymodeStateChanged ;
100+
99101#if ! UNITY_2020_2_OR_NEWER
100102 ToolManager . activeToolChanging += ActiveToolChanging ;
101- #endif
102103 ProBuilderEditor . selectModeChanged += OnSelectModeChanged ;
103-
104+ #endif
104105 }
105106
106107 void OnDisable ( )
107108 {
108109#if ! UNITY_2020_2_OR_NEWER
109110 ToolManager . activeToolChanging -= ActiveToolChanging ;
110- #endif
111111 ProBuilderEditor . selectModeChanged -= OnSelectModeChanged ;
112+ #endif
113+ EditorApplication . playModeStateChanged -= PlaymodeStateChanged ;
114+
112115 if ( m_ShapeEditor != null )
113116 DestroyImmediate ( m_ShapeEditor ) ;
114117 }
115118
119+ void PlaymodeStateChanged ( PlayModeStateChange stateChange )
120+ {
121+ if ( stateChange == PlayModeStateChange . ExitingEditMode
122+ || stateChange == PlayModeStateChange . ExitingPlayMode )
123+ return ;
124+
125+ if ( m_ShapeEditor != null )
126+ DestroyImmediate ( m_ShapeEditor ) ;
127+
128+ m_ShapeEditor = Editor . CreateEditor ( targets . ToArray ( ) , typeof ( ProBuilderShapeEditor ) ) ;
129+ }
130+
116131#if ! UNITY_2020_2_OR_NEWER
117132 void ActiveToolChanging ( )
118133 {
@@ -130,13 +145,21 @@ void ChangeToObjectMode()
130145 public override void OnActivated ( )
131146 {
132147 base . OnActivated ( ) ;
148+ ProBuilderEditor . selectModeChanged += OnSelectModeChanged ;
133149 EditorApplication . delayCall += ( ) => ProBuilderEditor . selectMode = SelectMode . Object ;
134150 }
135151
136152 public override void OnWillBeDeactivated ( )
137153 {
138154 base . OnWillBeDeactivated ( ) ;
139- EditorApplication . delayCall += ( ) => ProBuilderEditor . ResetToLastSelectMode ( ) ;
155+ ProBuilderEditor . selectModeChanged -= OnSelectModeChanged ;
156+ EditorApplication . delayCall += ( ) => ResetToLastSelectMode ( ) ;
157+ }
158+
159+ public void ResetToLastSelectMode ( )
160+ {
161+ if ( ProBuilderToolManager . activeTool != Tool . Custom )
162+ ProBuilderEditor . ResetToLastSelectMode ( ) ;
140163 }
141164#endif
142165
@@ -177,8 +200,6 @@ void OnOverlayGUI(Object obj, SceneView view)
177200 EditorSnapSettings . gridSnapEnabled = EditorGUILayout . Toggle ( "Grid Snapping" , EditorSnapSettings . gridSnapEnabled ) ;
178201 }
179202#endif
180- Editor . CreateCachedEditor ( targets . ToArray ( ) , typeof ( ProBuilderShapeEditor ) , ref m_ShapeEditor ) ;
181-
182203 using ( new EditorGUILayout . VerticalScope ( new GUIStyle ( EditorStyles . frameBox ) ) )
183204 {
184205 ( ( ProBuilderShapeEditor ) m_ShapeEditor ) . DrawShapeGUI ( null ) ;
0 commit comments