Skip to content

Commit f302adc

Browse files
author
Christopher-Marcel Böddecker
committed
fix(SDKManager): SDK Setup load/unload crashes on scene changes
Whenever the scene changed it was recommended to unload the currently loaded SDK Setup via the SDK Manager. This lead to issues with coming up with the exact time to call the unload method, especially when using `SteamVR_LoadLevel`. That SteamVR script even lead to crashes. This fix stops unloading the currently used VR Device everywhere. The result is that **there is no need to call the unload method manually anymore**. The SceneChanger script was simplified a bit after being updated for this fix.
1 parent 0676113 commit f302adc

File tree

3 files changed

+61
-50
lines changed

3 files changed

+61
-50
lines changed

Assets/VRTK/Examples/ExampleResources/Scripts/SceneChanger.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private void Awake()
1515
DynamicGI.UpdateEnvironment();
1616
}
1717

18-
private bool ForwardPressed()
18+
private bool IsForwardPressed()
1919
{
2020
if (!VRTK_ControllerReference.IsValid(controllerReference))
2121
{
@@ -31,7 +31,7 @@ private bool ForwardPressed()
3131
return false;
3232
}
3333

34-
private bool BackPressed()
34+
private bool IsBackPressed()
3535
{
3636
if (!VRTK_ControllerReference.IsValid(controllerReference))
3737
{
@@ -57,27 +57,29 @@ private void Update()
5757
{
5858
GameObject rightHand = VRTK_DeviceFinder.GetControllerRightHand(true);
5959
controllerReference = VRTK_ControllerReference.GetControllerReference(rightHand);
60-
if (ForwardPressed() || Input.GetKeyUp(KeyCode.Space))
60+
61+
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
62+
int nextSceneIndex = currentSceneIndex;
63+
64+
if (IsForwardPressed() || Input.GetKeyUp(KeyCode.Space))
6165
{
62-
int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
66+
nextSceneIndex++;
6367
if (nextSceneIndex >= SceneManager.sceneCountInBuildSettings)
64-
{
6568
nextSceneIndex = 0;
66-
}
67-
VRTK_SDKManager.instance.UnloadSDKSetup();
68-
SceneManager.LoadScene(nextSceneIndex);
69+
}
70+
else if (IsBackPressed() || Input.GetKeyUp(KeyCode.Backspace))
71+
{
72+
nextSceneIndex--;
73+
if (nextSceneIndex < 0)
74+
nextSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
6975
}
7076

71-
if (BackPressed() || Input.GetKeyUp(KeyCode.Backspace))
77+
if (nextSceneIndex == currentSceneIndex)
7278
{
73-
int previousSceneIndex = SceneManager.GetActiveScene().buildIndex - 1;
74-
if (previousSceneIndex < 0)
75-
{
76-
previousSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
77-
}
78-
VRTK_SDKManager.instance.UnloadSDKSetup();
79-
SceneManager.LoadScene(previousSceneIndex);
79+
return;
8080
}
81+
82+
SceneManager.LoadScene(nextSceneIndex);
8183
}
8284
}
8385
}

Assets/VRTK/Scripts/Utilities/SDK/VRTK_SDKManager.cs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,43 @@ public void RemoveBehaviourToToggleOnLoadedSetupChange(Behaviour behaviour)
384384
_behavioursToToggleOnLoadedSetupChange.Remove(behaviour);
385385
}
386386

387+
/// <summary>
388+
/// Tries to load a valid <see cref="VRTK_SDKSetup"/> from <see cref="setups"/>.
389+
/// </summary>
390+
public void TryLoadSDKSetupFromList()
391+
{
392+
int index = 0;
393+
394+
if (VRSettings.enabled)
395+
{
396+
// Use the SDK Setup for the current VR Device if it's working already
397+
// (may be due to command line argument '-vrmode')
398+
index = Array.FindIndex(
399+
setups,
400+
setup => setup.usedVRDeviceNames.Contains(VRSettings.loadedDeviceName)
401+
);
402+
}
403+
else
404+
{
405+
// If '-vrmode none' was used try to load the respective SDK Setup
406+
string[] commandLineArgs = Environment.GetCommandLineArgs();
407+
int commandLineArgIndex = Array.IndexOf(commandLineArgs, "-vrmode", 1);
408+
if (VRSettings.loadedDeviceName == "None"
409+
|| (commandLineArgIndex != -1
410+
&& commandLineArgIndex + 1 < commandLineArgs.Length
411+
&& commandLineArgs[commandLineArgIndex + 1].ToLowerInvariant() == "none"))
412+
{
413+
index = Array.FindIndex(
414+
setups,
415+
setup => setup.usedVRDeviceNames.All(vrDeviceName => vrDeviceName == "None")
416+
);
417+
}
418+
}
419+
420+
index = index == -1 ? 0 : index;
421+
TryLoadSDKSetup(index, false, setups.ToArray());
422+
}
423+
387424
/// <summary>
388425
/// Tries to load a valid <see cref="VRTK_SDKSetup"/> from a list.
389426
/// </summary>
@@ -492,7 +529,7 @@ public void SetLoadedSDKSetupToPopulateObjectReferences(VRTK_SDKSetup setup)
492529
/// Unloads the currently loaded <see cref="VRTK_SDKSetup"/>, if there is one.
493530
/// </summary>
494531
/// <param name="disableVR">Whether to disable VR altogether after unloading the SDK Setup.</param>
495-
public void UnloadSDKSetup(bool disableVR = true)
532+
public void UnloadSDKSetup(bool disableVR = false)
496533
{
497534
if (loadedSetup != null)
498535
{
@@ -538,43 +575,15 @@ private void OnEnable()
538575

539576
if (autoLoadSetup)
540577
{
541-
int index = 0;
542-
543-
if (VRSettings.enabled)
544-
{
545-
// Use the SDK Setup for the current VR Device if it's working already
546-
// (may be due to command line argument '-vrmode')
547-
index = Array.FindIndex(
548-
setups,
549-
setup => setup.usedVRDeviceNames.Contains(VRSettings.loadedDeviceName)
550-
);
551-
}
552-
else
553-
{
554-
// If '-vrmode none' was used try to load the respective SDK Setup
555-
string[] commandLineArgs = Environment.GetCommandLineArgs();
556-
int commandLineArgIndex = Array.IndexOf(commandLineArgs, "-vrmode", 1);
557-
if (commandLineArgIndex != -1
558-
&& commandLineArgIndex + 1 < commandLineArgs.Length
559-
&& commandLineArgs[commandLineArgIndex + 1].ToLowerInvariant() == "none")
560-
{
561-
index = Array.FindIndex(
562-
setups,
563-
setup => setup.usedVRDeviceNames.All(vrDeviceName => vrDeviceName == "None")
564-
);
565-
}
566-
}
567-
568-
index = index == -1 ? 0 : index;
569-
TryLoadSDKSetup(index, false, setups.ToArray());
578+
TryLoadSDKSetupFromList();
570579
}
571580
}
572581

573582
private void OnDisable()
574583
{
575-
if (_instance == this && !persistOnLoad)
576-
{
577-
UnloadSDKSetup();
584+
if (_instance == this && !persistOnLoad)
585+
{
586+
UnloadSDKSetup();
578587
}
579588
}
580589

Assets/VRTK/Scripts/Utilities/SDK/VRTK_SDKSetupSwitcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ protected virtual void AddSelectionButtons()
133133
chooseNoneButton.SetActive(true);
134134

135135
chooseNoneButton.GetComponent<Button>().onClick.AddListener(
136-
() => sdkManager.UnloadSDKSetup()
136+
() => sdkManager.UnloadSDKSetup(true)
137137
);
138138

139139
chooseButtonGameObjects.Add(chooseNoneButton);

0 commit comments

Comments
 (0)