Skip to content

Commit eb2face

Browse files
committed
Change spawn prefabs in own scene to make the hierarchy more usable in the editor
1 parent 03e269c commit eb2face

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Assets/Plugins/WebGL/WebTools/WebToolPlugins.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public static void AddFpsTrackingEvent(float fps)
8181
#if UNITY_WEBGL && !UNITY_EDITOR
8282
_AddFpsTrackingEvent(fps);
8383
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
84-
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(AddFpsTrackingEvent)} called with {fps:0.00}");
84+
// This is called often, so it can spam the console, uncomment if needed
85+
//Debug.Log($"{nameof(WebToolPlugins)}.{nameof(AddFpsTrackingEvent)} called with {fps:0.00}");
8586
#endif
8687
}
8788

Assets/Prefabs/Spawner.prefab

+1
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ MonoBehaviour:
4848
spawnCoolDownSeconds: 0.4
4949
spawnOffsetSeconds: 0
5050
maxInstances: 1000
51+
spawnSceneName: Cubes

Assets/Scripts/ObjectSpawner.cs

+31-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
// </author>
99
// --------------------------------------------------------------------------------------------------------------------
1010

11+
using System;
1112
using System.Collections.Generic;
1213
using UnityEngine;
14+
using UnityEngine.SceneManagement;
1315

1416
namespace 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

Comments
 (0)