[UnityEditor] Preferences/Settings 커스텀 설정 추가 가이드
| 개요

SettingsProvider클래스와 Attribute를 사용해서 Project Settings 혹은 Preferences 창을 커스터마이즈하는 법을 소개하고자 한다.
| 설정파일 추가
[FilePath("UserSettings/CustomSettings.asset",
FilePathAttribute.Location.ProjectFolder)]
public sealed class CustomSettings: ScriptableSingleton<CustomSettings>
{
// 설정 값 정의
public string customSettingsValue = null;
// 저장 함수 추가 (인자는 설정파일의 텍스트 저장 여부)
public void Save() => Save(true);
// 비활성화 되었을 때, 설정이 저장되도록
void OnDisable() => Save();
}
| SettingsProvider를 재정의할 클래스 정의
sealed class CustomSettingsProvider : SettingsProvider
{
public CustomSettingsProvider ()
: base("Project/Custom Settings", SettingsScope.Project) {}
public override void OnGUI(string search)
{
var settings = CustomSettings.instance;
// Properties...
var customSettingsValue = settings.customSettingsValue;
EditorGUI.BeginChangeCheck();
customSettingsValue = EditorGUILayout.TextField("Custom Settings", customSettingsValue);
// 값이 변경되었을 경우 설정 값을 저장
if (EditorGUI.EndChangeCheck())
{
settings.customSettingsValue = customSettingsValue;
settings.Save();
}
}
[SettingsProvider]
public static SettingsProvider CreateCustomSettingsProvider()
=> new CustomSettingsProvider();
}

| 키워드 설정
public CustomSettingsProvider ()
: base("Project/Custom Settings", SettingsScope.Project)
{
keywords = new HashSet<string>( new []{ "CustomSetting" });
}
위와 같이 keywords를 설정해주면 검색창에서 해당 키워드로 리스트업할 수 있다.

| 커스텀 설정 바로가기
void OnGUI()
{
if (GUILayout.Button("Open my custom preference"))
{
SettingsService.OpenUserPreferences("Preferences/MyCustomPref");
}
if (GUILayout.Button("Open my custom project settings"))
{
SettingsService.OpenProjectSettings("Project/MyCustomSettings");
}
}
| 마치며
SettingsProvider 클래스와 Attribute, ScriptableSingleton을 활용해서 간단히 설정창을 커스텀하는 방법을 알아보았다.
SettingsProvider를 상속한 추가 클래스 정의 없이, 아래의 Action 멤버변수를 지정하면 위 예제와 같이 설정 상세 UI를 커스터마이즈할 수 있다.
- IMGUI: guiHandler
- UI Toolkit: activeHandler
| 참고
Unity - Scripting API: SettingsProvider
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
docs.unity3d.com
Unity - Scripting API: SettingsService
Success! 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. Close
docs.unity3d.com