From a579079e9b7c187cdc2fdbfb6c69cade20354731 Mon Sep 17 00:00:00 2001 From: Bruno Mikoski Date: Wed, 5 Aug 2020 13:12:22 +0100 Subject: [PATCH] fix: AOT compilation warnings --- CHANGELOG.MD | 7 +++- Scripts/Editor/Utils/CollectionUtility.cs | 2 +- Scripts/Runtime/CollectionsRegistry.cs | 7 ++++ .../ResourceScriptableObjectSingleton.cs | 36 ++++++++----------- package.json | 8 +++-- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 5d0ea1d..8f0c99f 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -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 @@ -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 diff --git a/Scripts/Editor/Utils/CollectionUtility.cs b/Scripts/Editor/Utils/CollectionUtility.cs index ab43089..56c1545 100644 --- a/Scripts/Editor/Utils/CollectionUtility.cs +++ b/Scripts/Editor/Utils/CollectionUtility.cs @@ -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(); } public static Editor GetEditorForItem(CollectableScriptableObject collectionItem) diff --git a/Scripts/Runtime/CollectionsRegistry.cs b/Scripts/Runtime/CollectionsRegistry.cs index 1ee83df..2602c03 100644 --- a/Scripts/Runtime/CollectionsRegistry.cs +++ b/Scripts/Runtime/CollectionsRegistry.cs @@ -13,6 +13,13 @@ public class CollectionsRegistry : ResourceScriptableObjectSingleton collectionGUIDs = new List(); + public void UsedOnlyForAOTCodeGeneration() + { + LoadOrCreateInstance(); + // 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(); diff --git a/Scripts/Runtime/ResourceScriptableObjectSingleton.cs b/Scripts/Runtime/ResourceScriptableObjectSingleton.cs index f039d5d..ff92201 100644 --- a/Scripts/Runtime/ResourceScriptableObjectSingleton.cs +++ b/Scripts/Runtime/ResourceScriptableObjectSingleton.cs @@ -10,51 +10,45 @@ public static T Instance { get { - LoadOrCreateInstance(); + if (instance == null) + instance = LoadOrCreateInstance(); return instance; } } [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] - public static void LoadOrCreateInstance() + public static T LoadOrCreateInstance() where T: ScriptableObject { - if (instance != null) - return; - - instance = FindObjectOfType(); - - if (instance != null) - return; - - instance = Resources.Load(typeof(T).Name); + T newInstance = Resources.Load(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( + newInstance = (T) UnityEditor.AssetDatabase.LoadAssetAtPath( UnityEditor.AssetDatabase.GUIDToAssetPath(registryGUID)); } + + if (newInstance != null) + return newInstance; - if(instance != null) - return; - - - instance = CreateInstance(); + newInstance = CreateInstance(); 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; } } } diff --git a/package.json b/package.json index f842b97..5b561c1 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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" + } }