UniversalProjectSettings.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEditorInternal;
  4. namespace UnityEditor.Rendering.Universal
  5. {
  6. internal class UniversalProjectSettings : ScriptableObject
  7. {
  8. public static string filePath => "ProjectSettings/URPProjectSettings.asset";
  9. //preparing to eventual migration later
  10. enum Version
  11. {
  12. None,
  13. First
  14. }
  15. [SerializeField]
  16. int m_LastMaterialVersion = k_NeverProcessedMaterialVersion;
  17. internal const int k_NeverProcessedMaterialVersion = -1;
  18. public static int materialVersionForUpgrade
  19. {
  20. get => instance.m_LastMaterialVersion;
  21. set
  22. {
  23. instance.m_LastMaterialVersion = value;
  24. Save();
  25. }
  26. }
  27. //singleton pattern
  28. static UniversalProjectSettings s_Instance;
  29. static UniversalProjectSettings instance => s_Instance ?? CreateOrLoad();
  30. UniversalProjectSettings()
  31. {
  32. s_Instance = this;
  33. }
  34. static UniversalProjectSettings CreateOrLoad()
  35. {
  36. //try load
  37. InternalEditorUtility.LoadSerializedFileAndForget(filePath);
  38. //else create
  39. if (s_Instance == null)
  40. {
  41. UniversalProjectSettings created = CreateInstance<UniversalProjectSettings>();
  42. created.hideFlags = HideFlags.HideAndDontSave;
  43. }
  44. System.Diagnostics.Debug.Assert(s_Instance != null);
  45. return s_Instance;
  46. }
  47. static void Save()
  48. {
  49. if (s_Instance == null)
  50. {
  51. Debug.Log("Cannot save ScriptableSingleton: no instance!");
  52. return;
  53. }
  54. string folderPath = Path.GetDirectoryName(filePath);
  55. if (!Directory.Exists(folderPath))
  56. Directory.CreateDirectory(folderPath);
  57. InternalEditorUtility.SaveToSerializedFileAndForget(new[] { s_Instance }, filePath, allowTextSerialization: true);
  58. }
  59. }
  60. }