Skip to content

Commit

Permalink
feat(SDKManager): simplify single SDK Setup configurations
Browse files Browse the repository at this point in the history
Supporting multiple SDKs means VRTK had to disable all the SDK
Setups that are used automatically to ensure they're usable for
runtime switching. This change allows to completely bypass the
runtime loading and that requirement in case only one SDK Setup is
used. This means users of VRTK don't have to bother with handling
when an SDK Setup is loaded and usable, nor do they have to
re-enable their SDK Setups game objects again to work on them in the
editor while they're visible.

The SteamVR SDK Setup in the SDKSetups prefab is changed to allow
for this new behavior. This also fixes a bug that resulted in
invalid matrix errors being logged to the console by Unity.
  • Loading branch information
Christopher-Marcel Böddecker committed Jul 15, 2017
1 parent 90ddff1 commit 8fdf63c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 91 deletions.
54 changes: 28 additions & 26 deletions Assets/VRTK/Editor/VRTK_SDKSetupEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,37 +432,39 @@ private static string[] OnWillSaveAssets(string[] paths)

private static void FixOpenAndUnsavedScenes()
{
for (int index = 0; index < EditorSceneManager.loadedSceneCount; index++)

List<VRTK_SDKSetup> setups = Enumerable.Range(0, EditorSceneManager.loadedSceneCount)
.SelectMany(sceneIndex => SceneManager.GetSceneAt(sceneIndex).GetRootGameObjects())
.SelectMany(rootObject => rootObject.GetComponentsInChildren<VRTK_SDKManager>())
.Select(manager => manager.setups.Where(setup => setup != null).ToArray())
.Where(sdkSetups => sdkSetups.Length > 1)
.SelectMany(sdkSetups => sdkSetups)
.Where(setup => setup.gameObject.activeSelf)
.ToList();
if (setups.Count == 0)
{
Scene scene = SceneManager.GetSceneAt(index);
if (scene.isLoaded && scene.isDirty)
{
VRTK_SDKSetup[] setups = FindObjectsOfType<VRTK_SDKSetup>();
foreach (VRTK_SDKSetup setup in setups)
{
setup.gameObject.SetActive(false);
}
return;
}

if (setups.Length != 0)
{
string infoMessage = string.Format("The following game objects have been set inactive to allow for SDK loading and switching using the SDK Setups on them:\n{0}", string.Join(", ", setups.Select(setup => setup.name).ToArray()));
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
setups.ForEach(setup => setup.gameObject.SetActive(false));

string infoMessage = string.Format(
"The following game objects have been set inactive to allow for SDK loading and switching using the SDK Setups on them:\n{0}",
string.Join(", ", setups.Select(setup => setup.name).ToArray()));
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
#if UNITY_5_6_OR_NEWER
SessionState.SetString(
SessionState.SetString(
#else
EditorPrefs.SetString(
EditorPrefs.SetString(
#endif
PreferencesKey,
infoMessage
);
}
else
{
VRTK_Logger.Info(infoMessage);
}
}
}
PreferencesKey,
infoMessage
);
}
else
{
VRTK_Logger.Info(infoMessage);
}
}
}
Expand Down
71 changes: 18 additions & 53 deletions Assets/VRTK/Examples/ExampleResources/Prefabs/SDKSetups.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,7 @@ GameObject:
serializedVersion: 5
m_Component:
- component: {fileID: 4000010242415096}
- component: {fileID: 20000012316479598}
- component: {fileID: 114000011243730024}
- component: {fileID: 92000014137301068}
m_Layer: 0
m_Name: Camera (head)
m_TagString: MainCamera
Expand Down Expand Up @@ -1578,7 +1576,8 @@ GameObject:
- component: {fileID: 4000012660741642}
- component: {fileID: 20000011176388890}
- component: {fileID: 124000012904963108}
- component: {fileID: 114000012849300506}
- component: {fileID: 92569260401678672}
- component: {fileID: 114085069478805808}
m_Layer: 0
m_Name: Camera (eye)
m_TagString: MainCamera
Expand Down Expand Up @@ -3427,40 +3426,6 @@ Camera:
m_StereoConvergence: 10
m_StereoSeparation: 0.022
m_StereoMirrorMode: 0
--- !u!20 &20000012316479598
Camera:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1000011840228368}
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 4
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0.019607844}
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: 0
far clip plane: 1
field of view: 60
orthographic: 1
orthographic size: 1
m_Depth: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 0
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 0
m_OcclusionCulling: 0
m_StereoConvergence: 10
m_StereoSeparation: 0.022
m_StereoMirrorMode: 0
--- !u!20 &20000013040785452
Camera:
m_ObjectHideFlags: 1
Expand Down Expand Up @@ -5053,12 +5018,12 @@ Behaviour:
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1000010426818160}
m_Enabled: 1
--- !u!92 &92000014137301068
--- !u!92 &92569260401678672
Behaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1000011840228368}
m_GameObject: {fileID: 1000014132296520}
m_Enabled: 1
--- !u!92 &92889010644976706
Behaviour:
Expand Down Expand Up @@ -5964,20 +5929,6 @@ MonoBehaviour:
m_EffectColor: {r: 0, g: 0, b: 0, a: 0.09803922}
m_EffectDistance: {x: 1, y: 1}
m_UseGraphicAlpha: 0
--- !u!114 &114000012849300506
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1000014132296520}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6bca9ccf900ccc84c887d783321d27e2, type: 3}
m_Name:
m_EditorClassIdentifier:
_head: {fileID: 4000010242415096}
_ears: {fileID: 4000013003015800}
wireframe: 0
--- !u!114 &114000013231417920
MonoBehaviour:
m_ObjectHideFlags: 1
Expand Down Expand Up @@ -6426,6 +6377,20 @@ MonoBehaviour:
m_PreferredHeight: -1
m_FlexibleWidth: -1
m_FlexibleHeight: -1
--- !u!114 &114085069478805808
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1000014132296520}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6bca9ccf900ccc84c887d783321d27e2, type: 3}
m_Name:
m_EditorClassIdentifier:
_head: {fileID: 4000010242415096}
_ears: {fileID: 4000013003015800}
wireframe: 0
--- !u!114 &114139617658199918
MonoBehaviour:
m_ObjectHideFlags: 1
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRTK/SDK/Simulator/SDK_SimController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,14 @@ protected virtual bool IsButtonPressed(uint index, ButtonPressTypes type, KeyCod

if (index == 1)
{
if (!rightController.Selected)
if (rightController == null || !rightController.Selected)
{
return false;
}
}
else if (index == 2)
{
if (!leftController.Selected)
if (leftController == null || !leftController.Selected)
{
return false;
}
Expand Down
49 changes: 40 additions & 9 deletions Assets/VRTK/Scripts/Utilities/SDK/VRTK_SDKManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,20 @@ public static VRTK_SDKManager instance
/// <summary>
/// The loaded SDK Setup. <see langword="null"/> if no setup is currently loaded.
/// </summary>
public VRTK_SDKSetup loadedSetup { get; private set; }
public VRTK_SDKSetup loadedSetup
{
get
{
if (_loadedSetup == null && setups.Length == 1 && setups[0].isValid && setups[0].isActiveAndEnabled)
{
_loadedSetup = setups[0];
}

return _loadedSetup;
}
private set { _loadedSetup = value; }
}
private VRTK_SDKSetup _loadedSetup;
private static HashSet<VRTK_SDKInfo> _previouslyUsedSetupInfos = new HashSet<VRTK_SDKInfo>();

/// <summary>
Expand Down Expand Up @@ -332,28 +345,46 @@ public void ManageVRSettings(bool force)
.ToDictionary(grouping => grouping.Key,
grouping => grouping.Select(info => info.description.vrDeviceName)
.Distinct()
.Except(new[] { "None" })
.ToArray());

foreach (BuildTargetGroup targetGroup in VRTK_SharedMethods.GetValidBuildTargetGroups())
{
string[] deviceNames;
deviceNamesByTargetGroup.TryGetValue(targetGroup, out deviceNames);

int setupCount = deviceNames == null ? 0 : deviceNames.Length;
bool vrEnabled = deviceNames != null && deviceNames.Length > 0;

if (deviceNames != null)
{
deviceNames = deviceNames.Except(new[] { "None" }).ToArray();
}

#if UNITY_5_5_OR_NEWER
VREditor.SetVREnabledOnTargetGroup(targetGroup, vrEnabled);
#else
VREditor.SetVREnabled(targetGroup, vrEnabled);
#endif

string[] devices;
if (vrEnabled)
{
devices = setupCount > 1
? new[] { "None" }.Concat(deviceNames).ToArray()
: deviceNames;
}
else
{
devices = new string[0];
}

#if UNITY_5_5_OR_NEWER
VREditor.SetVREnabledDevicesOnTargetGroup(
#else
VREditor.SetVREnabledDevices(
#endif
targetGroup,
vrEnabled ? new[] { "None" }.Concat(deviceNames).ToArray() : new string[0]
devices
);
}
}
Expand Down Expand Up @@ -483,18 +514,18 @@ public void TryLoadSDKSetup(int startIndex, bool tryToReinitialize, params VRTK_
previousLoadedSetup.OnUnloaded(this);
}

bool isDeviceAlreadyLoaded = VRSettings.enabled
&& sdkSetups[0].usedVRDeviceNames.Contains(VRSettings.loadedDeviceName);
string loadedDeviceName = string.IsNullOrEmpty(VRSettings.loadedDeviceName) ? "None" : VRSettings.loadedDeviceName;
bool isDeviceAlreadyLoaded = sdkSetups[0].usedVRDeviceNames.Contains(loadedDeviceName);
if (!isDeviceAlreadyLoaded)
{
if (!tryToReinitialize && !VRSettings.enabled && !string.IsNullOrEmpty(VRSettings.loadedDeviceName))
if (!tryToReinitialize && !VRSettings.enabled && !string.IsNullOrEmpty(loadedDeviceName))
{
sdkSetups = sdkSetups.Where(setup => !setup.usedVRDeviceNames.Contains(VRSettings.loadedDeviceName))
sdkSetups = sdkSetups.Where(setup => !setup.usedVRDeviceNames.Contains(loadedDeviceName))
.ToArray();
}

VRTK_SDKSetup[] missingVRDeviceSetups = sdkSetups
.Where(setup => setup.usedVRDeviceNames.Except(VRSettings.supportedDevices).Any())
.Where(setup => setup.usedVRDeviceNames.Except(VRSettings.supportedDevices.Concat(new[] { "None" })).Any())
.ToArray();
foreach (VRTK_SDKSetup missingVRDeviceSetup in missingVRDeviceSetups)
{
Expand Down Expand Up @@ -603,7 +634,7 @@ private void OnEnable()

CreateInstance();

if (autoLoadSetup)
if (loadedSetup == null && autoLoadSetup)
{
TryLoadSDKSetupFromList();
}
Expand Down
5 changes: 4 additions & 1 deletion Assets/VRTK/Scripts/Utilities/SDK/VRTK_SDKSetupSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace VRTK
using UnityEngine.UI;
using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// The SDK Setup Switcher adds a GUI overlay to allow switching the loaded VRTK_SDKSetup of the the current VRTK_SDKManager.
Expand Down Expand Up @@ -112,7 +113,9 @@ protected virtual void Show(ViewingState viewingState)
return;
}

fallbackCamera.gameObject.SetActive(VRTK_DeviceFinder.HeadsetCamera() == null);
bool isAnyOtherCameraUsed = sdkManager.setups.Any(setup => setup != null && setup.gameObject.activeSelf)
|| VRTK_DeviceFinder.HeadsetCamera() != null;
fallbackCamera.gameObject.SetActive(!isAnyOtherCameraUsed);
eventSystem.gameObject.SetActive(EventSystem.current == null || EventSystem.current == eventSystem);
}

Expand Down

0 comments on commit 8fdf63c

Please sign in to comment.