88// </author>
99// --------------------------------------------------------------------------------------------------------------------
1010
11+ using System ;
1112using System . Collections . Generic ;
1213using UnityEngine ;
14+ using UnityEngine . SceneManagement ;
1315
1416namespace Supyrb
1517{
@@ -30,6 +32,10 @@ public class ObjectSpawner : MonoBehaviour
3032 [ SerializeField ]
3133 private int maxInstances = 200 ;
3234
35+ [ SerializeField ]
36+ [ Tooltip ( "If set, the object will be spawned in the scene with the given name" ) ]
37+ private string spawnSceneName = string . Empty ;
38+
3339 public float SpawnCoolDownSeconds
3440 {
3541 get => spawnCoolDownSeconds ;
@@ -102,12 +108,35 @@ private void SpawnObject()
102108 return ;
103109 }
104110
105- var newGo = Instantiate ( prefab , transform . position , transform . rotation ) ;
111+ var newGo = InstantiatePrefab ( ) ;
106112 spawnedObjects . Enqueue ( newGo ) ;
107113 totalSpawnCount ++ ;
108114 }
109115
110- #if UNITY_EDITOR
116+ private GameObject InstantiatePrefab ( )
117+ {
118+ var lastActiveScene = SceneManager . GetActiveScene ( ) ;
119+ if ( ! string . IsNullOrEmpty ( spawnSceneName ) )
120+ {
121+ var spawnScene = SceneManager . GetSceneByName ( spawnSceneName ) ;
122+ if ( ! spawnScene . IsValid ( ) )
123+ {
124+ spawnScene = SceneManager . CreateScene ( spawnSceneName ) ;
125+ }
126+ SceneManager . SetActiveScene ( spawnScene ) ;
127+ }
128+
129+ var newGo = Instantiate ( prefab , transform . position , transform . rotation ) ;
130+
131+ if ( ! string . IsNullOrEmpty ( spawnSceneName ) )
132+ {
133+ SceneManager . SetActiveScene ( lastActiveScene ) ;
134+ }
135+
136+ return newGo ;
137+ }
138+
139+ #if UNITY_EDITOR
111140 private void OnDrawGizmos ( )
112141 {
113142 Gizmos . DrawWireSphere ( transform . position , 0.5f ) ;
0 commit comments