class in UnityEditor
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseSettingsProvider is the configuration class that specifies how a Project setting or a preference should appear in the Settings or Preferences window.
In order to add new Project settings or preference pages, define a SettingsProvider. The SettingsProvider class provides the hooks to display any UI (using either IMGUI or UIElements to draw it). It also provides an API that allows you to specify keywords which are used in the Settings and Preferences windows in two ways:
1) The search bar filters out SettingsProviders that don't have matching keywords.
2) Property Labels are highlighted with matching keywords.
This example demonstrates multiple ways to create and configure different SettingsProviders:
using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; using UnityEditor.UIElements;
// Create a new type of Settings Asset. class MyCustomSettings : ScriptableObject { public const string k_MyCustomSettingsPath = "Assets/Editor/MyCustomSettings.asset";
[SerializeField] private int m_Number;
[SerializeField] private string m_SomeString;
internal static MyCustomSettings GetOrCreateSettings() { var settings = AssetDatabase.LoadAssetAtPath<MyCustomSettings>(k_MyCustomSettingsPath); if (settings == null) { settings = ScriptableObject.CreateInstance<MyCustomSettings>(); settings.m_Number = 42; settings.m_SomeString = "The answer to the universe"; AssetDatabase.CreateAsset(settings, k_MyCustomSettingsPath); AssetDatabase.SaveAssets(); } return settings; }
internal static SerializedObject GetSerializedSettings() { return new SerializedObject(GetOrCreateSettings()); } }
// Register a SettingsProvider using IMGUI for the drawing framework: static class MyCustomSettingsIMGUIRegister { [SettingsProvider] public static SettingsProvider CreateMyCustomSettingsProvider() { // First parameter is the path in the Settings window. // Second parameter is the scope of this setting: it only appears in the Project Settings window. var provider = new SettingsProvider("Project/MyCustomIMGUISettings", SettingsScope.Project) { // By default the last token of the path is used as display name if no label is provided. label = "Custom IMGUI", // Create the SettingsProvider and initialize its drawing (IMGUI) function in place: guiHandler = (searchContext) => { var settings = MyCustomSettings.GetSerializedSettings(); EditorGUILayout.PropertyField(settings.FindProperty("m_Number"), new GUIContent("My Number")); EditorGUILayout.PropertyField(settings.FindProperty("m_SomeString"), new GUIContent("My String")); settings.ApplyModifiedPropertiesWithoutUndo(); },
// Populate the search keywords to enable smart search filtering and label highlighting: keywords = new HashSet<string>(new[] { "Number", "Some String" }) };
return provider; } }
// Register a SettingsProvider using UIElements for the drawing framework: static class MyCustomSettingsUIElementsRegister { [SettingsProvider] public static SettingsProvider CreateMyCustomSettingsProvider() { // First parameter is the path in the Settings window. // Second parameter is the scope of this setting: it only appears in the Settings window for the Project scope. var provider = new SettingsProvider("Project/MyCustomUIElementsSettings", SettingsScope.Project) { label = "Custom UI Elements", // activateHandler is called when the user clicks on the Settings item in the Settings window. activateHandler = (searchContext, rootElement) => { var settings = MyCustomSettings.GetSerializedSettings();
// rootElement is a VisualElement. If you add any children to it, the OnGUI function // isn't called because the SettingsProvider uses the UIElements drawing framework. var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Editor/settings_ui.uss"); rootElement.styleSheets.Add(styleSheet); var title = new Label() { text = "Custom UI Elements" }; title.AddToClassList("title"); rootElement.Add(title);
var properties = new VisualElement() { style = { flexDirection = FlexDirection.Column } }; properties.AddToClassList("property-list"); rootElement.Add(properties);
properties.Add(new PropertyField(settings.FindProperty("m_SomeString"))); properties.Add(new PropertyField(settings.FindProperty("m_Number")));
rootElement.Bind(settings); },
// Populate the search keywords to enable smart search filtering and label highlighting: keywords = new HashSet<string>(new[] { "Number", "Some String" }) };
return provider; } }
// Create MyCustomSettingsProvider by deriving from SettingsProvider: class MyCustomSettingsProvider : SettingsProvider { private SerializedObject m_CustomSettings;
class Styles { public static GUIContent number = new GUIContent("My Number"); public static GUIContent someString = new GUIContent("Some string"); }
const string k_MyCustomSettingsPath = "Assets/Editor/MyCustomSettings.asset"; public MyCustomSettingsProvider(string path, SettingsScope scope = SettingsScope.User) : base(path, scope) {}
public static bool IsSettingsAvailable() { return File.Exists(k_MyCustomSettingsPath); }
public override void OnActivate(string searchContext, VisualElement rootElement) { // This function is called when the user clicks on the MyCustom element in the Settings window. m_CustomSettings = MyCustomSettings.GetSerializedSettings(); }
public override void OnGUI(string searchContext) { // Use IMGUI to display UI: EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("m_Number"), Styles.number); EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("m_SomeString"), Styles.someString); m_CustomSettings.ApplyModifiedPropertiesWithoutUndo(); }
// Register the SettingsProvider [SettingsProvider] public static SettingsProvider CreateMyCustomSettingsProvider() { if (IsSettingsAvailable()) { var provider = new MyCustomSettingsProvider("Project/MyCustomSettingsProvider", SettingsScope.Project);
// Automatically extract all keywords from the Styles. provider.keywords = GetSearchKeywordsFromGUIContentProperties<Styles>(); return provider; }
// Settings Asset doesn't exist yet; no need to display anything in the Settings window. return null; } }
activateHandler | Overrides SettingsProvider.OnActivate. |
deactivateHandler | Overrides SettingsProvider.OnDeactivate. |
footerBarGuiHandler | Overrides SettingsProvider.OnFooterBarGUI. |
guiHandler | Overrides SettingsProvider.OnGUI. |
hasSearchInterestHandler | Overrides SettingsProvider.HasSearchInterest. |
inspectorUpdateHandler | Overrides SettingsProvider.OnInspectorUpdate. |
keywords | Gets or sets the list of keywords to compare against what the user is searching for. When the user enters values in the search box on the Settings window, SettingsProvider.HasSearchInterest tries to match those keywords to this list. |
label | Gets or sets the display name of the SettingsProvider as it appears in the Settings window. If not set, the Settings window uses last token of SettingsProvider.settingsPath instead. |
scope | Gets the Scope of the SettingsProvider. The Scope determines whether the SettingsProvider appears in the Preferences window (SettingsScope.User) or the Settings window (SettingsScope.Project). |
settingsPath | Gets Path used to place the SettingsProvider in the tree view of the Settings window. The path should be unique among all other settings paths and should use "/" as its separator. |
titleBarGuiHandler | Overrides SettingsProvider.OnTitleBarGUI. |
SettingsProvider | Creates a new SettingsProvider. |
HasSearchInterest | Checks whether the SettingsProvider should appear when the user types something in the Settings window search box. SettingsProvider tries to match the search terms (even partially) to any of the SettingsProvider.keywords. The search is case insensitive. |
OnActivate | Use this function to implement a handler for when the user clicks on the Settings in the Settings window. You can fetch a settings Asset or set up UIElements UI from this function. |
OnDeactivate | Use this function to implement a handler for when the user clicks on another setting or when the Settings window closes. |
OnFooterBarGUI | Use this function to override drawing the footer for the SettingsProvider using IMGUI. |
OnGUI | Use this function to draw the UI based on IMGUI. This assumes you haven't added any children to the rootElement passed to the OnActivate function. |
OnInspectorUpdate | OnInspectorUpdate is called at 10 frames per second to give the inspector a chance to update. See EditorWindow.OnInspectorUpdate for more details. |
OnTitleBarGUI | Use this function to override drawing the title for the SettingsProvider using IMGUI. This allows you to add custom UI (such as a toolbar button) next to the title. AssetSettingsProvider uses this mecanism to display the "add to preset" and the "help" buttons. |
Repaint | Request the SettingsWindow for a repaint. |
GetSearchKeywordsFromGUIContentProperties | Extract search keywords from all public static memebers in a specific Type. |
GetSearchKeywordsFromPath | Extract search keywords from the serialized properties of an Asset at a specific path. |
GetSearchKeywordsFromSerializedObject | Extract search keywords from from the serialized properties of a SerializedObject. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.