Preferences.Project.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.IO;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace AmplifyShaderEditor
  8. {
  9. public partial class Preferences
  10. {
  11. public class Project
  12. {
  13. public static bool AutoSRP => Values.AutoSRP;
  14. public static bool DefineSymbol => Values.DefineSymbol;
  15. public static string TemplateExtensions => Values.TemplateExtensions;
  16. private class Styles
  17. {
  18. public static readonly GUIContent AutoSRP = new GUIContent( "Auto import SRP shader templates", "By default Amplify Shader Editor checks for your SRP version and automatically imports the correct corresponding shader templates.\nTurn this OFF if you prefer to import them manually." );
  19. public static readonly GUIContent DefineSymbol = new GUIContent( "Add Amplify Shader Editor define symbol", "Turning it OFF will disable the automatic insertion of the define symbol and remove it from the list while turning it ON will do the opposite.\nThis is used for compatibility with other plugins, if you are not sure if you need this leave it ON." );
  20. public static readonly GUIContent TemplateExtensions = new GUIContent( "Template Extensions", "Supported file extensions for parsing shader templates." );
  21. }
  22. private class Defaults
  23. {
  24. public const bool AutoSRP = true;
  25. public const bool DefineSymbol = true;
  26. public const string TemplateExtensions = ".shader;.shader.template";
  27. }
  28. [Serializable]
  29. private struct Layout
  30. {
  31. public bool AutoSRP;
  32. public bool DefineSymbol;
  33. public string TemplateExtensions;
  34. }
  35. private const string RelativePath = "ProjectSettings/AmplifyShaderEditor.asset";
  36. private static string FullPath = Path.GetFullPath( RelativePath );
  37. private static Layout Values = new Layout();
  38. public static void ResetSettings()
  39. {
  40. Values.AutoSRP = Defaults.AutoSRP;
  41. Values.DefineSymbol = Defaults.DefineSymbol;
  42. Values.TemplateExtensions = Defaults.TemplateExtensions;
  43. }
  44. public static void LoadSettings()
  45. {
  46. try
  47. {
  48. Values = JsonUtility.FromJson<Layout>( File.ReadAllText( FullPath ) );
  49. }
  50. catch ( System.Exception e )
  51. {
  52. if ( e.GetType() == typeof( FileNotFoundException ) )
  53. {
  54. ResetSettings();
  55. // @diogo: try to retrieve the old preferences if file does not exist yet
  56. Values.AutoSRP = EditorPrefs.GetBool( "ASEAutoSRP", Defaults.AutoSRP );
  57. Values.DefineSymbol = EditorPrefs.GetBool( "ASEDefineSymbol", Defaults.DefineSymbol );
  58. SaveSettings();
  59. }
  60. else
  61. {
  62. Debug.LogWarning( "[AmplifyTexture] Failed importing \"" + RelativePath + "\". Reverting to default settings." );
  63. }
  64. }
  65. }
  66. public static void SaveSettings()
  67. {
  68. if ( DefineSymbol )
  69. {
  70. IOUtils.SetAmplifyDefineSymbolOnBuildTargetGroup( EditorUserBuildSettings.selectedBuildTargetGroup );
  71. }
  72. else
  73. {
  74. IOUtils.RemoveAmplifyDefineSymbolOnBuildTargetGroup( EditorUserBuildSettings.selectedBuildTargetGroup );
  75. }
  76. try
  77. {
  78. File.WriteAllText( FullPath, JsonUtility.ToJson( Values ) );
  79. }
  80. catch ( System.Exception )
  81. {
  82. // TODO: Not critical?
  83. }
  84. }
  85. public static void InspectorLayout()
  86. {
  87. Values.AutoSRP = EditorGUILayout.Toggle( Styles.AutoSRP, Values.AutoSRP );
  88. Values.DefineSymbol = EditorGUILayout.Toggle( Styles.DefineSymbol, Values.DefineSymbol );
  89. Values.TemplateExtensions = EditorGUILayout.TextField( Styles.TemplateExtensions, Values.TemplateExtensions );
  90. }
  91. }
  92. }
  93. }