@@ -129,6 +129,11 @@ public partial class OceanPlanarReflection : CustomMonoBehaviour
129129 const int CULL_DISTANCE_COUNT = 32 ;
130130 float [ ] _cullDistances = new float [ CULL_DISTANCE_COUNT ] ;
131131
132+ #if UNITY_EDITOR
133+ bool _isSceneCamera ;
134+ float _changeViewTimer ;
135+ #endif
136+
132137 private void Start ( )
133138 {
134139 if ( OceanRenderer . Instance == null )
@@ -184,6 +189,7 @@ void LateUpdate()
184189 if ( ! editorCamera . TryGetComponent < OceanPlanarReflection > ( out var editor ) )
185190 {
186191 editor = editorCamera . gameObject . AddComponent < OceanPlanarReflection > ( ) ;
192+ editor . _isSceneCamera = true ;
187193 }
188194
189195 if ( editor != null )
@@ -212,6 +218,34 @@ void LateUpdate()
212218 return ;
213219 }
214220
221+ #if UNITY_EDITOR
222+ // Fix "Screen position out of view frustum" when 2D view activated.
223+ // Always had an exception on exiting 2D mode, so resorted to a timer.
224+ if ( _isSceneCamera )
225+ {
226+ var sceneView = UnityEditor . SceneView . lastActiveSceneView ;
227+
228+ if ( sceneView == null )
229+ {
230+ _camReflections . enabled = false ;
231+ return ;
232+ }
233+
234+ if ( sceneView . in2DMode )
235+ {
236+ _changeViewTimer = 1f ;
237+ }
238+
239+ _camReflections . enabled = _changeViewTimer <= 0f ;
240+
241+ if ( ! _camReflections . enabled )
242+ {
243+ _changeViewTimer -= 0.02f ;
244+ return ;
245+ }
246+ }
247+ #endif
248+
215249 // Find out the reflection plane: position and normal in world space
216250 Vector3 planePos = OceanRenderer . Instance . Root . position ;
217251 Vector3 planeNormal = Vector3 . up ;
@@ -423,17 +457,19 @@ private void OnDisable()
423457 PreparedReflections . Remove ( _camViewpoint . GetHashCode ( ) ) ;
424458 }
425459
460+ if ( _camReflections )
461+ {
462+ _camReflections . targetTexture = null ;
463+ Helpers . Destroy ( _camReflections . gameObject ) ;
464+ _camReflections = null ;
465+ }
466+
426467 // Cleanup all the objects we possibly have created
427468 if ( _reflectionTexture )
428469 {
429470 Helpers . Destroy ( _reflectionTexture ) ;
430471 _reflectionTexture = null ;
431472 }
432- if ( _camReflections )
433- {
434- Helpers . Destroy ( _camReflections . gameObject ) ;
435- _camReflections = null ;
436- }
437473 }
438474 }
439475
0 commit comments