XRGeneralSettingsUpgrade.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using UnityEngine;
  2. using UnityEngine.XR.Management;
  3. namespace UnityEditor.XR.Management
  4. {
  5. /// <summary>Helper class to auto update settings across versions.</summary>
  6. public static class XRGeneralSettingsUpgrade
  7. {
  8. /// <summary>Worker API to do the actual upgrade</summary>
  9. /// <param name="path">Path to asset to upgrade</param>
  10. /// <returns>True if settings were successfullly upgraded, else false.</returns>
  11. public static bool UpgradeSettingsToPerBuildTarget(string path)
  12. {
  13. var generalSettings = GetXRGeneralSettingsInstance(path);
  14. if (generalSettings == null)
  15. return false;
  16. if (!AssetDatabase.IsMainAsset(generalSettings))
  17. return false;
  18. XRGeneralSettings newSettings = ScriptableObject.CreateInstance<XRGeneralSettings>() as XRGeneralSettings;
  19. newSettings.Manager = generalSettings.Manager;
  20. generalSettings = null;
  21. AssetDatabase.DeleteAsset(path);
  22. XRGeneralSettingsPerBuildTarget buildTargetSettings = ScriptableObject.CreateInstance<XRGeneralSettingsPerBuildTarget>() as XRGeneralSettingsPerBuildTarget;
  23. AssetDatabase.CreateAsset(buildTargetSettings, path);
  24. buildTargetSettings.SetSettingsForBuildTarget(EditorUserBuildSettings.selectedBuildTargetGroup, newSettings);
  25. newSettings.name = $"{EditorUserBuildSettings.selectedBuildTargetGroup.ToString()} Settings";
  26. AssetDatabase.AddObjectToAsset(newSettings, path);
  27. AssetDatabase.SaveAssets();
  28. Debug.LogWarningFormat("XR General Settings have been upgraded to be per-Build Target Group. Original settings were moved to Build Target Group {0}.", EditorUserBuildSettings.selectedBuildTargetGroup);
  29. return true;
  30. }
  31. private static XRGeneralSettings GetXRGeneralSettingsInstance(string pathToSettings)
  32. {
  33. XRGeneralSettings ret = null;
  34. if (pathToSettings.Length > 0)
  35. {
  36. ret = AssetDatabase.LoadAssetAtPath(pathToSettings, typeof(XRGeneralSettings)) as XRGeneralSettings;
  37. }
  38. return ret;
  39. }
  40. }
  41. }