UnityEssentials
Small but useful tools and features for Unity
EnforcePresetPerFolder.cs
Go to the documentation of this file.
1 using System.IO;
2 using UnityEditor;
3 using UnityEngine;
4 #if UNITY_EDITOR
5 namespace Essentials.EssentialsSettings.UnityConfigurationModifications
6 {
7 
11  public class EnforcePresetPerFolder : Adjustment
12  {
16  public override void Apply()
17  {
18  AssetDatabase.Refresh();
19 
20  string[] foundAssets = AssetDatabase.FindAssets("EnforcePresetPostProcessor.cs");
21  if (foundAssets == null || foundAssets.Length < 1)
22  Debug.LogError("No files named 'EnforcePresetPostProcessor.cs...' have been found.");
23  else if (foundAssets.Length > 1)
24  Debug.LogError("More than one file named 'EnforcePresetPostProcessor.cs...' has been found.");
25  else
26  {
27  var path = AssetDatabase.GUIDToAssetPath(foundAssets[0]);
28  File.Move (path, path.Remove(path.Length-4, 4));
29  File.Delete(path+".meta");
30  AssetDatabase.Refresh();
31 
32  Debug.Log("Presets are now enforced at the folder where they are present.");
33 
34  Popup.Init();
35  }
36 
37  }
38 
42  public override void Revert()
43  {
44  string[] foundAssets = AssetDatabase.FindAssets("t:script EnforcePresetPostProcessor");
45  if (foundAssets == null || foundAssets.Length < 1)
46  Debug.LogError("No files named 'EnforcePresetPostProcessor...' have been found.");
47  else if (foundAssets.Length > 1)
48  Debug.LogError("More than one file named 'EnforcePresetPostProcessor...' has been found.");
49  else
50  {
51  string path = AssetDatabase.GUIDToAssetPath(foundAssets[0]);
52  File.Move(path, path + ".txt");
53  File.Delete(path + ".meta");
54  AssetDatabase.Refresh();
55 
56  Debug.Log("Enforcement of presets by folder disabled.");
57  }
58  }
59 
60  public override string title { get => "Enforce Preset Per Folder"; }
61  public override string revertButtonText { get => "Revert"; }
62  public override string infoURL { get => "https://docs.unity3d.com/Manual/DefaultPresetsByFolder.html"; }
63  public override string applyButtonText { get => "Apply"; }
64 
65  public override string applyAdjustmentShortExplanation { get => "Applies the presets to all assets contained in the same folder of the preset."; }
66  public override string revertAdjustmentShortExplanation { get => "Presets will no longer be automatically applied to the assets in the same folder of the preset."; }
67 
68  public override bool showInSettingsWindow => true;
69 
70  private class Popup : EditorWindow
71  {
72  public static void Init()
73  {
74  Popup window = ScriptableObject.CreateInstance<Popup>();
75  Vector2 windowSize = new Vector2(250f, 150f);
76  window.minSize = window.maxSize = windowSize;
77  window.position = Utils.GetEditorWindowCenteredPosition(windowSize);
78  window.ShowPopup();
79  }
80 
81  void OnGUI()
82  {
83  EditorGUILayout.LabelField("If you want your already-imported Assets to adopt the configuration in the preset in the same folder, reimport them by right-clicking on the name of the asset.", EditorStyles.wordWrappedLabel);
84  GUILayout.Space(60);
85  if (GUILayout.Button("Okay")) this.Close();
86  }
87  }
88  }
89 
90 }
91 #endif
A collection of useful methods that did not fit into any of the other sections of the asset.
Definition: Utils.cs:15