File size: 23,249 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | #if UNITY_2018_3_OR_NEWER
#define SETTINGS_PROVIDER_ENABLED
#endif
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
#if SETTINGS_PROVIDER_ENABLED
#if UNITY_2019_1_OR_NEWER
using UnityEngine.UIElements;
#else
using UnityEngine.Experimental.UIElements;
#endif
#endif
namespace UnityEditor.SettingsManagement
{
/// <summary>
/// A <see cref="UnityEditor.SettingsProvider"/> implementation that creates an interface from settings reflected
/// from a collection of assemblies.
/// </summary>
#if SETTINGS_PROVIDER_ENABLED
public sealed class UserSettingsProvider : SettingsProvider
#else
public sealed class UserSettingsProvider
#endif
{
public const string developerModeCategory = "Developer Mode";
const string k_SettingsName = "UserSettingsProviderSettings";
#if SETTINGS_PROVIDER_ENABLED
const int k_LabelWidth = 240;
static int labelWidth
{
get
{
if (s_DefaultLabelWidth != null)
return (int)((float)s_DefaultLabelWidth.GetValue(null, null));
return k_LabelWidth;
}
}
static int defaultLayoutMaxWidth
{
get
{
if (s_DefaultLayoutMaxWidth != null)
return (int)((float)s_DefaultLayoutMaxWidth.GetValue(null, null));
return 0;
}
}
#else
const int k_LabelWidth = 180;
int labelWidth
{
get { return k_LabelWidth; }
}
int defaultLayoutMaxWidth
{
get { return 0; }
}
#endif
List<string> m_Categories;
Dictionary<string, List<PrefEntry>> m_Settings;
Dictionary<string, List<MethodInfo>> m_SettingBlocks;
#if !SETTINGS_PROVIDER_ENABLED
HashSet<string> keywords = new HashSet<string>();
#endif
static readonly string[] s_SearchContext = new string[1];
EventType m_SettingsBlockKeywordsInitialized;
Assembly[] m_Assemblies;
static Settings s_Settings;
Settings m_SettingsInstance;
#if SETTINGS_PROVIDER_ENABLED
static PropertyInfo s_DefaultLabelWidth;
static PropertyInfo s_DefaultLayoutMaxWidth;
#endif
static Settings userSettingsProviderSettings
{
get
{
if (s_Settings == null)
s_Settings = new Settings(new [] { new UserSettingsRepository() });
return s_Settings;
}
}
internal static UserSetting<bool> showHiddenSettings = new UserSetting<bool>(userSettingsProviderSettings, "settings.showHidden", false, SettingsScope.User);
internal static UserSetting<bool> showUnregisteredSettings = new UserSetting<bool>(userSettingsProviderSettings, "settings.showUnregistered", false, SettingsScope.User);
internal static UserSetting<bool> listByKey = new UserSetting<bool>(userSettingsProviderSettings, "settings.listByKey", false, SettingsScope.User);
internal static UserSetting<bool> showUserSettings = new UserSetting<bool>(userSettingsProviderSettings, "settings.showUserSettings", true, SettingsScope.User);
internal static UserSetting<bool> showProjectSettings = new UserSetting<bool>(userSettingsProviderSettings, "settings.showProjectSettings", true, SettingsScope.User);
#if SETTINGS_PROVIDER_ENABLED
/// <summary>
/// Create a new UserSettingsProvider.
/// </summary>
/// <param name="path">The settings menu path.</param>
/// <param name="settings">The Settings instance that this provider is inspecting.</param>
/// <param name="assemblies">A collection of assemblies to scan for <see cref="UserSettingAttribute"/> and <see cref="UserSettingBlockAttribute"/> attributes.</param>
/// <param name="scopes">Which scopes this provider is valid for.</param>
/// <exception cref="ArgumentNullException">Thrown if settings or assemblies is null.</exception>
public UserSettingsProvider(string path, Settings settings, Assembly[] assemblies, SettingsScope scopes = SettingsScope.User)
: base(path, scopes)
#else
/// <summary>
/// Create a new UserSettingsProvider.
/// </summary>
/// <param name="settings">The Settings instance that this provider is inspecting.</param>
/// <param name="assemblies">A collection of assemblies to scan for <see cref="UserSettingAttribute"/> and <see cref="UserSettingBlockAttribute"/> attributes.</param>
public UserSettingsProvider(Settings settings, Assembly[] assemblies)
#endif
{
if (settings == null)
throw new ArgumentNullException("settings");
if (assemblies == null)
throw new ArgumentNullException("assemblies");
m_SettingsInstance = settings;
m_Assemblies = assemblies;
#if !SETTINGS_PROVIDER_ENABLED
SearchForUserSettingAttributes();
#endif
}
#if SETTINGS_PROVIDER_ENABLED
/// <summary>
/// Invoked by the SettingsProvider when activated in the Editor.
/// </summary>
/// <param name="searchContext"></param>
/// <param name="rootElement"></param>
public override void OnActivate(string searchContext, VisualElement rootElement)
{
SearchForUserSettingAttributes();
var window = GetType().GetProperty("settingsWindow", BindingFlags.Instance | BindingFlags.NonPublic);
if (window != null)
{
s_DefaultLabelWidth = window.PropertyType.GetProperty("s_DefaultLabelWidth", BindingFlags.Public | BindingFlags.Static);
s_DefaultLayoutMaxWidth = window.PropertyType.GetProperty("s_DefaultLayoutMaxWidth", BindingFlags.Public | BindingFlags.Static);
}
}
#endif
struct PrefEntry
{
GUIContent m_Content;
IUserSetting m_Pref;
public GUIContent content
{
get { return m_Content; }
}
public IUserSetting pref
{
get { return m_Pref; }
}
public PrefEntry(GUIContent content, IUserSetting pref)
{
m_Content = content;
m_Pref = pref;
}
}
void SearchForUserSettingAttributes()
{
var isDeveloperMode = EditorPrefs.GetBool("DeveloperMode", false);
var keywordsHash = new HashSet<string>();
if (m_Settings != null)
m_Settings.Clear();
else
m_Settings = new Dictionary<string, List<PrefEntry>>();
if (m_SettingBlocks != null)
m_SettingBlocks.Clear();
else
m_SettingBlocks = new Dictionary<string, List<MethodInfo>>();
var types = m_Assemblies.SelectMany(x => x.GetTypes());
// collect instance fields/methods too, but only so we can throw a warning that they're invalid.
var fields = types.SelectMany(x =>
x.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
.Where(prop => Attribute.IsDefined(prop, typeof(UserSettingAttribute))));
var methods = types.SelectMany(x => x.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
.Where(y => Attribute.IsDefined(y, typeof(UserSettingBlockAttribute))));
foreach (var field in fields)
{
if (!field.IsStatic)
{
Debug.LogWarning("Cannot create setting entries for instance fields. Skipping \"" + field.Name + "\".");
continue;
}
var attrib = (UserSettingAttribute)Attribute.GetCustomAttribute(field, typeof(UserSettingAttribute));
if (!attrib.visibleInSettingsProvider)
continue;
var pref = (IUserSetting)field.GetValue(null);
if (pref == null)
{
Debug.LogWarning("[UserSettingAttribute] is only valid for types implementing the IUserSetting interface. Skipping \"" + field.Name + "\"");
continue;
}
var category = string.IsNullOrEmpty(attrib.category) ? "Uncategorized" : attrib.category;
var content = listByKey ? new GUIContent(pref.key) : attrib.title;
if (developerModeCategory.Equals(category) && !isDeveloperMode)
continue;
List<PrefEntry> settings;
if (m_Settings.TryGetValue(category, out settings))
settings.Add(new PrefEntry(content, pref));
else
m_Settings.Add(category, new List<PrefEntry>() { new PrefEntry(content, pref) });
}
foreach (var method in methods)
{
var attrib = (UserSettingBlockAttribute)Attribute.GetCustomAttribute(method, typeof(UserSettingBlockAttribute));
var category = string.IsNullOrEmpty(attrib.category) ? "Uncategorized" : attrib.category;
if (developerModeCategory.Equals(category) && !isDeveloperMode)
continue;
List<MethodInfo> blocks;
var parameters = method.GetParameters();
if (!method.IsStatic || parameters.Length < 1 || parameters[0].ParameterType != typeof(string))
{
Debug.LogWarning("[UserSettingBlockAttribute] is only valid for static functions with a single string parameter. Ex, `static void MySettings(string searchContext)`. Skipping \"" + method.Name + "\"");
continue;
}
if (m_SettingBlocks.TryGetValue(category, out blocks))
blocks.Add(method);
else
m_SettingBlocks.Add(category, new List<MethodInfo>() { method });
}
if (showHiddenSettings)
{
var unlisted = new List<PrefEntry>();
m_Settings.Add("Unlisted", unlisted);
foreach (var pref in UserSettings.FindUserSettings(m_Assemblies, SettingVisibility.Unlisted | SettingVisibility.Hidden))
unlisted.Add(new PrefEntry(new GUIContent(pref.key), pref));
}
if (showUnregisteredSettings)
{
var unregistered = new List<PrefEntry>();
m_Settings.Add("Unregistered", unregistered);
foreach (var pref in UserSettings.FindUserSettings(m_Assemblies, SettingVisibility.Unregistered))
unregistered.Add(new PrefEntry(new GUIContent(pref.key), pref));
}
foreach (var cat in m_Settings)
{
foreach (var entry in cat.Value)
{
var content = entry.content;
if (content != null && !string.IsNullOrEmpty(content.text))
{
foreach (var word in content.text.Split(' '))
keywordsHash.Add(word);
}
}
}
keywords = keywordsHash;
m_Categories = m_Settings.Keys.Union(m_SettingBlocks.Keys).ToList();
m_Categories.Sort();
}
#if SETTINGS_PROVIDER_ENABLED
/// <summary>
/// Invoked by the SettingsProvider container when drawing the UI header.
/// </summary>
public override void OnTitleBarGUI()
{
if (GUILayout.Button(GUIContent.none, SettingsGUIStyles.settingsGizmo))
DoContextMenu();
}
#endif
void InitSettingsBlockKeywords()
{
// Have to let the blocks run twice - one for Layout, one for Repaint.
if (m_SettingsBlockKeywordsInitialized == EventType.Repaint)
return;
m_SettingsBlockKeywordsInitialized = Event.current.type;
// Allows SettingsGUILayout.SettingsField to populate keywords
SettingsGUILayout.s_Keywords = new HashSet<string>(keywords);
// Set a dummy value so that GUI blocks with conditional foldouts will behave as though searching.
s_SearchContext[0] = "Search";
foreach (var category in m_SettingBlocks)
{
foreach (var block in category.Value)
block.Invoke(null, s_SearchContext);
}
keywords = SettingsGUILayout.s_Keywords;
SettingsGUILayout.s_Keywords = null;
s_SearchContext[0] = "";
}
void DoContextMenu()
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Reset All"), false, () =>
{
if (!UnityEditor.EditorUtility.DisplayDialog("Reset All Settings", "Reset all settings? This is not undo-able.", "Reset", "Cancel"))
return;
// Do not reset SettingVisibility.Unregistered
foreach (var pref in UserSettings.FindUserSettings(m_Assemblies, SettingVisibility.Visible | SettingVisibility.Hidden | SettingVisibility.Unlisted))
pref.Reset();
m_SettingsInstance.Save();
});
if (EditorPrefs.GetBool("DeveloperMode", false))
{
menu.AddSeparator("");
menu.AddItem(new GUIContent("Developer/List Settings By Key"), listByKey, () =>
{
listByKey.SetValue(!listByKey, true);
SearchForUserSettingAttributes();
});
menu.AddSeparator("Developer/");
menu.AddItem(new GUIContent("Developer/Show User Settings"), showUserSettings, () =>
{
showUserSettings.SetValue(!showUserSettings, true);
SearchForUserSettingAttributes();
});
menu.AddItem(new GUIContent("Developer/Show Project Settings"), showProjectSettings, () =>
{
showProjectSettings.SetValue(!showProjectSettings, true);
SearchForUserSettingAttributes();
});
menu.AddSeparator("Developer/");
menu.AddItem(new GUIContent("Developer/Show Unlisted Settings"), showHiddenSettings, () =>
{
showHiddenSettings.SetValue(!showHiddenSettings, true);
SearchForUserSettingAttributes();
});
menu.AddItem(new GUIContent("Developer/Show Unregistered Settings"), showUnregisteredSettings, () =>
{
showUnregisteredSettings.SetValue(!showUnregisteredSettings, true);
SearchForUserSettingAttributes();
});
menu.AddSeparator("Developer/");
menu.AddItem(new GUIContent("Developer/Open Project Settings File"), false, () =>
{
var project = m_SettingsInstance.GetRepository(SettingsScope.Project);
if (project != null)
{
var path = Path.GetFullPath(project.path);
System.Diagnostics.Process.Start(path);
}
});
menu.AddItem(new GUIContent("Developer/Print All Settings"), false, () =>
{
Debug.Log(UserSettings.GetSettingsString(m_Assemblies));
});
#if UNITY_2019_1_OR_NEWER
menu.AddSeparator("Developer/");
#if UNITY_2019_3_OR_NEWER
menu.AddItem(new GUIContent("Developer/Recompile Scripts"), false, EditorUtility.RequestScriptReload);
#else
menu.AddItem(new GUIContent("Developer/Recompile Scripts"), false, UnityEditorInternal.InternalEditorUtility.RequestScriptReload);
#endif
#endif
}
menu.ShowAsContext();
}
#if SETTINGS_PROVIDER_ENABLED
/// <summary>
/// Invoked by the Settings editor.
/// </summary>
/// <param name="searchContext">
/// A string containing the contents of the search bar.
/// </param>
public override void OnGUI(string searchContext)
#else
/// <summary>
/// Invoked by the Settings editor.
/// </summary>
/// <param name="searchContext">
/// A string containing the contents of the search bar.
/// </param>
public void OnGUI(string searchContext)
#endif
{
#if !SETTINGS_PROVIDER_ENABLED
var evt = Event.current;
if (evt.type == EventType.ContextClick)
DoContextMenu();
#endif
InitSettingsBlockKeywords();
EditorGUIUtility.labelWidth = labelWidth;
EditorGUI.BeginChangeCheck();
var maxWidth = defaultLayoutMaxWidth;
if (maxWidth != 0)
GUILayout.BeginVertical(SettingsGUIStyles.settingsArea, GUILayout.MaxWidth(maxWidth));
else
GUILayout.BeginVertical(SettingsGUIStyles.settingsArea);
var hasSearchContext = !string.IsNullOrEmpty(searchContext);
s_SearchContext[0] = searchContext;
if (hasSearchContext)
{
// todo - Improve search comparison
var searchKeywords = searchContext.Split(' ');
foreach (var settingField in m_Settings)
{
foreach (var setting in settingField.Value)
{
if (searchKeywords.Any(x => !string.IsNullOrEmpty(x) && setting.content.text.IndexOf(x, StringComparison.InvariantCultureIgnoreCase) > -1))
DoPreferenceField(setting.content, setting.pref);
}
}
foreach (var settingsBlock in m_SettingBlocks)
{
foreach (var block in settingsBlock.Value)
{
block.Invoke(null, s_SearchContext);
}
}
}
else
{
foreach (var key in m_Categories)
{
GUILayout.Label(key, EditorStyles.boldLabel);
List<PrefEntry> settings;
if (m_Settings.TryGetValue(key, out settings))
foreach (var setting in settings)
DoPreferenceField(setting.content, setting.pref);
List<MethodInfo> blocks;
if (m_SettingBlocks.TryGetValue(key, out blocks))
foreach (var block in blocks)
block.Invoke(null, s_SearchContext);
GUILayout.Space(8);
}
}
EditorGUIUtility.labelWidth = 0;
GUILayout.EndVertical();
if (EditorGUI.EndChangeCheck())
{
m_SettingsInstance.Save();
}
}
void DoPreferenceField(GUIContent title, IUserSetting pref)
{
if (EditorPrefs.GetBool("DeveloperMode", false))
{
if (pref.scope == SettingsScope.Project && !showProjectSettings)
return;
if (pref.scope == SettingsScope.User && !showUserSettings)
return;
}
if (pref is UserSetting<float>)
{
var cast = (UserSetting<float>)pref;
cast.value = EditorGUILayout.FloatField(title, cast.value);
}
else if (pref is UserSetting<int>)
{
var cast = (UserSetting<int>)pref;
cast.value = EditorGUILayout.IntField(title, cast.value);
}
else if (pref is UserSetting<bool>)
{
var cast = (UserSetting<bool>)pref;
cast.value = EditorGUILayout.Toggle(title, cast.value);
}
else if (pref is UserSetting<string>)
{
var cast = (UserSetting<string>)pref;
cast.value = EditorGUILayout.TextField(title, cast.value);
}
else if (pref is UserSetting<Color>)
{
var cast = (UserSetting<Color>)pref;
cast.value = EditorGUILayout.ColorField(title, cast.value);
}
#if UNITY_2018_3_OR_NEWER
else if (pref is UserSetting<Gradient>)
{
var cast = (UserSetting<Gradient>)pref;
cast.value = EditorGUILayout.GradientField(title, cast.value);
}
#endif
else if (pref is UserSetting<Vector2>)
{
var cast = (UserSetting<Vector2>)pref;
cast.value = EditorGUILayout.Vector2Field(title, cast.value);
}
else if (pref is UserSetting<Vector3>)
{
var cast = (UserSetting<Vector3>)pref;
cast.value = EditorGUILayout.Vector3Field(title, cast.value);
}
else if (pref is UserSetting<Vector4>)
{
var cast = (UserSetting<Vector4>)pref;
cast.value = EditorGUILayout.Vector4Field(title, cast.value);
}
else if (typeof(Enum).IsAssignableFrom(pref.type))
{
Enum val = (Enum)pref.GetValue();
EditorGUI.BeginChangeCheck();
if (Attribute.IsDefined(pref.type, typeof(FlagsAttribute)))
val = EditorGUILayout.EnumFlagsField(title, val);
else
val = EditorGUILayout.EnumPopup(title, val);
if (EditorGUI.EndChangeCheck())
pref.SetValue(val);
}
else if (typeof(UnityEngine.Object).IsAssignableFrom(pref.type))
{
var obj = (UnityEngine.Object)pref.GetValue();
EditorGUI.BeginChangeCheck();
obj = EditorGUILayout.ObjectField(title, obj, pref.type, false);
if (EditorGUI.EndChangeCheck())
pref.SetValue(obj);
}
else
{
GUILayout.BeginHorizontal();
GUILayout.Label(title, GUILayout.Width(EditorGUIUtility.labelWidth - EditorStyles.label.margin.right * 2));
var obj = pref.GetValue();
GUILayout.Label(obj == null ? "null" : pref.GetValue().ToString());
GUILayout.EndHorizontal();
}
SettingsGUILayout.DoResetContextMenuForLastRect(pref);
}
}
}
|