Skip to content

Commit dada5d5

Browse files
author
Bruno Mikoski
committed
add: features and small bugfixing
1 parent 02f56f7 commit dada5d5

7 files changed

Lines changed: 74 additions & 7 deletions

File tree

CHANGELOG.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77

88
## [1.3.2]
9+
### Added
10+
- Automatically selection of the newly created item, for renaming
11+
- Added keyword validation for names
12+
- Added ability to create new items from the selection dropdown.
13+
914
### Changed
1015
- Fixed issue when trying to play without the `CollectionRegistry` created
1116

Scripts/Editor/CollectableDropdown.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using System;
22
using System.Linq;
3+
using UnityEditor;
34
using UnityEditor.IMGUI.Controls;
45
using UnityEngine;
6+
using Object = UnityEngine.Object;
57

68
namespace BrunoMikoski.ScriptableObjectCollections
79
{
810
public sealed class CollectableDropdown : AdvancedDropdown
911
{
12+
private const string CREATE_NEW_TEXT = "+ Create New";
1013
private ScriptableObjectCollection collection;
1114
private Action<CollectableScriptableObject> callback;
1215

@@ -33,6 +36,8 @@ protected override AdvancedDropdownItem BuildRoot()
3336
parent.AddChild(new CollectableDropdownItem(collectionItem));
3437
}
3538
}
39+
40+
root.AddChild(new AdvancedDropdownItem(CREATE_NEW_TEXT));
3641
return root;
3742
}
3843

@@ -53,6 +58,16 @@ private AdvancedDropdownItem GetOrCreateDropdownItemForType(AdvancedDropdownItem
5358
protected override void ItemSelected(AdvancedDropdownItem item)
5459
{
5560
base.ItemSelected(item);
61+
62+
if (item.name.Equals(CREATE_NEW_TEXT, StringComparison.OrdinalIgnoreCase))
63+
{
64+
CollectableScriptableObject collectable = collection.AddNew(collection.GetCollectionType());
65+
callback?.Invoke(collectable);
66+
Selection.objects = new Object[] {collection};
67+
ScriptableObjectCollectionCustomEditor.LastAddedEnum = collectable;
68+
return;
69+
}
70+
5671
if (item is CollectableDropdownItem collectableDropdownItem)
5772
callback?.Invoke(collectableDropdownItem.Collectable);
5873
else

Scripts/Editor/CreateCollectionWizzard.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ private void OnGUI()
152152
EditorGUILayout.Space();
153153

154154
collectableName = EditorGUILayout.TextField("Collectable Name", collectableName);
155-
156155
collectionName = EditorGUILayout.TextField("Collection Name", collectionName);
157156

158157
generateIndirectAccess = EditorGUILayout.Toggle("Generate Indirect Access", generateIndirectAccess);

Scripts/Editor/ScriptableObjectCollectionCustomEditor.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private enum Warning
3030
private SearchField searchField;
3131
private bool showSettings;
3232
private Warning warnings;
33-
33+
public static CollectableScriptableObject LastAddedEnum;
3434

3535
private static bool isWaitingForNewTypeBeCreated
3636
{
@@ -282,7 +282,7 @@ public static void AfterStaticAssemblyReload()
282282
out ScriptableObjectCollection collection))
283283
{
284284
Selection.activeObject = null;
285-
collection.AddNew(targetType);
285+
LastAddedEnum = collection.AddNew(targetType);
286286
EditorApplication.delayCall += () =>
287287
{
288288
Selection.activeObject = collection;
@@ -292,7 +292,7 @@ public static void AfterStaticAssemblyReload()
292292

293293
private void AddNewItemOfType(Type targetType)
294294
{
295-
collection.AddNew(targetType);
295+
LastAddedEnum = collection.AddNew(targetType);
296296
filteredItemListDirty = true;
297297
}
298298

@@ -351,11 +351,29 @@ private void DrawItem(int index)
351351

352352
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
353353
{
354+
GUI.SetNextControlName(collectionItem.GUID);
355+
354356
string newName = EditorGUILayout.DelayedTextField(collectionItem.name, CollectionEditorGUI.ItemNameStyle,
355357
GUILayout.ExpandWidth(true));
356358

359+
if (LastAddedEnum == collectionItem)
360+
{
361+
EditorGUI.FocusTextInControl(collectionItem.GUID);
362+
LastAddedEnum = null;
363+
}
364+
357365
if (changeCheck.changed)
358-
AssetDatabaseUtils.RenameAsset(collectionItem, newName);
366+
{
367+
if (newName.IsReservedKeyword())
368+
{
369+
Debug.LogError($"{newName} is a reserved C# keyword, will cause issues with " +
370+
$"code generation, reverting to previous name");
371+
}
372+
else
373+
{
374+
AssetDatabaseUtils.RenameAsset(collectionItem, newName);
375+
}
376+
}
359377
}
360378

361379
DrawSelectItem(collectionItem);

Scripts/Runtime/Extensions/StringExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ public static class StringExtensions
1414
static readonly Regex FIRST_CHAR_FOLLOWED_BY_UPPER_CASES_ONLY = new Regex("(?<=[A-Z])[A-Z0-9]+$");
1515
static readonly Regex LOWER_CASE_NEXT_TO_NUMBER = new Regex("(?<=[0-9])[a-z]");
1616
static readonly Regex UPPER_CASE_INSIDE = new Regex("(?<=[A-Z])[A-Z]+?((?=[A-Z][a-z])|(?=[0-9]))");
17+
18+
private static string[] RESERVED_KEYWORDS =
19+
{
20+
"abstract", "as", "base", " bool", " break", "byte", "case", "catch", "char", "checked", "class", "const",
21+
"continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern",
22+
"false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface",
23+
"internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override",
24+
"params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short",
25+
"sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof",
26+
"uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while", "add",
27+
"alias", "ascending", "async", "await", "by", "descending", "dynamic", "equals", "from", "get", "global",
28+
"group", "into", "join", "let", "nameof", "notnull", "on", "orderby", "partial", "partial", "remove",
29+
"select", "set", "unmanaged", "value", "var", "when", "where", "where", "with", "yield","values"
30+
};
1731

1832
public static string Sanitize(this string input)
1933
{
@@ -123,5 +137,17 @@ public static string FirstToUpper(this string input)
123137
{
124138
return char.ToUpper(input[0]) + input.Substring(1);
125139
}
140+
141+
public static bool IsReservedKeyword(this string targetName)
142+
{
143+
for (int i = 0; i < RESERVED_KEYWORDS.Length; i++)
144+
{
145+
string reservedKeyword = RESERVED_KEYWORDS[i];
146+
if (reservedKeyword.Equals(targetName, StringComparison.OrdinalIgnoreCase))
147+
return true;
148+
}
149+
150+
return false;
151+
}
126152
}
127153
}

Scripts/Runtime/ScriptableObjectCollection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ public CollectableScriptableObject AddNew(Type collectionType, string assetName
157157
}
158158

159159
item.name = itemName;
160+
161+
if(itemName.IsReservedKeyword())
162+
Debug.LogError($"{itemName} is a reserved keyword name, will cause issues with code generation, please rename it");
163+
160164
string newFileName = Path.Combine(parentFolderPath, item.name + ".asset");
161165

162166
this.Add(item);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "com.brunomikoski.scriptableobjectcollection",
33
"displayName": "Scriptable Object Collection",
4-
"version": "1.3.1",
4+
"version": "1.3.2",
55
"unity": "2018.4",
6-
"description": "Scriptable Object Collection",
6+
"description": "A library to help improve the usability of Unity3D Scriptable Objects by grouping then into a collection and exposing then by code or nice inspectors!",
77
"keywords": [
88
"scriptable object",
99
"collection",

0 commit comments

Comments
 (0)