public static void OnRuntimeLoad() { #if !UNITY_EDITOR OpenVRManifest manifestSettingsAsset = LoadOpenVRManifestSettings(); if (manifestSettingsAsset == null) { Debug.Log("OpenVR manifest not configured!"); return; } if (!manifestSettingsAsset.isEnabled) { return; } // At this point, OpenVR may be already initialized (if SteamVR is enabled by default in the first scene) or not (if None is at top // of supported SDKs list, that is the case e.g. when using VRTK, or your first scene just has no VR enabled at all). // If it's not, we'll have to handle initialization outselves if (XRSettings.loadedDeviceName != "OpenVR") { EVRInitError error = EVRInitError.None; OpenVR.Init(ref error, EVRApplicationType.VRApplication_Utility); if (error != EVRInitError.None) { Debug.LogError("Failed to initialize OpenVR to register manifest, error = " + error.ToString()); return; } } string exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; // TODO: This is UGLY! Why doesn't Unity have this string exeDir = Path.GetDirectoryName(exePath); string exeNameBase = Path.GetFileNameWithoutExtension(exePath); string vrmanifestPath = Path.Combine(exeDir, exeNameBase + ".vrmanifest"); Debug.Log("Registering the OpenVR manifest from " + vrmanifestPath); OpenVR.Applications.AddApplicationManifest(vrmanifestPath, manifestSettingsAsset.temporaryManifest); // To make sure that the new settings get applied immediately // (without this it works only after app restart) // PS. OpenVR documentation sucks, it says that if you pass 0 instead of process id it will use the calling process automatically but it actually doesn't OpenVR.Applications.IdentifyApplication((uint)System.Diagnostics.Process.GetCurrentProcess().Id, manifestSettingsAsset.appKey); // And don't forget to clean up afterwards! if (XRSettings.loadedDeviceName != "OpenVR") { OpenVR.Shutdown(); } #endif }
public static void OpenSettings() { OpenVRManifest manifestSettingsAsset = LoadOpenVRManifestSettings(); if (manifestSettingsAsset == null) { if (!AssetDatabase.IsValidFolder("Assets/Resources")) { AssetDatabase.CreateFolder("Assets", "Resources"); } manifestSettingsAsset = ScriptableObject.CreateInstance <OpenVRManifest>(); AssetDatabase.CreateAsset(manifestSettingsAsset, "Assets/Resources/" + MANIFEST_SETTINGS_ASSET_NAME + ".asset"); AssetDatabase.SaveAssets(); } Selection.activeObject = manifestSettingsAsset; }
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) { if (Array.IndexOf(XRSettings.supportedDevices, "OpenVR") < 0) { return; } OpenVRManifest manifestSettingsAsset = LoadOpenVRManifestSettings(); if (manifestSettingsAsset == null) { Debug.Log("OpenVR manifest not configured! Click Edit > Project Settings > OpenVR Manifest"); return; } if (manifestSettingsAsset.appKey == "") { Debug.LogWarning("App key cannot be empty. OpenVR manifest will not be generated. See Edit > Project Settings > OpenVR Manifest"); return; } if (manifestSettingsAsset.appImage != null && manifestSettingsAsset.appImageUrl != "") { Debug.LogWarning("Cannot use app image and app image URL at the same time. See Edit > Project Settings > OpenVR Manifest"); throw new UnityException("Cannot use app image and app image URL at the same time"); } string outputDirName = Path.GetDirectoryName(pathToBuiltProject); string programBinaryName = Path.GetFileName(pathToBuiltProject); string programBinaryNameBase = Path.GetFileNameWithoutExtension(pathToBuiltProject); // Put the app image inside output directory string appImage = ""; if (manifestSettingsAsset.appImageUrl != "") { appImage = manifestSettingsAsset.appImageUrl; } else if (manifestSettingsAsset.appImage) { if (manifestSettingsAsset.appImage.width != APP_IMAGE_RECOMMENDED_WIDTH || manifestSettingsAsset.appImage.width != APP_IMAGE_RECOMMENDED_HEIGHT) { Debug.LogWarning("The OpenVR app image is not using the recommended size of " + APP_IMAGE_RECOMMENDED_WIDTH + "x" + APP_IMAGE_RECOMMENDED_HEIGHT + ", but we'll use it anyway"); } // Make sure we can access the texture with EncodeToPNG TextureImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(manifestSettingsAsset.appImage)) as TextureImporter; if (importer) { bool dirty = false; if (!importer.isReadable) { Debug.Log("Texture " + manifestSettingsAsset.appImage + " did not have script read access enabled, enabling now..."); importer.isReadable = true; dirty = true; } if (importer.textureCompression != TextureImporterCompression.Uncompressed) { Debug.Log("Texture " + manifestSettingsAsset.appImage + " had compression enabled, disabling now..."); importer.textureCompression = TextureImporterCompression.Uncompressed; dirty = true; } if (importer.npotScale != TextureImporterNPOTScale.None) { Debug.Log("Texture " + manifestSettingsAsset.appImage + " had NPOT scaling enabled, disabling now..."); importer.npotScale = TextureImporterNPOTScale.None; dirty = true; } if (importer.mipmapEnabled) { Debug.Log("Texture " + manifestSettingsAsset.appImage + " had mipmaps enabled, disabling now..."); importer.mipmapEnabled = false; dirty = true; } if (dirty) { importer.SaveAndReimport(); } } else { Debug.LogWarning("Unable to access TextureImporter for " + manifestSettingsAsset.appImage.name + " ?"); } // And save the texture in build directory string appImageFilename = programBinaryNameBase + "_vrlogo.png"; string appImagePath = Path.Combine(outputDirName, appImageFilename); byte[] appImageData = manifestSettingsAsset.appImage.EncodeToPNG(); File.WriteAllBytes(appImagePath, appImageData); appImage = appImageFilename; } // Generate the manifest file OpenVRManifestData jsonData = new OpenVRManifestData() { applications = new List <OpenVRManifestData.OpenVRManifestApplicationData>() { new OpenVRManifestData.OpenVRManifestApplicationData() { app_key = manifestSettingsAsset.appKey, binary_path_windows = programBinaryName, arguments = manifestSettingsAsset.launchArguments, image_path = appImage, strings = new OpenVRManifestData.OpenVRManifestApplicationData.OpenVRManifestApplicationStringsArray() { en_us = new OpenVRManifestData.OpenVRManifestApplicationData.OpenVRManifestApplicationStringsData() { name = Application.productName } } } } }; string json = JsonUtility.ToJson(jsonData, true); string vrmanifestPath = Path.Combine(outputDirName, programBinaryNameBase + ".vrmanifest"); File.WriteAllText(vrmanifestPath, json); }
private string GetApplicationKey() { return(OpenVRManifest.LoadOpenVRManifestSettings().appKey); // "steam.overlay.732230" }