Skip to content

Commit 4c9c8fd

Browse files
author
Bruno Mikoski
committed
add: ui manager changes and updates
1 parent 28381ac commit 4c9c8fd

9 files changed

Lines changed: 123 additions & 53 deletions

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ private void DrawItem(int index)
339339
{
340340
using (new EditorGUILayout.HorizontalScope())
341341
{
342-
CollectionUtility.SetFoldoutOpen(collectionItem,
343-
EditorGUILayout.Toggle(GUIContent.none, CollectionUtility.IsFoldoutOpen(collectionItem), EditorStyles.foldout,
344-
GUILayout.Width(13)));
342+
CollectionUtility.SetFoldoutOpen(EditorGUILayout.Toggle(GUIContent.none,
343+
CollectionUtility.IsFoldoutOpen(collectionItem, target), EditorStyles.foldout,
344+
GUILayout.Width(13)), collectionItem, target);
345345

346346
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
347347
{
@@ -358,7 +358,7 @@ private void DrawItem(int index)
358358
DrawDeleteButton(collectionItem);
359359
}
360360

361-
if (CollectionUtility.IsFoldoutOpen(collectionItem))
361+
if (CollectionUtility.IsFoldoutOpen(collectionItem, target))
362362
{
363363
EditorGUI.indentLevel++;
364364
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 createFoldForThisCollection,
10+
string targetName) where T : ScriptableObject
11+
{
12+
return CreateScriptableObjectOfType<T>(AssetDatabase.GetAssetPath(parentFolder),
13+
createFoldForThisCollection, targetName);
14+
}
15+
16+
public static T CreateScriptableObjectOfType<T>(string parentFolderPath, bool createFoldForThisCollection,
17+
string targetName) where T : ScriptableObject
18+
{
19+
T targetCollection = ScriptableObject.CreateInstance<T>();
20+
targetCollection.name = targetName;
21+
22+
string targetFolderPath = parentFolderPath;
23+
if (createFoldForThisCollection)
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/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/Runtime/CollectionsRegistry.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ public ScriptableObjectCollection GetCollectionByGUID(string guid)
9797
return null;
9898
}
9999

100+
public bool TryGetCollection<T>(out T resultCollection) where T: ScriptableObjectCollection
101+
{
102+
for (int i = 0; i < collections.Count; i++)
103+
{
104+
ScriptableObjectCollection scriptableObjectCollection = collections[i];
105+
if (scriptableObjectCollection is T collectionT)
106+
{
107+
resultCollection = collectionT;
108+
return true;
109+
}
110+
}
111+
112+
resultCollection = null;
113+
return false;
114+
}
115+
116+
100117
public bool TryGetCollectionForType(Type targetCollectionType, out ScriptableObjectCollection scriptableObjectCollection)
101118
{
102119
for (int i = 0; i < collections.Count; i++)

Scripts/Runtime/ScriptableObjectCollection.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public bool Add(CollectableScriptableObject item)
128128
}
129129

130130
#if UNITY_EDITOR
131-
public CollectableScriptableObject AddNew(Type collectionType)
131+
132+
public CollectableScriptableObject AddNew(Type collectionType, string assetName = "")
132133
{
133134
if (Application.isPlaying)
134135
throw new NotSupportedException();
@@ -138,17 +139,21 @@ public CollectableScriptableObject AddNew(Type collectionType)
138139
string parentFolderPath = Path.Combine(assetPath, "Items");
139140
AssetDatabaseUtils.CreatePathIfDontExist(parentFolderPath);
140141

141-
string itemName;
142-
int count = Count;
143-
while (true)
142+
string itemName = assetName;
143+
144+
if (string.IsNullOrEmpty(itemName))
144145
{
145-
itemName = $"New{collectionType.Name}{count}";
146-
string testPath = Path.Combine(parentFolderPath, itemName);
146+
int count = Count;
147+
while (true)
148+
{
149+
itemName = $"New{collectionType.Name}{count}";
150+
string testPath = Path.Combine(parentFolderPath, itemName);
147151

148-
if (!File.Exists(Path.GetFullPath($"{testPath}.asset")))
149-
break;
152+
if (!File.Exists(Path.GetFullPath($"{testPath}.asset")))
153+
break;
150154

151-
count++;
155+
count++;
156+
}
152157
}
153158

154159
item.name = itemName;
@@ -396,6 +401,18 @@ public class ScriptableObjectCollection<ObjectType> : ScriptableObjectCollection
396401
set => base[index] = value;
397402
}
398403

404+
#if UNITY_EDITOR
405+
public ObjectType AddNew(string targetName)
406+
{
407+
return (ObjectType) AddNew(GetCollectionType(), targetName);
408+
}
409+
#endif
410+
411+
public ObjectType AddNew()
412+
{
413+
return (ObjectType)AddNew(GetCollectionType());
414+
}
415+
399416
public ObjectType GetCollectableByGUID(string targetGUID)
400417
{
401418
for (int i = 0; i < Items.Count; i++)

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
"author": {
2020
"name": "Bruno Mikoski",
2121
"url": "https://www.brunomikoski.com"
22-
}
23-
}
22+
},
23+
"type": "library"
24+
}

0 commit comments

Comments
 (0)