8
8
// </author>
9
9
// --------------------------------------------------------------------------------------------------------------------
10
10
11
+ using System ;
11
12
using System . Collections . Generic ;
12
13
using UnityEngine ;
14
+ using UnityEngine . SceneManagement ;
13
15
14
16
namespace Supyrb
15
17
{
@@ -30,6 +32,10 @@ public class ObjectSpawner : MonoBehaviour
30
32
[ SerializeField ]
31
33
private int maxInstances = 200 ;
32
34
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
+
33
39
public float SpawnCoolDownSeconds
34
40
{
35
41
get => spawnCoolDownSeconds ;
@@ -102,12 +108,35 @@ private void SpawnObject()
102
108
return ;
103
109
}
104
110
105
- var newGo = Instantiate ( prefab , transform . position , transform . rotation ) ;
111
+ var newGo = InstantiatePrefab ( ) ;
106
112
spawnedObjects . Enqueue ( newGo ) ;
107
113
totalSpawnCount ++ ;
108
114
}
109
115
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
111
140
private void OnDrawGizmos ( )
112
141
{
113
142
Gizmos . DrawWireSphere ( transform . position , 0.5f ) ;
0 commit comments