Esempio n. 1
0
        /// <summary>
        /// Manages (i.e. adds and removes) the VR SDKs of the <see cref="PlayerSettings"/> for the currently set SDK infos.
        /// This method is only available in the editor, so usage of the method needs to be surrounded by `#if UNITY_EDITOR` and `#endif` when used
        /// in a type that is also compiled for a standalone build.
        /// </summary>
        /// <param name="force">Whether to ignore <see cref="autoManageVRSettings"/> while deciding to manage.</param>
        public void ManageVRSettings(bool force)
        {
            if (EditorApplication.isPlayingOrWillChangePlaymode || !(force || autoManageVRSettings))
            {
                return;
            }

            Dictionary <BuildTargetGroup, string[]> deviceNamesByTargetGroup = setups
                                                                               .Where(setup => setup != null && setup.isValid)
                                                                               .SelectMany(setup => new[]
            {
                setup.systemSDKInfo, setup.boundariesSDKInfo, setup.headsetSDKInfo, setup.controllerSDKInfo
            })
                                                                               .GroupBy(info => info.description.buildTargetGroup)
                                                                               .ToDictionary(grouping => grouping.Key,
                                                                                             grouping => grouping.Select(info => info.description.vrDeviceName)
                                                                                             .Distinct()
                                                                                             .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,
                    devices
                    );
            }
        }