OpenVRSettingsCopier.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEditor.Callbacks;
  6. using System.IO;
  7. using System;
  8. using System.Linq;
  9. #if UNITY_XR_MANAGEMENT
  10. using UnityEngine.XR;
  11. using UnityEngine.Experimental.XR;
  12. using UnityEngine.XR.Management;
  13. using UnityEditor.XR.Management;
  14. using UnityEditor.Build;
  15. using UnityEditor.Build.Reporting;
  16. #endif
  17. namespace Unity.XR.OpenVR.Editor
  18. {
  19. public class OpenVRSettingsCopier
  20. {
  21. private const string defaultAssetPath = "Assets/XR/Settings/Open VR Settings.asset";
  22. private static void CreatePath(string path)
  23. {
  24. string[] split = defaultAssetPath.Split('/');
  25. for (int splitIndex = 1; splitIndex < split.Length; splitIndex++)
  26. {
  27. string splitPath = string.Join("/", split, 0, splitIndex);
  28. if (AssetDatabase.IsValidFolder(splitPath) == false)
  29. {
  30. AssetDatabase.CreateFolder(string.Join("/", split, 0, splitIndex - 1), split[splitIndex-1]);
  31. Debug.Log("Created: " + splitPath);
  32. }
  33. }
  34. }
  35. [PostProcessBuildAttribute(1)]
  36. public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
  37. {
  38. //make sure we're on a reasonable target
  39. if (target != BuildTarget.StandaloneLinux64 && target != BuildTarget.StandaloneWindows && target != BuildTarget.StandaloneWindows64)
  40. return;
  41. #if UNITY_XR_MANAGEMENT
  42. //make sure we have the xr settings
  43. XRGeneralSettings generalSettings = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget));
  44. if (generalSettings == null)
  45. return;
  46. //make sure our loader is checked
  47. bool hasLoader = generalSettings.Manager.loaders.Any(loader => loader is OpenVRLoader);
  48. if (hasLoader == false)
  49. return;
  50. #endif
  51. OpenVRSettings settings = OpenVRSettings.GetSettings();
  52. bool saved = settings.InitializeActionManifestFileRelativeFilePath();
  53. string settingsAssetPath = AssetDatabase.GetAssetPath(settings);
  54. if (string.IsNullOrEmpty(settingsAssetPath))
  55. {
  56. CreatePath(defaultAssetPath);
  57. UnityEditor.AssetDatabase.CreateAsset(settings, defaultAssetPath);
  58. settingsAssetPath = AssetDatabase.GetAssetPath(settings);
  59. }
  60. FileInfo buildPath = new FileInfo(pathToBuiltProject);
  61. string buildName = buildPath.Name.Replace(buildPath.Extension, "");
  62. DirectoryInfo buildDirectory = buildPath.Directory;
  63. string dataDirectory = Path.Combine(buildDirectory.FullName, buildName + "_Data");
  64. if (Directory.Exists(dataDirectory) == false)
  65. {
  66. string vsDebugDataDirectory = Path.Combine(buildDirectory.FullName, "build/bin/" + buildName + "_Data");
  67. if (Directory.Exists(vsDebugDataDirectory) == false)
  68. {
  69. Debug.LogError("<b>[OpenVR]</b> Could not find data directory at: " + dataDirectory + ". Also checked vs debug at: " + vsDebugDataDirectory);
  70. }
  71. else
  72. {
  73. dataDirectory = vsDebugDataDirectory;
  74. }
  75. }
  76. string streamingAssets = Path.Combine(dataDirectory, "StreamingAssets");
  77. if (Directory.Exists(streamingAssets) == false)
  78. Directory.CreateDirectory(streamingAssets);
  79. string streamingSteamVR = Path.Combine(streamingAssets, "SteamVR");
  80. if (Directory.Exists(streamingSteamVR) == false)
  81. Directory.CreateDirectory(streamingSteamVR);
  82. Debug.Log("settingsAssetPath: " + settingsAssetPath);
  83. FileInfo currentSettingsPath = new FileInfo(settingsAssetPath);
  84. FileInfo newSettingsPath = new FileInfo(Path.Combine(streamingSteamVR, "OpenVRSettings.asset"));
  85. if (newSettingsPath.Exists)
  86. {
  87. newSettingsPath.IsReadOnly = false;
  88. newSettingsPath.Delete();
  89. }
  90. File.Copy(currentSettingsPath.FullName, newSettingsPath.FullName);
  91. Debug.Log("Copied openvr settings to build directory: " + newSettingsPath.FullName);
  92. }
  93. }
  94. }