XRPackageInitializationSettings.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime;
  7. using UnityEngine;
  8. using UnityEditor;
  9. namespace UnityEditor.XR.Management
  10. {
  11. internal class XRPackageInitializationSettings : ScriptableObject
  12. {
  13. private static XRPackageInitializationSettings s_PackageSettings = null;
  14. private static object s_Lock = new object();
  15. [SerializeField]
  16. private List<string> m_Settings = new List<string>();
  17. private XRPackageInitializationSettings(){ }
  18. internal static XRPackageInitializationSettings Instance
  19. {
  20. get
  21. {
  22. if (s_PackageSettings == null)
  23. {
  24. lock(s_Lock)
  25. {
  26. if (s_PackageSettings == null)
  27. {
  28. s_PackageSettings = ScriptableObject.CreateInstance<XRPackageInitializationSettings>();
  29. s_PackageSettings.LoadSettings();
  30. }
  31. }
  32. }
  33. return s_PackageSettings;
  34. }
  35. }
  36. internal void LoadSettings()
  37. {
  38. string packageInitPath = Path.Combine("ProjectSettings", "XRPackageSettings.asset");
  39. if (File.Exists(packageInitPath))
  40. {
  41. using (StreamReader sr = new StreamReader(packageInitPath))
  42. {
  43. string settings = sr.ReadToEnd();
  44. JsonUtility.FromJsonOverwrite(settings, this);
  45. }
  46. }
  47. }
  48. internal void SaveSettings()
  49. {
  50. string packageInitPath = Path.Combine("ProjectSettings", "XRPackageSettings.asset");
  51. using (StreamWriter sw = new StreamWriter(packageInitPath))
  52. {
  53. string settings = JsonUtility.ToJson(this, true);
  54. sw.Write(settings);
  55. }
  56. }
  57. internal bool HasSettings(string key)
  58. {
  59. return m_Settings.Contains(key);
  60. }
  61. internal void AddSettings(string key)
  62. {
  63. if (!HasSettings(key))
  64. m_Settings.Add(key);
  65. }
  66. }
  67. }