AssetSettingsProvider 是 SettingsProvider 类的一种特化表示,可将旧版设置转换为统一设置 (Unified Settings)。旧版设置包括使用 Inspector 自行修改的任何设置,例如 ProjectSettings 文件夹下的 *.asset 文件。在后台,AssetSettingsProvider 为特定的资源创建 Editor,并通过封装 Editor.OnInspectorGUI 函数为 Settings 窗口构建 UI。
在内部,我们使用此类来封装现有设置。
using UnityEditor; using UnityEngine;
// 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 SerializedObject GetSettings() { 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); }
return new SerializedObject(settings); } }
[CustomEditor(typeof(MyCustomSettings))] class MyCustomSettingsEditor : Editor { // Nothing to do, this uses the Generic Editor to display MyCustomSettings properties }
class AssetSettingsProviderRegister { [SettingsProvider] public static SettingsProvider CreateFromFilePath() { // Create an AssetSettingsProvider from a file path: var provider = AssetSettingsProvider.CreateProviderFromAssetPath("Project/AssetSettings/FromFile", MyCustomSettings.k_MyCustomSettingsPath);
// Register keywords from the properties of MyCustomSettings provider.keywords = SettingsProvider.GetSearchKeywordsFromSerializedObject(new SerializedObject(AssetDatabase.LoadAllAssetsAtPath(MyCustomSettings.k_MyCustomSettingsPath))); return provider; }
[SettingsProvider] public static SettingsProvider CreateFromSettingsObject() { // Create an AssetSettingsProvider from a settings object (UnityEngine.Object): var settingsObj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(MyCustomSettings.k_MyCustomSettingsPath); var provider = AssetSettingsProvider.CreateProviderFromObject("Project/AssetSettings/FromObject", settingsObj);
// Register keywords from the properties of MyCustomSettings provider.keywords = SettingsProvider.GetSearchKeywordsFromSerializedObject(new SerializedObject(settingsObj)); return provider; }
[SettingsProvider] public static SettingsProvider CreateFromSettingsFromFunctor() { // Create an AssetSettingsProvider from a functor that must return a UnityEngine.Object: var provider = new AssetSettingsProvider("Project/AssetSettings/FromFunctor", () => Editor.CreateEditor(AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(MyCustomSettings.k_MyCustomSettingsPath)));
// Register keywords from the properties of MyCustomSettings provider.keywords = SettingsProvider.GetSearchKeywordsFromSerializedObject(new SerializedObject(AssetDatabase.LoadAllAssetsAtPath(MyCustomSettings.k_MyCustomSettingsPath))); return provider; } }
settingsEditor | 编辑器提供 UI 来修改设置。 |
AssetSettingsProvider | 创建一个新的 AssetSettingsProvider,以便可以封装旧版设置(即先前在 Inspector 中显示的设置)。 |
OnActivate | 覆盖此 AssetSettingsProvider 的 SettingsProvider.OnActivate。 |
OnDeactivate | 覆盖此 AssetSettingsProvider 的 SettingsProvider.OnDeactivate。 |
OnFooterBarGUI | 覆盖此 AssetSettingsProvider 的 SettingsProvider.OnFooterBarGUI。 |
OnGUI | 覆盖此 AssetSettingsProvider 的 SettingsProvider.OnGUI。 |
OnTitleBarGUI | 覆盖此 AssetSettingsProvider 的 SettingsProvider.OnTitleBarGUI。这将绘制包含“add to preset”和“help”按钮的按钮栏。 |
CreateProviderFromAssetPath | 从资源路径创建 AssetSettingsProvider。 |
CreateProviderFromObject | 从设置对象创建 AssetSettingsProvider。 |
CreateProviderFromResourcePath | 从资源的资源路径创建 AssetSettingsProvider。 |
activateHandler | 重载 SettingsProvider.OnActivate。 |
deactivateHandler | 重载 SettingsProvider.OnDeactivate。 |
footerBarGuiHandler | 重载 SettingsProvider.OnFooterBarGUI。 |
guiHandler | 重载 SettingsProvider.OnGUI。 |
hasSearchInterestHandler | 重载 SettingsProvider.HasSearchInterest。 |
inspectorUpdateHandler | 重写 SettingsProvider.OnInspectorUpdate。 |
keywords | 获取或设置关键字列表,以便将这些关键字与用户正在搜索的内容进行比较。当用户在 Settings 窗口的搜索框中输入值时,SettingsProvider.HasSearchInterest 会尝试将这些关键字与此列表进行匹配。 |
label | 获取或设置 SettingsProvider 在 Settings 窗口中的显示名称。如果未设置,Settings 窗口将改用 SettingsProvider.settingsPath 的最后一个标记。 |
scope | 获取 SettingsProvider 的范围。该范围确定 SettingsProvider 显示在 Preferences 窗口 (SettingsScope.User) 中还是显示在 Settings 窗口 (SettingsScope.Project) 中。 |
settingsPath | 获取用于将 SettingsProvider 放置在 Settings 窗口的树视图中的路径。该路径在所有其他设置路径中应该具有唯一性,并且应该将“/”用作分隔符。 |
titleBarGuiHandler | 重载 SettingsProvider.OnTitleBarGUI。 |
HasSearchInterest | 检查用户在 Settings 窗口搜索框中键入内容时是否应显示 SettingsProvider。SettingsProvider 尝试将搜索词(甚至搜索词一部分)与任何 SettingsProvider.keywords 匹配。该搜索不区分大小写。 |
OnInspectorUpdate | OnInspectorUpdate 以每秒 10 帧的速度调用,以便检视面板有机会进行更新。有关更多详细信息,请参阅 EditorWindow.OnInspectorUpdate。 |
Repaint | 请求 SettingsWindow 以进行重绘。 |
GetSearchKeywordsFromGUIContentProperties | 从特定类型的所有公共静态成员中提取搜索关键字。 |
GetSearchKeywordsFromPath | 从特定路径中资源的序列化属性提取搜索关键字。 |
GetSearchKeywordsFromSerializedObject | 从 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.