Skip to content

Commit 361e691

Browse files
committed
feat: Moved SearchbarMinItemsCount and UseBuiltInNames attribute properties to Project Settings
1 parent 8b06c32 commit 361e691

10 files changed

+117
-23
lines changed

Editor/Drawers/TypeDropdownDrawer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public TypeDropdownDrawer(Type selectedType, TypeOptionsAttribute attribute, Typ
2929
public void Draw(Action<Type> onTypeSelected)
3030
{
3131
var dropdownItems = GetDropdownItems();
32-
var selectionTree = new SelectionTree(dropdownItems, _selectedType, onTypeSelected, _attribute.SearchbarMinItemsCount, _attribute.ExcludeNone);
32+
var selectionTree = new SelectionTree(dropdownItems, _selectedType, onTypeSelected, ProjectSettings.SearchbarMinItemsCount, _attribute.ExcludeNone);
3333

3434
if (_attribute.ExpandAllFolders)
3535
selectionTree.ExpandAllFolders();
@@ -81,7 +81,7 @@ private TypeItem[] GetFilteredTypes()
8181
{
8282
bool containsMSCorLib = false;
8383

84-
var typeRelatedAssemblies = _attribute.UseBuiltInNames
84+
var typeRelatedAssemblies = ProjectSettings.UseBuiltInNames
8585
? TypeCollector.GetAssembliesTypeHasAccessTo(_declaringType, out containsMSCorLib)
8686
: TypeCollector.GetAssembliesTypeHasAccessTo(_declaringType);
8787

@@ -90,7 +90,7 @@ private TypeItem[] GetFilteredTypes()
9090

9191
var filteredTypes = TypeCollector.GetFilteredTypesFromAssemblies(typeRelatedAssemblies, _attribute);
9292

93-
bool replaceBuiltInNames = _attribute.UseBuiltInNames && containsMSCorLib;
93+
bool replaceBuiltInNames = ProjectSettings.UseBuiltInNames && containsMSCorLib;
9494

9595
int filteredTypesLength = filteredTypes.Count;
9696

Editor/Drawers/TypeFieldDrawer.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ internal class TypeFieldDrawer
2020
private readonly SerializedTypeReference _serializedTypeRef;
2121
private readonly TypeDropdownDrawer _dropdownDrawer;
2222
private readonly bool _showShortName;
23-
private readonly bool _useBuiltInNames;
2423
private readonly Rect _position;
2524
private readonly Action<Type> _onTypeSelected;
2625

@@ -31,15 +30,13 @@ public TypeFieldDrawer(
3130
Rect position,
3231
TypeDropdownDrawer dropdownDrawer,
3332
bool showShortName,
34-
bool useBuiltInNames,
3533
Action<Type> onTypeSelected = null,
3634
bool triggerDropdown = false)
3735
{
3836
_serializedTypeRef = serializedTypeRef;
3937
_position = position;
4038
_dropdownDrawer = dropdownDrawer;
4139
_showShortName = showShortName;
42-
_useBuiltInNames = useBuiltInNames;
4340
_onTypeSelected = onTypeSelected;
4441
_triggerDropdown = triggerDropdown;
4542
}
@@ -124,7 +121,7 @@ private void DrawFieldContent(int controlID)
124121

125122
private string GetTypeToShow(string typeName)
126123
{
127-
if (_useBuiltInNames)
124+
if (ProjectSettings.UseBuiltInNames)
128125
{
129126
string builtInName = typeName.ReplaceWithBuiltInName();
130127
if (builtInName != typeName)

Editor/Drawers/TypeReferencePropertyDrawer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ private void DrawTypeReferenceField(Rect position, SerializedProperty property)
5454
serializedTypeRef,
5555
position,
5656
dropdownDrawer,
57-
typeOptionsAttribute.ShortName,
58-
typeOptionsAttribute.UseBuiltInNames);
57+
typeOptionsAttribute.ShortName);
5958

6059
fieldDrawer.Draw();
6160
}

Editor/ProjectSettings.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace TypeReferences.Editor
2+
{
3+
using UnityEditor.SettingsManagement;
4+
5+
internal static class ProjectSettings
6+
{
7+
private const string PackageName = "com.solidalloy.type-references";
8+
9+
private static Settings _instance;
10+
11+
private static UserSetting<int> _searchbarMinItemsCount;
12+
13+
public static int SearchbarMinItemsCount
14+
{
15+
get
16+
{
17+
InitializeIfNeeded();
18+
return _searchbarMinItemsCount.value;
19+
}
20+
21+
set => _searchbarMinItemsCount.value = value;
22+
}
23+
24+
private static UserSetting<bool> _useBuiltInNames;
25+
26+
public static bool UseBuiltInNames
27+
{
28+
get
29+
{
30+
InitializeIfNeeded();
31+
return _useBuiltInNames.value;
32+
}
33+
34+
set => _useBuiltInNames.value = value;
35+
}
36+
37+
private static void InitializeIfNeeded()
38+
{
39+
if (_instance != null)
40+
return;
41+
42+
_instance = new Settings(PackageName);
43+
44+
_searchbarMinItemsCount = new UserSetting<int>(_instance, nameof(_searchbarMinItemsCount), 10);
45+
_useBuiltInNames = new UserSetting<bool>(_instance, nameof(_useBuiltInNames), true);
46+
}
47+
}
48+
}

Editor/ProjectSettings.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/ProjectSettingsDrawer.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace TypeReferences.Editor
2+
{
3+
using System.Collections.Generic;
4+
using SolidUtilities.Editor.Helpers;
5+
using UnityEditor;
6+
using UnityEngine;
7+
8+
internal static class ProjectSettingsDrawer
9+
{
10+
private const string SearchbarLabel = "Search bar minimum items count";
11+
private const string SearchbarTooltip = "The minimum number of items in the drop-down for the search bar to appear.";
12+
13+
private const string BuiltInLabel = "Use built-in names";
14+
private const string BuiltInTooltip = "Whether to make dropdown show built-in types by their keyword name (e.g. int) instead of the full name";
15+
16+
[SettingsProvider]
17+
public static SettingsProvider CreateSettingsProvider()
18+
{
19+
return new SettingsProvider("Project/Type References", SettingsScope.Project)
20+
{
21+
guiHandler = OnGUI,
22+
keywords = GetKeywords()
23+
};
24+
}
25+
26+
private static void OnGUI(string searchContext)
27+
{
28+
using var _ = EditorGUIUtilityHelper.LabelWidthBlock(220f);
29+
ProjectSettings.SearchbarMinItemsCount = EditorGUILayout.IntField(new GUIContent(SearchbarLabel, SearchbarTooltip), ProjectSettings.SearchbarMinItemsCount, GUILayout.MaxWidth(300f));
30+
ProjectSettings.UseBuiltInNames = EditorGUILayout.Toggle(new GUIContent(BuiltInLabel, BuiltInTooltip), ProjectSettings.UseBuiltInNames);
31+
}
32+
33+
private static HashSet<string> GetKeywords()
34+
{
35+
var keywords = new HashSet<string>();
36+
keywords.AddWords(SearchbarLabel);
37+
keywords.AddWords(SearchbarTooltip);
38+
keywords.AddWords(BuiltInLabel);
39+
keywords.AddWords(BuiltInTooltip);
40+
return keywords;
41+
}
42+
43+
private static readonly char[] _separators = { ' ' };
44+
45+
private static void AddWords(this HashSet<string> set, string phrase)
46+
{
47+
foreach (string word in phrase.Split(_separators))
48+
{
49+
set.Add(word);
50+
}
51+
}
52+
}
53+
}

Editor/ProjectSettingsDrawer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/TypeReferences.Editor.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"references": [
55
"GUID:ea6a6314f279f814facc8c48a7d3c9f1",
66
"GUID:b73ef07971851e14fa74ae50b76f8225",
7-
"GUID:48d58eaab94e1de429344393b7653b1f"
7+
"GUID:48d58eaab94e1de429344393b7653b1f",
8+
"GUID:49818357e697641afb75d2f8acaf1861"
89
],
910
"includePlatforms": [
1011
"Editor"

Runtime/Attributes/TypeOptionsAttribute.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,12 @@ public class TypeOptionsAttribute : PropertyAttribute
5050
/// </summary>
5151
[PublicAPI] public bool ExpandAllFolders;
5252

53-
/// <summary>
54-
/// Sets the minimum number of items in the drop-down for the search bar to appear. Defaults to 10.
55-
/// </summary>
56-
[PublicAPI] public int SearchbarMinItemsCount = 10;
57-
5853
/// <summary>
5954
/// Makes the field show the short name of the selected type instead of the full one.
6055
/// <see langword="false"/> by default.
6156
/// </summary>
6257
[PublicAPI] public bool ShortName;
6358

64-
/// <summary>
65-
/// Whether to make dropdown show built-in types by their keyword name (int) instead of the full name
66-
/// (System.Int32). Defaults to <see langword="true"/>.
67-
/// </summary>
68-
[PublicAPI] public bool UseBuiltInNames = true;
69-
7059
/// <summary>
7160
/// If enabled, shows only types that can be serialized by Unity. Defaults to <see langword="false"/>.
7261
/// </summary>

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"displayName": "Type References",
55
"description": "A plugin that allows selecting a type from a drop-down menu in the inspector.",
66
"dependencies": {
7-
"com.solidalloy.util": "1.28.0"
7+
"com.solidalloy.util": "1.28.0",
8+
"com.unity.settings-manager": "1.0.3"
89
},
910
"keywords": [
1011
"dropdown",
@@ -25,4 +26,4 @@
2526
"path": "Samples~/Usage Examples"
2627
}
2728
]
28-
}
29+
}

0 commit comments

Comments
 (0)