-
- /// <summary>
- /// 用于存储我们的设置配置
- /// </summary>
- public class EditorLearnSettings : ScriptableObject
- {
- /// <summary>
- /// 用于我们设置的保存位置
- /// </summary>
- private const string SettingsFile= "Assets/Settings/EditorLearnSetting.asset";
-
- #region 此处可以存储我们自定义的配置信息
- /// <summary>
- /// 模拟我们的配置
- /// </summary>
- public int m_ConfigVersionCode = 1;
- #endregion
- }
在我们的EditorLearnSettings类提供以下方法,为我们提供全局配置信息。
- /// <summary>
- /// 这里提供一个配置对象
- /// </summary>
- /// <returns></returns>
- public static EditorLearnSettings GetOrCreateSettings()
- {
- var settings = AssetDatabase.LoadAssetAtPath<EditorLearnSettings>(SettingsFile);
- if (settings == null)
- {
- settings = ScriptableObject.CreateInstance<EditorLearnSettings>();
- settings.m_ConfigVersionCode = 0;
- if (!Directory.Exists(Path.GetDirectoryName(SettingsFile)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(SettingsFile));
- }
- AssetDatabase.CreateAsset(settings, SettingsFile);
- AssetDatabase.SaveAssets();
- }
- return settings;
- }
-
- /// <summary>
- /// 提供一个SerializedObject的对象
- /// </summary>
- /// <returns></returns>
- public static SerializedObject GetSerializedSettings()
- {
- return new SerializedObject(GetOrCreateSettings());
- }
- [CustomEditor(typeof(EditorLearnSettings))]
- public class EditorLearnSettingsView:Editor
- {
- protected override void OnHeaderGUI()
- {
- GUILayout.Label("请从Project Settings配置");
- }
- public override void OnInspectorGUI() { }
- }
我们继续在EditorLearnSettings类提供以下方法。
- /// <summary>
- /// 这里主要是制定我们的Settings路径
- /// </summary>
- private const string ProjectSettingPtah = "Project/编辑器学习";
-
- /// <summary>
- /// 将我们的Setting提供给Unity
- /// </summary>
- /// <returns></returns>
- [SettingsProvider]
- public static SettingsProvider CreateEditorLearnSettingsProvider()
- {
- ///创建一个SettingsProvider
- var provider = new SettingsProvider(ProjectSettingPtah, SettingsScope.Project)
- {
- guiHandler = (searchContext) =>
- {
- ///searchContext:提供用户搜索的信息
- ///这里是一个gui的处理行为
- var settings = EditorLearnSettings.GetSerializedSettings();
- EditorGUILayout.PropertyField(settings.FindProperty("m_ConfigVersionCode"), new GUIContent("版本码:"));
- settings.ApplyModifiedPropertiesWithoutUndo();
- },
-
- // 用于搜索的关键字,在Project 可以搜索对应的配置
- keywords = new HashSet<string>(new[] { "Learn", "KTGame" })
- };
-
- return provider;
- }
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- using System.IO;
-
- [CustomEditor(typeof(EditorLearnSettings))]
- public class EditorLearnSettingsView:Editor
- {
- protected override void OnHeaderGUI()
- {
- GUILayout.Label("请从Project Settings配置");
- }
- public override void OnInspectorGUI() { }
- }
-
- /// <summary>
- /// 用于存储我们的设置配置
- /// </summary>
- public class EditorLearnSettings : ScriptableObject
- {
- /// <summary>
- /// 用于我们设置的保存位置
- /// </summary>
- private const string SettingsFile= "Assets/Settings/EditorLearnSetting.asset";
-
- #region 此处可以存储我们自定义的配置信息
- /// <summary>
- /// 模拟我们的配置
- /// </summary>
- public int m_ConfigVersionCode = 1;
- #endregion
-
- /// <summary>
- /// 这里提供一个全局的设置获取方式
- /// </summary>
- /// <returns></returns>
- public static EditorLearnSettings GetOrCreateSettings()
- {
- var settings = AssetDatabase.LoadAssetAtPath<EditorLearnSettings>(SettingsFile);
- if (settings == null)
- {
- settings = ScriptableObject.CreateInstance<EditorLearnSettings>();
- settings.m_ConfigVersionCode = 0;
- if (!Directory.Exists(Path.GetDirectoryName(SettingsFile)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(SettingsFile));
- }
- AssetDatabase.CreateAsset(settings, SettingsFile);
- AssetDatabase.SaveAssets();
- }
- return settings;
- }
-
- /// <summary>
- /// 提供一个SerializedObject的获取
- /// </summary>
- /// <returns></returns>
- public static SerializedObject GetSerializedSettings()
- {
- return new SerializedObject(GetOrCreateSettings());
- }
-
- /// <summary>
- /// 这里主要是制定我们的Settings路径
- /// </summary>
- private const string ProjectSettingPtah = "Project/编辑器学习";
-
- /// <summary>
- /// 将我们的Setting提供给Unity
- /// </summary>
- /// <returns></returns>
- [SettingsProvider]
- public static SettingsProvider CreateEditorLearnSettingsProvider()
- {
- ///创建一个SettingsProvider
- var provider = new SettingsProvider(ProjectSettingPtah, SettingsScope.Project)
- {
- guiHandler = (searchContext) =>
- {
- ///searchContext:提供用户搜索的信息
- ///这里是一个gui的处理行为
- var settings = EditorLearnSettings.GetSerializedSettings();
- EditorGUILayout.PropertyField(settings.FindProperty("m_ConfigVersionCode"), new GUIContent("版本码:"));
- settings.ApplyModifiedPropertiesWithoutUndo();
- },
-
- // 用于搜索的关键字,在Project 可以搜索对应的配置
- keywords = new HashSet<string>(new[] { "Learn", "KTGame" })
- };
-
- return provider;
- }
- }
配置文件我们直接使用上面的文件。
- /// <summary>
- /// 用于存储我们的设置配置
- /// </summary>
- public class EditorLearnSettings : ScriptableObject
- {
- /// <summary>
- /// 用于我们设置的保存位置
- /// </summary>
- private const string SettingsFile= "Assets/Settings/EditorLearnSetting.asset";
-
- #region 此处可以存储我们自定义的配置信息
- /// <summary>
- /// 模拟我们的配置
- /// </summary>
- public int m_ConfigVersionCode = 1;
- #endregion
-
- /// <summary>
- /// 这里提供一个全局的设置获取方式
- /// </summary>
- /// <returns></returns>
- public static EditorLearnSettings GetOrCreateSettings()
- {
- var settings = AssetDatabase.LoadAssetAtPath<EditorLearnSettings>(SettingsFile);
- if (settings == null)
- {
- settings = ScriptableObject.CreateInstance<EditorLearnSettings>();
- settings.m_ConfigVersionCode = 0;
- if (!Directory.Exists(Path.GetDirectoryName(SettingsFile)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(SettingsFile));
- }
- AssetDatabase.CreateAsset(settings, SettingsFile);
- AssetDatabase.SaveAssets();
- }
- return settings;
- }
-
- /// <summary>
- /// 提供一个SerializedObject的获取
- /// </summary>
- /// <returns></returns>
- public static SerializedObject GetSerializedSettings()
- {
- return new SerializedObject(GetOrCreateSettings());
- }
- }
首先我们创建EditorLearnSettingsProvider类并继承SettingsProvider。
public class EditorLearnSettingsProvider: SettingsProvider{}
实现SettingsProvider构造函数。
public EditorLearnSettingsProvider(string path, SettingsScope scope) : base(path, scope) { }
重写OnGUI和OnActivate
- /// <summary>
- /// 当模块被激活时被调用
- /// </summary>
- /// <param name="searchContext">用户搜索的内容</param>
- /// <param name="rootElement">UIElements根节点。如果添加到此,则 SettingsProvider 使用 UIElements 而不是调用 SettingsProvider.OnGUI 来构建 UI。如果不添加到此 VisualElement,则必须使用 IMGUI 来构建 UI。</param>
- public override void OnActivate(string searchContext, VisualElement rootElement)
- {
- serializedObject = EditorLearnSettings.GetSerializedSettings();
- }
-
- public override void OnGUI(string searchContext)
- {
- EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ConfigVersionCode"), new GUIContent("版本码:"));
- }
- [SettingsProvider]
- public static SettingsProvider CreateMyCustomSettingsProvider()
- {
- var provider = new EditorLearnSettingsProvider(ProjectSettingPtah, SettingsScope.Project);
- provider.keywords = new[] { "Learn", "KTGame" };
- return provider;
- }
- using UnityEditor;
- using UnityEngine;
- using System.IO;
- using UnityEngine.UIElements;
-
- [CustomEditor(typeof(EditorLearnSettings))]
- public class EditorLearnSettingsView:Editor
- {
- protected override void OnHeaderGUI()
- {
- GUILayout.Label("请从Project Settings配置");
- }
- public override void OnInspectorGUI() { }
- }
-
- /// <summary>
- /// 用于存储我们的设置配置
- /// </summary>
- public class EditorLearnSettings : ScriptableObject
- {
- /// <summary>
- /// 用于我们设置的保存位置
- /// </summary>
- private const string SettingsFile= "Assets/Settings/EditorLearnSetting.asset";
-
- #region 此处可以存储我们自定义的配置信息
- /// <summary>
- /// 模拟我们的配置
- /// </summary>
- public int m_ConfigVersionCode = 1;
- #endregion
-
- /// <summary>
- /// 这里提供一个全局的设置获取方式
- /// </summary>
- /// <returns></returns>
- public static EditorLearnSettings GetOrCreateSettings()
- {
- var settings = AssetDatabase.LoadAssetAtPath<EditorLearnSettings>(SettingsFile);
- if (settings == null)
- {
- settings = ScriptableObject.CreateInstance<EditorLearnSettings>();
- settings.m_ConfigVersionCode = 0;
- if (!Directory.Exists(Path.GetDirectoryName(SettingsFile)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(SettingsFile));
- }
- AssetDatabase.CreateAsset(settings, SettingsFile);
- AssetDatabase.SaveAssets();
- }
- return settings;
- }
-
- /// <summary>
- /// 提供一个SerializedObject的获取
- /// </summary>
- /// <returns></returns>
- public static SerializedObject GetSerializedSettings()
- {
- return new SerializedObject(GetOrCreateSettings());
- }
- }
-
- public class EditorLearnSettingsProvider: SettingsProvider
- {
- private const string ProjectSettingPtah = "Project/编辑器学习";
- public EditorLearnSettingsProvider(string path, SettingsScope scope) : base(path, scope) { }
-
- private SerializedObject serializedObject;
- /// <summary>
- /// 当模块被激活时被调用
- /// </summary>
- /// <param name="searchContext">用户搜索的内容</param>
- /// <param name="rootElement">UIElements根节点。如果添加到此,则 SettingsProvider 使用 UIElements 而不是调用 SettingsProvider.OnGUI 来构建 UI。如果不添加到此 VisualElement,则必须使用 IMGUI 来构建 UI。</param>
- public override void OnActivate(string searchContext, VisualElement rootElement)
- {
- serializedObject = EditorLearnSettings.GetSerializedSettings();
- }
-
- public override void OnGUI(string searchContext)
- {
- EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ConfigVersionCode"), new GUIContent("版本码:"));
- }
-
- [SettingsProvider]
- public static SettingsProvider CreateMyCustomSettingsProvider()
- {
- var provider = new EditorLearnSettingsProvider(ProjectSettingPtah, SettingsScope.Project);
- provider.keywords = new[] { "Learn", "KTGame" };
- return provider;
- }
- }