Skip to content

Commit 7975664

Browse files
committed
fix(Examples): ensure constructor scene is available in build settings
Previously, the constructor scene was removed from being injected into the example scenes as it wasn't working in a build, instead the constructor scene was required to be the last scene in the build settings and was loaded in by index. This only worked if using the project settings from the VRTK repo but would not work if VRTK was imported into another project. To remedy this, the scene injection is now being used again and the last scene index is being used as a fallback. If no scene is in the build settings then the script will attempt to add it as the last scene automatically.
1 parent d481b95 commit 7975664

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

Assets/VRTK/Examples/ExampleResources/SharedResources/Scripts/VRTKExample_AdditiveSceneLoader.cs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
namespace VRTK.Examples.Utilities
22
{
3+
#if UNITY_EDITOR
4+
using UnityEditor;
5+
#endif
36
using UnityEngine;
47
using UnityEngine.SceneManagement;
58

9+
[ExecuteInEditMode]
610
public class VRTKExample_AdditiveSceneLoader : MonoBehaviour
711
{
12+
[Tooltip("The constructor scene containing the VRTK SDK Manager setup to load into the scene.")]
13+
public Object sceneConstructor;
814
[Tooltip("The GameObject to inject into the VRTK SDK Manager as the Left Controller Script Alias.")]
915
public GameObject leftScriptAlias;
1016
[Tooltip("The GameObject to inject into the VRTK SDK Manager as the Right Controller Script Alias.")]
@@ -16,13 +22,43 @@ public class VRTKExample_AdditiveSceneLoader : MonoBehaviour
1622

1723
protected VRTK_SDKSetupSwitcher setupSwitcher;
1824
protected int constructorSceneIndex;
25+
protected string constructorPath = "Assets/VRTK/Examples/VRTK_SDKManager_Constructor.unity";
1926

2027
protected virtual void Awake()
2128
{
22-
constructorSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
23-
ToggleScriptAlias(false);
24-
SceneManager.sceneLoaded += OnSceneLoaded;
25-
SceneManager.LoadScene(constructorSceneIndex, LoadSceneMode.Additive);
29+
if (!Application.isPlaying && Application.isEditor)
30+
{
31+
ManageBuildSettings();
32+
}
33+
else
34+
{
35+
constructorSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
36+
ToggleScriptAlias(false);
37+
SceneManager.sceneLoaded += OnSceneLoaded;
38+
if (sceneConstructor != null)
39+
{
40+
SceneManager.LoadScene(sceneConstructor.name, LoadSceneMode.Additive);
41+
}
42+
else
43+
{
44+
SceneManager.LoadScene(constructorSceneIndex, LoadSceneMode.Additive);
45+
}
46+
}
47+
}
48+
49+
protected virtual void ManageBuildSettings()
50+
{
51+
#if UNITY_EDITOR
52+
EditorBuildSettingsScene[] currentBuildScenes = EditorBuildSettings.scenes;
53+
if (currentBuildScenes.Length == 0 || currentBuildScenes[currentBuildScenes.Length - 1].path != constructorPath)
54+
{
55+
EditorBuildSettingsScene[] newBuildScenes = new EditorBuildSettingsScene[currentBuildScenes.Length + 1];
56+
System.Array.Copy(currentBuildScenes, newBuildScenes, currentBuildScenes.Length);
57+
EditorBuildSettingsScene sceneToAdd = new EditorBuildSettingsScene(constructorPath, true);
58+
newBuildScenes[newBuildScenes.Length - 1] = sceneToAdd;
59+
EditorBuildSettings.scenes = newBuildScenes;
60+
}
61+
#endif
2662
}
2763

2864
protected virtual void LateUpdate()
@@ -35,7 +71,7 @@ protected virtual void LateUpdate()
3571

3672
protected virtual void OnSceneLoaded(Scene loadedScene, LoadSceneMode loadMode)
3773
{
38-
if (loadedScene.buildIndex == constructorSceneIndex)
74+
if (IsConstructorScene(loadedScene))
3975
{
4076
VRTK_SDKManager sdkManager = FindObjectOfType<VRTK_SDKManager>();
4177
sdkManager.gameObject.SetActive(false);
@@ -54,6 +90,11 @@ protected virtual void OnSceneLoaded(Scene loadedScene, LoadSceneMode loadMode)
5490
}
5591
}
5692

93+
protected virtual bool IsConstructorScene(Scene checkScene)
94+
{
95+
return (checkScene != null && ((sceneConstructor != null && checkScene.name == sceneConstructor.name) || (sceneConstructor == null && checkScene.buildIndex == constructorSceneIndex)));
96+
}
97+
5798
protected virtual void ToggleScriptAlias(bool state)
5899
{
59100
if (leftScriptAlias != null)

0 commit comments

Comments
 (0)