Last active
February 26, 2021 09:17
-
-
Save giacomelli/623de59997fa6f2aaeeb231a2a789933 to your computer and use it in GitHub Desktop.
#unitytips: SettingsProvider - http://diegogiacomelli.com.br/unitytips-settings-provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace Giacomelli.Framework | |
{ | |
public class FrameworkSettings : ScriptableObject | |
{ | |
[SerializeField] | |
bool _entityLogEnabled; | |
[SerializeField] | |
bool _notificationsLogEnabled; | |
public static bool EntityLogEnabled { get; private set; } | |
public static bool NotificationsLogEnabled { get; private set; } | |
private void OnEnable() | |
{ | |
Refresh(this); | |
} | |
public static void Refresh(FrameworkSettings instance) | |
{ | |
EntityLogEnabled = instance._entityLogEnabled; | |
NotificationsLogEnabled = instance._notificationsLogEnabled; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
namespace Giacomelli.Framework | |
{ | |
public static class FrameworkSettingsRegister | |
{ | |
const string SettingsPath = "Assets/Editor/Giacomelli.Framework.Settings.asset"; | |
[SettingsProvider] | |
public static SettingsProvider CreateSettingsProvider() | |
{ | |
var logFoldout = true; | |
return new SettingsProvider("Project/Giacomelli.Framework", SettingsScope.Project) | |
{ | |
guiHandler = (searchContext) => | |
{ | |
var settings = Load(); | |
var serialized = new SerializedObject(settings); | |
EditorGUI.BeginChangeCheck(); | |
logFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(logFoldout, "Logging"); | |
if (logFoldout) | |
{ | |
EditorGUILayout.PropertyField(serialized.FindProperty("_entityLogEnabled"), new GUIContent("Entity")); | |
EditorGUILayout.PropertyField(serialized.FindProperty("_notificationsLogEnabled"), new GUIContent("Notifications")); | |
EditorGUILayout.HelpBox("Those log messages will appear on console with the prefix [ENTITY] or [NOTIFICATIONS]", MessageType.Info); | |
} | |
EditorGUILayout.EndFoldoutHeaderGroup(); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
serialized.ApplyModifiedProperties(); | |
FrameworkSettings.Refresh(settings); | |
} | |
}, | |
keywords = new HashSet<string>(new[] { "Entity log enabled", "Notifications log enabled" }) | |
}; | |
} | |
static FrameworkSettings Load() | |
{ | |
var settings = AssetDatabase.LoadAssetAtPath<FrameworkSettings>(SettingsPath); | |
if (settings == null) | |
{ | |
settings = ScriptableObject.CreateInstance<FrameworkSettings>(); | |
AssetDatabase.CreateAsset(settings, SettingsPath); | |
AssetDatabase.SaveAssets(); | |
} | |
return settings; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment