Skip to content

Commit

Permalink
Merge pull request #22 from badawe/feature/fix-aot-issue
Browse files Browse the repository at this point in the history
fix: AOT compilation warnings
  • Loading branch information
brunomikoski authored Aug 5, 2020
2 parents 6db3e9d + a579079 commit f3ddd86
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [1.1.9]
## Changed
- Fixed AOT compilation issue

## [1.1.8]
## Changed
- Fixed mobile builds (using editor references on runtime files)


## [1.1.7]
## Added
- Proper error when there's no static script folder defined
Expand Down Expand Up @@ -96,6 +100,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Add a setup wizzard for first time settings creation

[1.1.9]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.9
[1.1.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.8
[1.1.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.7
[1.1.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.6
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Editor/Utils/CollectionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static void CreateNewItem()
[MenuItem("Assets/Create/ScriptableObject Collection/Create Settings", false, 200)]
private static void CreateSettings()
{
ScriptableObjectCollectionSettings.LoadOrCreateInstance();
ScriptableObjectCollectionSettings.LoadOrCreateInstance<ScriptableObjectCollectionSettings>();
}

public static Editor GetEditorForItem(CollectableScriptableObject collectionItem)
Expand Down
7 changes: 7 additions & 0 deletions Scripts/Runtime/CollectionsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public class CollectionsRegistry : ResourceScriptableObjectSingleton<Collections
[SerializeField, HideInInspector]
private List<string> collectionGUIDs = new List<string>();

public void UsedOnlyForAOTCodeGeneration()
{
LoadOrCreateInstance<CollectionsRegistry>();
// Include an exception so we can be sure to know if this method is ever called.
throw new InvalidOperationException("This method is used for AOT code generation only. Do not call it at runtime.");
}

public bool IsKnowCollectionGUID(string guid)
{
ValidateCurrentGUIDs();
Expand Down
36 changes: 15 additions & 21 deletions Scripts/Runtime/ResourceScriptableObjectSingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,45 @@ public static T Instance
{
get
{
LoadOrCreateInstance();
if (instance == null)
instance = LoadOrCreateInstance<T>();
return instance;
}
}

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void LoadOrCreateInstance()
public static T LoadOrCreateInstance<T>() where T: ScriptableObject
{
if (instance != null)
return;

instance = FindObjectOfType<T>();

if (instance != null)
return;

instance = Resources.Load<T>(typeof(T).Name);
T newInstance = Resources.Load<T>(typeof(T).Name);

if (instance != null)
return;
if (newInstance != null)
return newInstance;

#if UNITY_EDITOR
if (Application.isPlaying)
return;
return null;

string registryGUID = UnityEditor.AssetDatabase.FindAssets($"t:{typeof(T).Name}")
.FirstOrDefault();

if (!string.IsNullOrEmpty(registryGUID))
{
instance = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(
newInstance = (T) UnityEditor.AssetDatabase.LoadAssetAtPath<ScriptableObject>(
UnityEditor.AssetDatabase.GUIDToAssetPath(registryGUID));
}

if (newInstance != null)
return newInstance;

if(instance != null)
return;


instance = CreateInstance<T>();
newInstance = CreateInstance<T>();

AssetDatabaseUtils.CreatePathIfDontExist("Assets/Resources");
UnityEditor.AssetDatabase.CreateAsset(instance, $"Assets/Resources/{typeof(T).Name}.asset");
UnityEditor.AssetDatabase.CreateAsset(newInstance, $"Assets/Resources/{typeof(T).Name}.asset");
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
return newInstance;
#endif
return null;
}
}
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.brunomikoski.scriptableobjectcollection",
"displayName": "Scriptable Object Collection",
"version": "1.1.8",
"version": "1.1.9",
"unity": "2018.4",
"description": "Scriptable Object Collection",
"keywords": [
Expand All @@ -15,5 +15,9 @@
"type": "git",
"url": "https://github.com/badawe/ScriptableObjectCollection.git"
},
"bugs": "https://github.com/badawe/ScriptableObjectCollection/issues"
"bugs": "https://github.com/badawe/ScriptableObjectCollection/issues",
"author": {
"name": "Bruno Mikoski",
"url": "https://www.brunomikoski.com"
}
}

0 comments on commit f3ddd86

Please sign in to comment.