UnityEssentials
Small but useful tools and features for Unity
SettingsWindow.cs
Go to the documentation of this file.
1 #if UNITY_EDITOR
2 using System;
3 using UnityEditor;
4 using UnityEditorInternal;
5 using UnityEngine;
6 
8 {
9 
14  public class SettingsWindow : EditorWindow
15  {
16 
20  [InitializeOnLoadMethod]
21  private static void OpenWindowAtFirstUse()
22  {
23  //Check if is running tests
24  if (!InternalEditorUtility.isHumanControllingUs || InternalEditorUtility.inBatchMode)
25  return;
26 
27  if (!SavedData.settingsShown)
28  {
29  EditorApplication.delayCall += () =>
30  {
31  OpenWindow();
32  SavedData.settingsShown = true;
33  };
34  }
35 
36  }
37 
41  [MenuItem("Window/Essentials/Settings")]
42  private static void OpenWindow()
43  {
44  // Get existing open window or if none, make a new one:
45  SettingsWindow window = CreateWindow<SettingsWindow>();
46  //SettingsWindow window = (SettingsWindow)EditorWindow.GetWindow(typeof(SettingsWindow), false, "Essentials' Settings and Adjustments");
47  Vector2 windowSize = new Vector2(600f, 420f);
48  window.minSize = window.maxSize = windowSize;
49  window.position = Utils.GetEditorWindowCenteredPosition(windowSize);
50  window.titleContent = new GUIContent("Essentials' Settings and Adjustments");
51  window.Show();
52  window.Focus();
53  }
54 
58  private Type[] adjustments;
59 
63  private void OnGUI()
64  {
65  if (adjustments == null || adjustments.Length == 0)
66  SearchAdjustments();
67 
68  GUILayout.Label("Essentials Settings", EditorStyles.boldLabel);
69  GUILayout.Label("");
70 
71  if (adjustments != null && adjustments.Length != 0)
72  {
73 
74  GUILayout.Label("Available adjustments:");
75 
76  EditorGUILayout.BeginHorizontal();
77 
78  EditorGUILayout.BeginVertical();
79  foreach (Type adjustmentType in adjustments)
80  {
81  IAdjustment adjustment = (IAdjustment) Activator.CreateInstance(adjustmentType);
82  if (!adjustment.showInSettingsWindow)
83  continue;
84  GUILayout.Label($" • {adjustment.title}");
85  }
86  EditorGUILayout.EndVertical();
87 
88  EditorGUILayout.BeginVertical();
89  foreach (Type adjustmentType in adjustments)
90  {
91 
92  IAdjustment adjustment = (IAdjustment) Activator.CreateInstance(adjustmentType);
93  if (!adjustment.showInSettingsWindow)
94  continue;
95 
96  EditorGUILayout.BeginHorizontal();
97 
98  if (GUILayout.Button(new GUIContent(adjustment.infoButtonText, $"Open {adjustment.infoURL}")))
99  adjustment.OpenInfoURL();
100  GUILayout.Label(""); //Separation
101  GUILayout.Label(""); //Separation
102 
103  if (GUILayout.Button(new GUIContent(adjustment.applyButtonText, adjustment.applyAdjustmentShortExplanation)))
104  adjustment.Apply();
105  if (GUILayout.Button(new GUIContent(adjustment.revertButtonText, adjustment.revertAdjustmentShortExplanation)))
106  adjustment.Revert();
107 
108  EditorGUILayout.EndHorizontal();
109  }
110  EditorGUILayout.EndVertical();
111 
112  EditorGUILayout.EndHorizontal();
113 
114  GUILayout.Label("");
115 
116 
117  EditorGUILayout.BeginHorizontal();
118  if (GUILayout.Button("Apply all"))
119  {
120  foreach (Type adjustmentType in adjustments)
121  {
122  IAdjustment adjustment = (IAdjustment) Activator.CreateInstance(adjustmentType);
123  adjustment.Apply();
124  }
125  }
126 
127  if (GUILayout.Button("Revert all"))
128  {
129  foreach (Type adjustmentType in adjustments)
130  {
131  IAdjustment adjustment = (IAdjustment) Activator.CreateInstance(adjustmentType);
132  adjustment.Revert();
133  }
134  }
135  EditorGUILayout.EndHorizontal();
136 
137  GUILayout.Label("");
138 
139  }
140 
141  GUILayout.Label("");
142 
143  #region Tooltip
144  GUIStyle style = GUI.skin.label; //get the current style of the GUI;
145  TextAnchor defaultAlignment = style.alignment; // Expected: TextAnchor.MiddleLeft
146  style.alignment = TextAnchor.MiddleRight;
147  GUILayout.Label (GUI.tooltip, style);
148  style.alignment = defaultAlignment;
149  #endregion
150 
151  /*
152  EditorGUILayout.BeginHorizontal();
153  if (implementations != null) EditorGUILayout.LabelField($"Found {implementations.Count()} adjustments", EditorStyles.helpBox);
154  if (implementations == null) EditorGUILayout.LabelField($"NO IMPLEMENTATIONS FOUND");
155  if (GUILayout.Button(new GUIContent("Search for adjustments", "Search for any implementation of the abstract class 'Adjustment' to be displayed in this window." )) && implementations == null)
156  SearchConfigurationModifiers();
157  EditorGUILayout.EndHorizontal();
158  */
159 
160  GUILayout.Label("");
161  EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
162 
163 
164  #region Links
165  EditorGUILayout.BeginHorizontal();
166  if (GUILayout.Button(new GUIContent("Rate the asset! ❤️", "Open the Asset Store page of the asset, so you can rate it, share it or give any kind of love you want!" )))
167  EssentialsHelp.OpenLinkRateAsset();
168  if (GUILayout.Button(new GUIContent("Give feedback or any ideas! 💡", "Open a form to share any thoughts you have about the asset, so we can keep improving." )))
169  EssentialsHelp.OpenLinkFeedback();
170  if (GUILayout.Button(new GUIContent("About me : )", "Open my personal webpage where you can know more about me!" )))
171  EssentialsHelp.OpenLinkAboutMe();
172  EditorGUILayout.EndHorizontal();
173  EditorGUILayout.BeginHorizontal();
174  if (GUILayout.Button(new GUIContent("Quick Documentation", "Open the online document containing the quick documentation of the latest version of the asset." )))
175  EssentialsHelp.OpenLinkDocumentation();
176  if (GUILayout.Button(new GUIContent("Scripting Documentation", "Open the online scripting documentation of the latest version of the asset." )))
177  EssentialsHelp.OpenLinkScriptingDocumentation();
178  if (GUILayout.Button(new GUIContent("GitHub", "Open the GitHub repository of the asset. You are welcome to collaborate!" )))
179  EssentialsHelp.OpenLinkGitHubRepository();
180  EditorGUILayout.EndHorizontal();
181  #endregion
182  }
183 
187  private void SearchAdjustments()
188  {
189  adjustments = Utils.GetTypeImplementationsNotUnityObject<IAdjustment>();
190  }
191  }
192 }
193 
194 #endif