Skip to content

Commit 09473a5

Browse files
authored
Merge pull request #54 from brunomikoski/bugfix/fix-settings-creation
Bugfix/fix settings creation
2 parents 8e26daf + 344efb2 commit 09473a5

5 files changed

Lines changed: 53 additions & 22 deletions

File tree

CHANGELOG.MD

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ 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.4.1]
8+
### Changed
9+
- Fixed issue when using the Create Settings menu
10+
- Added automatically open of the selected collectable when using the goto button on the CollectableProperty Drawer
11+
- Added type specific GetEnumerator for the Collection
12+
713
## [1.4.0]
814
### Added
915
- Added quick access to the .Values from the collection, to be used without code generation, you can now use `CustomCollection.Values` to gett all your items
@@ -185,10 +191,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
185191
- First initial working version
186192

187193
### [Unreleased]
188-
- Added automatically open of the selected collectable when using the goto button on the CollectableProperty Drawer
189-
- Added type specific GetEnumerator for the Collection
190194

191195

196+
[1.4.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.4.1
197+
[1.4.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.4.0
192198
[1.3.2]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.3.2
193199
[1.3.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.3.1
194200
[1.3.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.3.0

Scripts/Editor/Utils/CollectionUtility.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ private static void CreateNewItem()
2525
[MenuItem("Assets/Create/ScriptableObject Collection/Create Settings", false, 200)]
2626
private static void CreateSettings()
2727
{
28-
ScriptableObjectCollectionSettings.LoadOrCreateInstance<ScriptableObjectCollection>();
28+
ScriptableObjectCollectionSettings.LoadOrCreateInstance<ScriptableObjectCollectionSettings>();
2929
}
30-
31-
30+
31+
[MenuItem("Assets/Create/ScriptableObject Collection/Create Settings", true, 200)]
32+
private static bool CreateSettings_Validation()
33+
{
34+
return !ScriptableObjectCollectionSettings.Exist();
35+
}
36+
3237
private static int GetHasCount(Object[] objects)
3338
{
3439
int hasValue = 0;

Scripts/Runtime/ResourceScriptableObjectSingleton.cs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,40 @@ public static T Instance
1818

1919
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
2020
public static T LoadOrCreateInstance<T>() where T : ScriptableObject
21+
{
22+
if (!TryToLoadInstance(out T resultInstance))
23+
{
24+
#if UNITY_EDITOR
25+
resultInstance = CreateInstance<T>();
26+
27+
AssetDatabaseUtils.CreatePathIfDontExist("Assets/Resources");
28+
UnityEditor.AssetDatabase.CreateAsset(resultInstance, $"Assets/Resources/{typeof(T).Name}.asset");
29+
UnityEditor.AssetDatabase.SaveAssets();
30+
UnityEditor.AssetDatabase.Refresh();
31+
return resultInstance;
32+
#endif
33+
return null;
34+
}
35+
36+
return resultInstance;
37+
}
38+
39+
public static bool Exist()
40+
{
41+
return TryToLoadInstance<T>(out _);
42+
}
43+
44+
private static bool TryToLoadInstance<T>(out T result) where T : ScriptableObject
2145
{
2246
T newInstance = Resources.Load<T>(typeof(T).Name);
2347

2448
if (newInstance != null)
25-
return newInstance;
49+
{
50+
result = newInstance;
51+
return true;
52+
}
2653

2754
#if UNITY_EDITOR
28-
if (Application.isPlaying)
29-
return null;
30-
3155
string registryGUID = UnityEditor.AssetDatabase.FindAssets($"t:{typeof(T).Name}")
3256
.FirstOrDefault();
3357

@@ -38,19 +62,14 @@ public static T LoadOrCreateInstance<T>() where T : ScriptableObject
3862
}
3963

4064
if (newInstance != null)
41-
return newInstance ;
42-
43-
newInstance = CreateInstance<T>();
44-
45-
AssetDatabaseUtils.CreatePathIfDontExist("Assets/Resources");
46-
UnityEditor.AssetDatabase.CreateAsset(newInstance, $"Assets/Resources/{typeof(T).Name}.asset");
47-
UnityEditor.AssetDatabase.SaveAssets();
48-
UnityEditor.AssetDatabase.Refresh();
49-
return newInstance;
65+
{
66+
result = newInstance;
67+
return true;
68+
}
5069
#endif
51-
#pragma warning disable CS0162
52-
return null;
53-
#pragma warning restore CS0162
70+
result = null;
71+
return false;
5472
}
73+
5574
}
5675
}

Scripts/Runtime/ScriptableObjectCollectionSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,6 @@ private CollectionToSettings GetOrCreateSettingsForCollection(ScriptableObjectCo
167167
return settings;
168168
}
169169

170+
170171
}
171172
}

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.4.0",
4+
"version": "1.4.1",
55
"unity": "2018.4",
66
"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": [

0 commit comments

Comments
 (0)