Skip to content

Commit

Permalink
fix(Examples): ensure constructor scene is available in build settings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thestonefox committed Mar 12, 2018
1 parent d481b95 commit 7975664
Showing 1 changed file with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
namespace VRTK.Examples.Utilities
{
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.SceneManagement;

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

protected VRTK_SDKSetupSwitcher setupSwitcher;
protected int constructorSceneIndex;
protected string constructorPath = "Assets/VRTK/Examples/VRTK_SDKManager_Constructor.unity";

protected virtual void Awake()
{
constructorSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
ToggleScriptAlias(false);
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.LoadScene(constructorSceneIndex, LoadSceneMode.Additive);
if (!Application.isPlaying && Application.isEditor)
{
ManageBuildSettings();
}
else
{
constructorSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
ToggleScriptAlias(false);
SceneManager.sceneLoaded += OnSceneLoaded;
if (sceneConstructor != null)
{
SceneManager.LoadScene(sceneConstructor.name, LoadSceneMode.Additive);
}
else
{
SceneManager.LoadScene(constructorSceneIndex, LoadSceneMode.Additive);
}
}
}

protected virtual void ManageBuildSettings()
{
#if UNITY_EDITOR
EditorBuildSettingsScene[] currentBuildScenes = EditorBuildSettings.scenes;
if (currentBuildScenes.Length == 0 || currentBuildScenes[currentBuildScenes.Length - 1].path != constructorPath)
{
EditorBuildSettingsScene[] newBuildScenes = new EditorBuildSettingsScene[currentBuildScenes.Length + 1];
System.Array.Copy(currentBuildScenes, newBuildScenes, currentBuildScenes.Length);
EditorBuildSettingsScene sceneToAdd = new EditorBuildSettingsScene(constructorPath, true);
newBuildScenes[newBuildScenes.Length - 1] = sceneToAdd;
EditorBuildSettings.scenes = newBuildScenes;
}
#endif
}

protected virtual void LateUpdate()
Expand All @@ -35,7 +71,7 @@ protected virtual void LateUpdate()

protected virtual void OnSceneLoaded(Scene loadedScene, LoadSceneMode loadMode)
{
if (loadedScene.buildIndex == constructorSceneIndex)
if (IsConstructorScene(loadedScene))
{
VRTK_SDKManager sdkManager = FindObjectOfType<VRTK_SDKManager>();
sdkManager.gameObject.SetActive(false);
Expand All @@ -54,6 +90,11 @@ protected virtual void OnSceneLoaded(Scene loadedScene, LoadSceneMode loadMode)
}
}

protected virtual bool IsConstructorScene(Scene checkScene)
{
return (checkScene != null && ((sceneConstructor != null && checkScene.name == sceneConstructor.name) || (sceneConstructor == null && checkScene.buildIndex == constructorSceneIndex)));
}

protected virtual void ToggleScriptAlias(bool state)
{
if (leftScriptAlias != null)
Expand Down

0 comments on commit 7975664

Please sign in to comment.