Skip to content

Commit de4e901

Browse files
authored
Merge pull request #20 from badawe/feature/fixes_and_qol
fix: more tweaks and improvements
2 parents 6e0478e + b5c1c59 commit de4e901

File tree

6 files changed

+94
-25
lines changed

6 files changed

+94
-25
lines changed

CHANGELOG.MD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## [1.1.7]
9+
## Added
10+
- Proper error when there's no static script folder defined
11+
- Default namespace on settings
12+
- Stored the last scripts folder as cache for the wizzard
13+
- Validation of null items on the Registry
14+
15+
## Changed
16+
- Changed to the static file use the Colllectable Type again
17+
- Values on the static files now its just values not `CollectableType+Values`
18+
819
## [1.1.6]
920
## Changed
1021
- Crash while changing to debug mode while having the Collection inspector open
@@ -81,6 +92,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8192
## [Unreleased]
8293
- Add a setup wizzard for first time settings creation
8394

95+
[1.1.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.7
8496
[1.1.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.6
8597
[1.1.5]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.5
8698
[1.1.4]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.4

Scripts/Editor/CreateCollectionWizzard.cs

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ namespace BrunoMikoski.ScriptableObjectCollections
99
{
1010
public sealed class CreateCollectionWizzard : EditorWindow
1111
{
12+
private const string WAITING_SCRIPTS_TO_RECOMPILE_TO_CONTINUE_KEY = "WaitingScriptsToRecompileToContinueKey";
1213
private const string LAST_COLLECTION_SCRIPTABLE_OBJECT_PATH_KEY = "CollectionScriptableObjectPathKey";
1314
private const string LAST_COLLECTION_FULL_NAME_KEY = "CollectionFullNameKey";
14-
private const string LAST_COLLECTION_SCRIPT_PATH_KEY = "CollectionScriptPathKey";
15+
private const string LAST_GENERATED_COLLECTION_SCRIPT_PATH_KEY = "CollectionScriptPathKey";
16+
private const string LAST_TARGET_SCRIPTS_FOLDER_KEY = "LastTargetScriptsFolder";
1517

1618
private DefaultAsset cachedScriptableObjectFolder;
1719
private DefaultAsset ScriptableObjectFolder
@@ -26,6 +28,12 @@ private DefaultAsset ScriptableObjectFolder
2628
cachedScriptableObjectFolder = AssetDatabase.LoadAssetAtPath<DefaultAsset>(targetFolder);
2729
return cachedScriptableObjectFolder;
2830
}
31+
32+
if (!string.IsNullOrEmpty(LastCollectionScriptableObjectPath))
33+
{
34+
cachedScriptableObjectFolder = AssetDatabase.LoadAssetAtPath<DefaultAsset>(LastCollectionScriptableObjectPath);
35+
return cachedScriptableObjectFolder;
36+
}
2937

3038
if (!string.IsNullOrEmpty(ScriptableObjectCollectionSettings.Instance.DefaultScriptableObjectsFolder))
3139
{
@@ -45,6 +53,12 @@ private DefaultAsset ScriptsFolder
4553
if (cachedScriptsFolder != null)
4654
return cachedScriptsFolder;
4755

56+
if (!string.IsNullOrEmpty(LastScriptsTargetFolder))
57+
{
58+
cachedScriptsFolder = AssetDatabase.LoadAssetAtPath<DefaultAsset>(Path.GetDirectoryName(LastScriptsTargetFolder));
59+
return cachedScriptsFolder;
60+
}
61+
4862
if (!string.IsNullOrEmpty(targetFolder))
4963
{
5064
cachedScriptsFolder = AssetDatabase.LoadAssetAtPath<DefaultAsset>(targetFolder);
@@ -64,11 +78,22 @@ private DefaultAsset ScriptsFolder
6478
private string cachedNameSpace;
6579
private string TargetNameSpace
6680
{
67-
get => cachedNameSpace;
81+
get
82+
{
83+
if (string.IsNullOrEmpty(cachedNameSpace))
84+
cachedNameSpace = ScriptableObjectCollectionSettings.Instance.DefaultNamespace;
85+
return cachedNameSpace;
86+
}
6887
set => cachedNameSpace = value;
6988
}
7089

7190

91+
private static bool WaitingRecompileForContinue
92+
{
93+
get => EditorPrefs.GetBool(WAITING_SCRIPTS_TO_RECOMPILE_TO_CONTINUE_KEY, false);
94+
set => EditorPrefs.SetBool(WAITING_SCRIPTS_TO_RECOMPILE_TO_CONTINUE_KEY, value);
95+
}
96+
7297
private static string LastCollectionScriptableObjectPath
7398
{
7499
get => EditorPrefs.GetString(LAST_COLLECTION_SCRIPTABLE_OBJECT_PATH_KEY, String.Empty);
@@ -81,10 +106,16 @@ private static string LastCollectionFullName
81106
set => EditorPrefs.SetString(LAST_COLLECTION_FULL_NAME_KEY, value);
82107
}
83108

84-
private static string LastCollectionScriptPath
109+
private static string LastScriptsTargetFolder
110+
{
111+
get => EditorPrefs.GetString(LAST_TARGET_SCRIPTS_FOLDER_KEY, String.Empty);
112+
set => EditorPrefs.SetString(LAST_TARGET_SCRIPTS_FOLDER_KEY, value);
113+
}
114+
115+
private static string LastGeneratedCollectionScriptPath
85116
{
86-
get => EditorPrefs.GetString(LAST_COLLECTION_SCRIPT_PATH_KEY, String.Empty);
87-
set => EditorPrefs.SetString(LAST_COLLECTION_SCRIPT_PATH_KEY, value);
117+
get => EditorPrefs.GetString(LAST_GENERATED_COLLECTION_SCRIPT_PATH_KEY, String.Empty);
118+
set => EditorPrefs.SetString(LAST_GENERATED_COLLECTION_SCRIPT_PATH_KEY, value);
88119
}
89120

90121
private bool createFoldForThisCollection = true;
@@ -175,7 +206,6 @@ private void CreateNewCollection()
175206
scriptsGenerated |= CreateCollectableScript();
176207
scriptsGenerated |= CreateCollectionScript();
177208

178-
LastCollectionScriptableObjectPath = CreateCollectionObject();
179209

180210
AssetDatabase.SaveAssets();
181211
AssetDatabase.Refresh();
@@ -202,15 +232,16 @@ private string CreateCollectionObject()
202232

203233
private bool CreateCollectableScript()
204234
{
205-
string targetFolder = AssetDatabase.GetAssetPath(ScriptsFolder);
235+
string folder = AssetDatabase.GetAssetPath(ScriptsFolder);
236+
LastScriptsTargetFolder = folder;
206237
if (createFoldForThisCollectionScripts)
207-
targetFolder = Path.Combine(targetFolder, $"{collectionName}");
238+
folder = Path.Combine(folder, $"{collectionName}");
208239

209240
return CodeGenerationUtility.CreateNewEmptyScript(collectableName,
210-
targetFolder,
241+
folder,
211242
TargetNameSpace,
212243
string.Empty,
213-
$"public class {collectableName} : CollectableScriptableObject",
244+
$"public partial class {collectableName} : CollectableScriptableObject",
214245
typeof(CollectableScriptableObject).Namespace);
215246
}
216247

@@ -224,14 +255,14 @@ private bool CreateCollectionScript()
224255
targetFolder,
225256
TargetNameSpace,
226257
$"[CreateAssetMenu(menuName = \"ScriptableObject Collection/Collections/Create {collectionName}\", fileName = \"{collectionName}\", order = 0)]",
227-
$"public partial class {collectionName} : ScriptableObjectCollection<{collectableName}>", typeof(ScriptableObjectCollection).Namespace, "UnityEngine");
258+
$"public class {collectionName} : ScriptableObjectCollection<{collectableName}>", typeof(ScriptableObjectCollection).Namespace, "UnityEngine");
228259

229260
if (string.IsNullOrEmpty(TargetNameSpace))
230261
LastCollectionFullName = $"{collectionName}";
231262
else
232263
LastCollectionFullName = $"{TargetNameSpace}.{collectionName}";
233264

234-
LastCollectionScriptPath = Path.Combine(targetFolder, $"{collectionName}.cs");
265+
LastGeneratedCollectionScriptPath = Path.Combine(targetFolder, $"{collectionName}.cs");
235266
return result;
236267
}
237268

@@ -255,13 +286,15 @@ private bool AreSettingsValid()
255286
[DidReloadScripts]
256287
static void OnAfterScriptsReloading()
257288
{
258-
if (string.IsNullOrEmpty(LastCollectionScriptableObjectPath))
289+
if (!WaitingRecompileForContinue)
259290
return;
260291

292+
WaitingRecompileForContinue = false;
293+
261294
ScriptableObjectCollection collectionAsset =
262295
AssetDatabase.LoadAssetAtPath<ScriptableObjectCollection>(LastCollectionScriptableObjectPath);
263296

264-
string assemblyName = CompilationPipeline.GetAssemblyNameFromScriptPath(LastCollectionScriptPath);
297+
string assemblyName = CompilationPipeline.GetAssemblyNameFromScriptPath(LastGeneratedCollectionScriptPath);
265298

266299
Type targetType = Type.GetType($"{LastCollectionFullName}, {assemblyName}");
267300

@@ -273,9 +306,6 @@ static void OnAfterScriptsReloading()
273306
collectionScriptableObject.FindProperty("m_Script").objectReferenceInstanceIDValue = typeId;
274307
collectionScriptableObject.ApplyModifiedProperties();
275308

276-
LastCollectionScriptableObjectPath = string.Empty;
277-
LastCollectionFullName = string.Empty;
278-
279309
Selection.objects = new[] {collectionScriptableObject.targetObject};
280310
EditorGUIUtility.PingObject(collectionScriptableObject.targetObject);
281311
GetWindowInstance().Close();

Scripts/Editor/Utils/CodeGenerationUtility.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Linq;
55
using System.Text;
66
using UnityEditor;
7+
using UnityEngine;
8+
using Object = UnityEngine.Object;
79

810
namespace BrunoMikoski.ScriptableObjectCollections
911
{
@@ -133,9 +135,17 @@ public static void GenerateStaticCollectionScript(ScriptableObjectCollection col
133135
{
134136
string dehumanizeCollectionName = collection.name.Sanitize();
135137

136-
string fileName = $"{dehumanizeCollectionName.FirstToUpper()}Static";
138+
string fileName = $"{collection.GetCollectionType().Name}Static";
137139
string nameSpace = collection.GetCollectionType().Namespace;
138140
string finalFolder = ScriptableObjectCollectionSettings.Instance.GetStaticFileFolderForCollection(collection);
141+
142+
if (string.IsNullOrEmpty(finalFolder))
143+
{
144+
Debug.LogError("Static Code Generation folder not assigned, please assign it on the ScriptableObjectCollectionSettings");
145+
EditorGUIUtility.PingObject(ScriptableObjectCollectionSettings.Instance);
146+
Selection.objects = new Object[] {ScriptableObjectCollectionSettings.Instance};
147+
return;
148+
}
139149

140150
AssetDatabaseUtils.CreatePathIfDontExist(finalFolder);
141151
using (StreamWriter writer = new StreamWriter(Path.Combine(finalFolder, $"{fileName}.cs")))
@@ -148,7 +158,7 @@ public static void GenerateStaticCollectionScript(ScriptableObjectCollection col
148158
directives.AddRange(GetCollectionDirectives(collection));
149159

150160
AppendHeader(writer, ref indentation, nameSpace,
151-
collection.GetType().Name, true, false, directives.Distinct().ToArray());
161+
collection.GetCollectionType().Name, true, false, directives.Distinct().ToArray());
152162

153163
GeneratedStaticFileType staticFileTypeForCollection = ScriptableObjectCollectionSettings.Instance.GetStaticFileTypeForCollection(collection);
154164
if (staticFileTypeForCollection == GeneratedStaticFileType.DirectAccess)
@@ -176,8 +186,8 @@ private static string[] GetCollectionDirectives(ScriptableObjectCollection colle
176186
private static void WriteTryGetAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
177187
ref int indentation)
178188
{
179-
string cachedValuesName = $"cached{collection.name.Sanitize().FirstToUpper()}Values";
180-
string valuesName = $"{collection.name.Sanitize().FirstToUpper()}Values";
189+
string cachedValuesName = $"values";
190+
string valuesName = $"Values";
181191
string tryGetValuesName = $"TryGet{valuesName}";
182192

183193
AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");
@@ -271,7 +281,7 @@ private static void WriteTryGetAccessCollectionStatic(ScriptableObjectCollection
271281
private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
272282
ref int indentation)
273283
{
274-
string cachedValuesName = $"cached{collection.name.Sanitize().FirstToUpper()}Values";
284+
string cachedValuesName = "values";
275285
AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");
276286

277287
for (int i = 0; i < collection.Items.Count; i++)
@@ -283,7 +293,7 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
283293

284294
AppendLine(writer, indentation);
285295

286-
string valuesName = $"{collection.name.Sanitize().FirstToUpper()}Values";
296+
string valuesName = $"Values";
287297
AppendLine(writer, indentation,
288298
$"public static {collection.GetType().Name} {valuesName}");
289299
AppendLine(writer, indentation, "{");

Scripts/Runtime/CollectionsRegistry.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,20 @@ public void UnregisterCollection(ScriptableObjectCollection targetCollection)
3737
collectionGUIDs.Remove(targetCollection.GUID);
3838
}
3939

40+
private void ValidateItems()
41+
{
42+
for (int i = collections.Count - 1; i >= 0; i--)
43+
{
44+
if (collections[i] == null)
45+
{
46+
collections.RemoveAt(i);
47+
collectionGUIDs.RemoveAt(i);
48+
}
49+
}
50+
}
4051
private void ValidateCurrentGUIDs()
4152
{
53+
ValidateItems();
4254
if (collectionGUIDs.Count != collections.Count)
4355
{
4456
ReloadCollections();

Scripts/Runtime/ScriptableObjectCollectionSettings.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ private class CollectionToSettings
4040

4141
[SerializeField]
4242
private UnityEditor.DefaultAsset defaultGeneratedCodeFolder;
43-
public string DefaultGeneratedCodeFolder => UnityEditor.AssetDatabase.GetAssetPath(defaultGeneratedCodeFolder);
43+
44+
private string DefaultGeneratedCodeFolder => UnityEditor.AssetDatabase.GetAssetPath(defaultGeneratedCodeFolder);
4445
#pragma warning restore 0649
4546
#endif
4647

@@ -50,6 +51,10 @@ private class CollectionToSettings
5051
[SerializeField, HideInInspector]
5152
private List<CollectionToSettings> collectionsSettings = new List<CollectionToSettings>();
5253

54+
[SerializeField]
55+
private string defaultNamespace;
56+
public string DefaultNamespace => defaultNamespace;
57+
5358
public GeneratedStaticFileType GetStaticFileTypeForCollection(ScriptableObjectCollection collection)
5459
{
5560
if (!TryGetSettingsForCollection(collection, out CollectionToSettings settings))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.brunomikoski.scriptableobjectcollection",
33
"displayName": "Scriptable Object Collection",
4-
"version": "1.1.6",
4+
"version": "1.1.7",
55
"unity": "2018.4",
66
"description": "Scriptable Object Collection",
77
"keywords": [

0 commit comments

Comments
 (0)