Skip to content

Commit 101a9c5

Browse files
committed
Utility methods for the EditorPrefs API
1 parent c5eb953 commit 101a9c5

2 files changed

Lines changed: 46 additions & 26 deletions

File tree

Assets/Editor Toolbox/Editor/ToolboxEditorSettingsEditor.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ private void OnEnable()
4747
currentTarget = target as ToolboxEditorSettings;
4848

4949
//internal properties cached by 'EditorPrefs'
50-
hierarchyAnimBool = new AnimBool(EditorPrefs.GetBool(string.Format("{0}.HierarchyEnabled", nameof(ToolboxEditorSettings)), false));
51-
projectAnimBool = new AnimBool(EditorPrefs.GetBool(string.Format("{0}.ProjectEnabled", nameof(ToolboxEditorSettings)), false));
52-
inspectorAnimBool = new AnimBool(EditorPrefs.GetBool(string.Format("{0}.InspectorEnabled", nameof(ToolboxEditorSettings)), false));
50+
hierarchyAnimBool = new AnimBool(ToolboxPrefs.GetBool(nameof(ToolboxEditorSettings), "HierarchyEnabled"));
51+
projectAnimBool = new AnimBool(ToolboxPrefs.GetBool(nameof(ToolboxEditorSettings), "ProjectEnabled"));
52+
inspectorAnimBool = new AnimBool(ToolboxPrefs.GetBool(nameof(ToolboxEditorSettings), "InspectorEnabled"));
5353

54-
enabledToShowDrawerType = EditorPrefs.GetInt(string.Format("{0}.PickedDrawerType", nameof(ToolboxEditorSettings)), 0);
54+
enabledToShowDrawerType = ToolboxPrefs.GetInt(nameof(ToolboxEditorSettings), "PickedDrawerType");
5555

5656
var repaintAction = new UnityAction(() =>
5757
{
@@ -131,10 +131,10 @@ private void OnEnable()
131131

132132
private void OnDisable()
133133
{
134-
EditorPrefs.SetBool(string.Format("{0}.HierarchyEnabled", nameof(ToolboxEditorSettings)), hierarchyAnimBool.target);
135-
EditorPrefs.SetBool(string.Format("{0}.ProjectEnabled", nameof(ToolboxEditorSettings)), projectAnimBool.target);
136-
EditorPrefs.SetBool(string.Format("{0}.InspectorEnabled", nameof(ToolboxEditorSettings)), inspectorAnimBool.target);
137-
EditorPrefs.SetInt(string.Format("{0}.PickedDrawerType", nameof(ToolboxEditorSettings)), enabledToShowDrawerType);
134+
ToolboxPrefs.SetBool(nameof(ToolboxEditorSettings), "HierarchyEnabled", hierarchyAnimBool.target);
135+
ToolboxPrefs.SetBool(nameof(ToolboxEditorSettings), "ProjectEnabled", projectAnimBool.target);
136+
ToolboxPrefs.SetBool(nameof(ToolboxEditorSettings), "InspectorEnabled", inspectorAnimBool.target);
137+
ToolboxPrefs.SetInt(nameof(ToolboxEditorSettings), "PickedDrawerType", enabledToShowDrawerType);
138138
}
139139

140140
private void DrawHierarchySettings()
Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
using System;
2-
using System.Collections.Generic;
32

43
using UnityEditor;
54

65
namespace Toolbox.Editor
76
{
7+
/// <summary>
8+
/// Additional overlay for <see cref="EditorPrefs"/>.
9+
/// This utility class is Work in Progress.
10+
/// </summary>
811
internal static class ToolboxPrefs
912
{
10-
private const string separator = ".";
11-
private const string keyBase = "Toolbox";
12-
13-
private static readonly HashSet<string> usedKeys = new HashSet<string>();
14-
15-
16-
private static string GetKey(object causer, string propertyName)
17-
{
18-
return GetKey(causer.GetType(), propertyName);
19-
}
20-
2113
private static string GetKey(Type causer, string propertyName)
2214
{
2315
return GetKey(causer.Name, propertyName);
2416
}
2517

2618
private static string GetKey(string causer, string propertyName)
2719
{
28-
return string.Format("{0}{3}{1}{3}{2}", keyBase, causer, propertyName, separator);
20+
return string.Format("{0}{3}{1}{3}{2}", "Toolbox", causer, propertyName, ".");
2921
}
3022

3123

@@ -34,20 +26,48 @@ public static void DeleteAll()
3426
EditorPrefs.DeleteAll();
3527
}
3628

37-
public static void DeleteCached()
29+
public static bool GetBool(object causer, string propertyName, bool defaultValue = false)
3830
{
39-
//TODO:
31+
return GetBool(causer.GetType(), propertyName, defaultValue);
4032
}
4133

42-
public static bool GetBool(string causer, string propertyName, bool defaultValue)
34+
public static bool GetBool(Type causer, string propertyName, bool defaultValue = false)
4335
{
4436
var key = GetKey(causer, propertyName);
4537
return EditorPrefs.GetBool(key, defaultValue);
4638
}
4739

48-
public static T GetValue<T>(string causer, string propertyName, T defaultValue)
40+
public static void SetBool(object causer, string propertyName, bool value)
41+
{
42+
SetBool(causer.GetType(), propertyName, value);
43+
}
44+
45+
public static void SetBool(Type causer, string propertyName, bool value)
46+
{
47+
var key = GetKey(causer, propertyName);
48+
EditorPrefs.SetBool(key, value);
49+
}
50+
51+
public static int GetInt(object causer, string propertyName, int defaultValue = 0)
52+
{
53+
return GetInt(causer.GetType(), propertyName, defaultValue);
54+
}
55+
56+
public static int GetInt(Type causer, string propertyName, int defaultValue = 0)
57+
{
58+
var key = GetKey(causer, propertyName);
59+
return EditorPrefs.GetInt(key, defaultValue);
60+
}
61+
62+
public static void SetInt(object causer, string propertyName, int value)
63+
{
64+
SetInt(causer.GetType(), propertyName, value);
65+
}
66+
67+
public static void SetInt(Type causer, string propertyName, int value)
4968
{
50-
return default;
69+
var key = GetKey(causer, propertyName);
70+
EditorPrefs.SetInt(key, value);
5171
}
5272
}
5373
}

0 commit comments

Comments
 (0)