Skip to content

Commit 8f5ee34

Browse files
authored
Merge pull request #41 from brunomikoski/feature/ui-manager-features
Feature/ui manager features
2 parents 28381ac + 54fd1a5 commit 8f5ee34

13 files changed

Lines changed: 220 additions & 89 deletions

CHANGELOG.MD

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
44
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

7+
## [1.2.9]
8+
### Changed
9+
- Fixed issue with the Foldout issue, now showing multiple inherited collectables should work fine
10+
- Improved the ADD New on the collection and what is displayed, and the the creation of new types is on a subfolder.
11+
- Added static access to the `ScriptableObjectCollection<>.Values`
12+
- Renamed the generated Static value back to the `.Values` to keep in line with the static access. so you can now access by `Collection.Values` or `Collectable.Values`
13+
14+
### Added
15+
- Support to create Collections / Collectables based on a Utils script, useful for another packages creating the necessary Collection.
16+
717
## [1.2.8]
818
### Changed
919
- Fixed issue when trying to create a new type from abstract using the new Collectable editor.
@@ -143,9 +153,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
143153
- First initial working version
144154

145155
### [Unreleased]
146-
- Proper setup for automatic creation of necessary collections
147156

148-
[1.2.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.8
157+
[1.2.9]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.9
158+
[1.2.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.8
149159
[1.2.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.7
150160
[1.2.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.6
151161
[1.2.5]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.5

Scripts/Editor/CollectableScriptableObjectPropertyDrawer.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ private CollectableEditorOptionsAttribute optionsAttribute
3333
private GUIContent[] GUIContents;
3434

3535
private CollectableScriptableObject collectableItem;
36-
37-
private Object foldoutObject;
36+
private Object currentObject;
3837

3938
~CollectableScriptableObjectPropertyDrawer()
4039
{
@@ -104,7 +103,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
104103

105104
if (collectableItem != null)
106105
{
107-
if (CollectionUtility.IsFoldoutOpen(foldoutObject))
106+
if (CollectionUtility.IsFoldoutOpen(collectableItem, currentObject))
108107
{
109108
EditorGUI.indentLevel++;
110109
using (new EditorGUILayout.VerticalScope("Box"))
@@ -162,7 +161,7 @@ private void Initialize(SerializedProperty property)
162161
optionsNames = displayOptions.ToArray();
163162
GUIContents = optionsNames.Select(s => new GUIContent(s)).ToArray();
164163

165-
foldoutObject = property.serializedObject.targetObject;
164+
currentObject = property.serializedObject.targetObject;
166165
initialized = true;
167166
}
168167

@@ -212,12 +211,12 @@ private void DrawEditFoldoutButton(ref Rect popupRect)
212211
buttonRect.x += popupRect.width;
213212

214213
GUIContent guiContent = CollectionEditorGUI.EditGUIContent;
215-
if (CollectionUtility.IsFoldoutOpen(foldoutObject))
214+
if (CollectionUtility.IsFoldoutOpen(collectableItem, currentObject))
216215
guiContent = CollectionEditorGUI.CloseGUIContent;
217216

218217
if (GUI.Button(buttonRect, guiContent))
219218
{
220-
CollectionUtility.SetFoldoutOpen(foldoutObject, !CollectionUtility.IsFoldoutOpen(foldoutObject));
219+
CollectionUtility.SetFoldoutOpen(!CollectionUtility.IsFoldoutOpen(collectableItem, currentObject), collectableItem, currentObject);
221220
ObjectUtility.SetDirty(collectableItem);
222221
}
223222
}

Scripts/Editor/CreateCollectionWizzard.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ private void CreateNewCollection()
215215
CreateIndirectAccess();
216216
WaitingRecompileForContinue = true;
217217

218-
LastCollectionScriptableObjectPath = CreateCollectionObject();
218+
ScriptableObjectCollection instance = ScriptableObjectCollectionUtils.CreateScriptableObjectOfType<ScriptableObjectCollection>(ScriptableObjectFolder, createFoldForThisCollection, collectionName);
219+
LastCollectionScriptableObjectPath = AssetDatabase.GetAssetPath(instance);
219220
AssetDatabase.SaveAssets();
220221
AssetDatabase.Refresh();
221222

@@ -262,22 +263,6 @@ private void CreateIndirectAccess()
262263
}
263264
}
264265

265-
private string CreateCollectionObject()
266-
{
267-
ScriptableObjectCollection targetCollection = CreateInstance<ScriptableObjectCollection>();
268-
targetCollection.name = collectionName;
269-
270-
string targetFolderPath = AssetDatabase.GetAssetPath(ScriptableObjectFolder);
271-
if (createFoldForThisCollection)
272-
targetFolderPath = Path.Combine(targetFolderPath, $"{collectionName}");
273-
274-
AssetDatabaseUtils.CreatePathIfDontExist(Path.Combine(targetFolderPath, "Items"));
275-
276-
string collectionAssetPath = Path.Combine(targetFolderPath, $"{collectionName}.asset");
277-
AssetDatabase.CreateAsset(targetCollection, collectionAssetPath);
278-
return collectionAssetPath;
279-
}
280-
281266
private bool CreateCollectableScript()
282267
{
283268
string folder = AssetDatabase.GetAssetPath(ScriptsFolder);

Scripts/Editor/ScriptableObjectCollectionCustomEditor.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ private void UpdateFilteredItemList()
150150

151151
for (int i = 0; i < filteredItemList.Count; i++)
152152
{
153+
if (filteredItemList[i] == null)
154+
continue;
155+
153156
filteredSerializedList.Add(new SerializedObject(filteredItemList[i]));
154157
}
155158

@@ -214,28 +217,35 @@ private void AddNewItem()
214217
{
215218
List<Type> collectableSubclasses = TypeUtility.GetAllSubclasses(collection.GetCollectionType(), true);
216219

220+
collectableSubclasses.Add(collection.GetCollectionType());
217221
GenericMenu optionsMenu = new GenericMenu();
218-
if (!collection.GetCollectionType().IsAbstract)
219-
{
220-
AddMenuOption(optionsMenu, collection.GetCollectionType().Name, () =>
221-
{
222-
EditorApplication.delayCall += () => { AddNewItemOfType(collection.GetCollectionType()); };
223-
});
224-
}
225222

226223
for (int i = 0; i < collectableSubclasses.Count; i++)
227224
{
228225
Type collectableSubClass = collectableSubclasses[i];
226+
if (collectableSubClass.IsAbstract)
227+
continue;
228+
229229
AddMenuOption(optionsMenu, collectableSubClass.Name, () =>
230230
{
231231
EditorApplication.delayCall += () => { AddNewItemOfType(collectableSubClass); };
232232
});
233233
}
234234

235-
AddMenuOption(optionsMenu, $"Create new Type : {collection.GetCollectionType().Name}", () =>
235+
optionsMenu.AddSeparator("");
236+
237+
for (int i = 0; i < collectableSubclasses.Count; i++)
236238
{
237-
EditorApplication.delayCall += () => { CreateAndAddNewItemOfType(collection.GetCollectionType()); };
238-
});
239+
Type collectableSubClass = collectableSubclasses[i];
240+
241+
if (collectableSubClass.IsSealed)
242+
continue;
243+
244+
AddMenuOption(optionsMenu, $"Create New/class $NEW : {collectableSubClass.Name}", () =>
245+
{
246+
EditorApplication.delayCall += () => { CreateAndAddNewItemOfType(collectableSubClass); };
247+
});
248+
}
239249

240250
optionsMenu.ShowAsContext();
241251
}
@@ -339,9 +349,9 @@ private void DrawItem(int index)
339349
{
340350
using (new EditorGUILayout.HorizontalScope())
341351
{
342-
CollectionUtility.SetFoldoutOpen(collectionItem,
343-
EditorGUILayout.Toggle(GUIContent.none, CollectionUtility.IsFoldoutOpen(collectionItem), EditorStyles.foldout,
344-
GUILayout.Width(13)));
352+
CollectionUtility.SetFoldoutOpen(EditorGUILayout.Toggle(GUIContent.none,
353+
CollectionUtility.IsFoldoutOpen(collectionItem, target), EditorStyles.foldout,
354+
GUILayout.Width(13)), collectionItem, target);
345355

346356
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
347357
{
@@ -358,7 +368,7 @@ private void DrawItem(int index)
358368
DrawDeleteButton(collectionItem);
359369
}
360370

361-
if (CollectionUtility.IsFoldoutOpen(collectionItem))
371+
if (CollectionUtility.IsFoldoutOpen(collectionItem, target))
362372
{
363373
EditorGUI.indentLevel++;
364374
Editor editor = CollectionUtility.GetOrCreateEditorForItem(collectionItem);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.IO;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace BrunoMikoski.ScriptableObjectCollections
6+
{
7+
public static class ScriptableObjectCollectionUtils
8+
{
9+
public static T CreateScriptableObjectOfType<T>(DefaultAsset parentFolder, bool createFolderForThisCollection,
10+
string targetName) where T : ScriptableObject
11+
{
12+
return CreateScriptableObjectOfType<T>(AssetDatabase.GetAssetPath(parentFolder),
13+
createFolderForThisCollection, targetName);
14+
}
15+
16+
public static T CreateScriptableObjectOfType<T>(string parentFolderPath, bool createFolderForThisCollection,
17+
string targetName) where T : ScriptableObject
18+
{
19+
T targetCollection = ScriptableObject.CreateInstance<T>();
20+
targetCollection.name = targetName;
21+
22+
string targetFolderPath = parentFolderPath;
23+
if (createFolderForThisCollection)
24+
targetFolderPath = Path.Combine(targetFolderPath, $"{targetName}");
25+
26+
AssetDatabaseUtils.CreatePathIfDontExist(Path.Combine(targetFolderPath, "Items"));
27+
28+
string collectionAssetPath = Path.Combine(targetFolderPath, $"{targetName}.asset");
29+
AssetDatabase.CreateAsset(targetCollection, collectionAssetPath);
30+
return targetCollection;
31+
}
32+
}
33+
}

Scripts/Editor/ScriptableObjectCollectionUtils.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.

Scripts/Editor/Utils/CodeGenerationUtility.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ private static string[] GetCollectionDirectives(ScriptableObjectCollection colle
239239
private static void WriteTryGetAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
240240
ref int indentation)
241241
{
242-
string cachedValuesName = $"collection";
243-
string valuesName = $"Collection";
242+
string cachedValuesName = $"values";
243+
string valuesName = $"Values";
244244
string tryGetValuesName = $"TryGet{valuesName}";
245245

246246
AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");
@@ -336,7 +336,7 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
336336
{
337337
bool isGeneratingCustomStaticFile = ScriptableObjectCollectionSettings.Instance.IsGeneratingCustomStaticFile(collection);
338338

339-
string cachedValuesName = "collection";
339+
string cachedValuesName = "values";
340340
AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");
341341

342342
for (int i = 0; i < collection.Items.Count; i++)
@@ -348,12 +348,12 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
348348

349349
AppendLine(writer, indentation);
350350

351-
string valuesName = $"Collection";
351+
string valuesName = $"Values";
352352

353353
if(!isGeneratingCustomStaticFile)
354354
{
355355
AppendLine(writer, indentation,
356-
$"public new static {collection.GetType().Name} {valuesName}");
356+
$"public static {collection.GetType().Name} {valuesName}");
357357
}
358358
else
359359
{

Scripts/Editor/Utils/CollectionUtility.cs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class CollectionUtility
1010
private static Dictionary<Object, Editor> itemToEditor =
1111
new Dictionary<Object, Editor>();
1212

13-
private static Dictionary<Object, bool> objectToFoldOut = new Dictionary<Object, bool>();
13+
private static Dictionary<int, bool> objectToFoldOut = new Dictionary<int, bool>();
1414

1515
[MenuItem("Assets/Create/ScriptableObject Collection/New Collection", false, 100)]
1616
private static void CreateNewItem()
@@ -28,6 +28,23 @@ private static void CreateSettings()
2828
ScriptableObjectCollectionSettings.LoadOrCreateInstance<ScriptableObjectCollection>();
2929
}
3030

31+
32+
private static int GetHasCount(Object[] objects)
33+
{
34+
int hasValue = 0;
35+
for (int i = 0; i < objects.Length; i++)
36+
{
37+
Object targetObj = objects[i];
38+
39+
if (targetObj == null)
40+
continue;
41+
42+
hasValue += targetObj.GetHashCode();
43+
}
44+
45+
return hasValue;
46+
}
47+
3148
public static Editor GetOrCreateEditorForItem(Object collectionItem)
3249
{
3350
if (itemToEditor.TryGetValue(collectionItem, out Editor customEditor))
@@ -37,28 +54,26 @@ public static Editor GetOrCreateEditorForItem(Object collectionItem)
3754
itemToEditor.Add(collectionItem, customEditor);
3855
return customEditor;
3956
}
40-
41-
public static bool IsFoldoutOpen(Object targetObject)
57+
58+
public static bool IsFoldoutOpen(params Object[] objects)
4259
{
43-
if (targetObject.IsNull())
60+
int hashCount = GetHasCount(objects);
61+
62+
if (hashCount == 0)
4463
return false;
45-
46-
bool value;
47-
if(!objectToFoldOut.TryGetValue(targetObject, out value))
48-
objectToFoldOut.Add(targetObject, value);
64+
65+
if(!objectToFoldOut.TryGetValue(hashCount, out bool value))
66+
objectToFoldOut.Add(hashCount, value);
4967

5068
return value;
5169
}
5270

53-
public static void SetFoldoutOpen(Object targetObject, bool value)
71+
public static void SetFoldoutOpen(bool value, params Object[] objects)
5472
{
55-
if (!objectToFoldOut.ContainsKey(targetObject))
56-
objectToFoldOut.Add(targetObject, value);
57-
else
58-
objectToFoldOut[targetObject] = value;
59-
}
73+
int hashCount = GetHasCount(objects);
6074

61-
75+
objectToFoldOut[hashCount] = value;
76+
}
6277
}
6378
}
6479

Scripts/Editor/Utils/TypeUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static List<Type> GetAllSubclasses(Type targetType, bool ignoreCache = fa
3737
{
3838
if (ignoreCache || !typeToSubclasses.TryGetValue(targetType, out List<Type> results))
3939
{
40-
results = AvailableTypes.Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(targetType)).ToList();
40+
results = AvailableTypes.Where(t => t.IsClass && t.IsSubclassOf(targetType)).ToList();
4141
if(!ignoreCache)
4242
typeToSubclasses.Add(targetType, results);
4343
}

Scripts/Runtime/CollectableScriptableObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public string GUID
1919
[SerializeField, HideInInspector]
2020
private ScriptableObjectCollection collection;
2121
public ScriptableObjectCollection Collection => collection;
22-
22+
2323
public void SetCollection(ScriptableObjectCollection collection)
2424
{
2525
this.collection = collection;

0 commit comments

Comments
 (0)