diff --git a/Assets/Pacore/Runtime/ClassAttributeCache.cs b/Assets/Pacore/Runtime/ClassAttributeCache.cs index f6fc0df..a444ddc 100644 --- a/Assets/Pacore/Runtime/ClassAttributeCache.cs +++ b/Assets/Pacore/Runtime/ClassAttributeCache.cs @@ -13,8 +13,8 @@ namespace PashaBibko.Pacore.Runtime public static ReadOnlyCollection GetAttributesOf() { - return AttributeCache.TryGetValue(typeof(T), out List attributes) ? - attributes.AsReadOnly() : + return AttributeCache.TryGetValue(typeof(T), out List classes) ? + classes.AsReadOnly() : throw new ArgumentException($"Attribute [{nameof(T)}] is not used by any classes"); } diff --git a/Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs b/Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs new file mode 100644 index 0000000..64ecf10 --- /dev/null +++ b/Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs @@ -0,0 +1,33 @@ +using PashaBibko.Pacore.Shared.Attributes; +using System.Collections.ObjectModel; +using UnityEngine; +using System; + +using Object = UnityEngine.Object; + +namespace PashaBibko.Pacore.Runtime +{ + public static class CreateInstanceOnStartLoader + { + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + private static void CreateAllInstances() + { + /* Fetches all the types with the CreateInstanceOnStart attribute */ + ReadOnlyCollection types = + ClassAttributeCache.GetAttributesOf(); + + /* Creates a holder for all the other game objects to not clutter inspector */ + GameObject holder = new("ScriptSpawnedInstances"); + Object.DontDestroyOnLoad(holder); // Stops all the game objects from being destroyed + + Transform parent = holder.transform; + + /* Creates all the game objects for the selected types */ + foreach (Type type in types) + { + GameObject go = new(type.Name, type); + go.transform.SetParent(parent); + } + } + } +} diff --git a/Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs.meta b/Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs.meta new file mode 100644 index 0000000..222bd83 --- /dev/null +++ b/Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cc80f2f5d2d545e2a5776a2c7d86a813 +timeCreated: 1769274499 \ No newline at end of file diff --git a/Assets/Pacore/Shared/Attributes/CreateInstanceOnStart.cs b/Assets/Pacore/Shared/Attributes/CreateInstanceOnStart.cs new file mode 100644 index 0000000..4f176c7 --- /dev/null +++ b/Assets/Pacore/Shared/Attributes/CreateInstanceOnStart.cs @@ -0,0 +1,8 @@ +using UnityEngine.Scripting; +using System; + +namespace PashaBibko.Pacore.Shared.Attributes +{ + [Preserve, AttributeUsage(AttributeTargets.Class)] + public class CreateInstanceOnStartAttribute : Attribute { } +} diff --git a/Assets/Pacore/Shared/Attributes/CreateInstanceOnStart.cs.meta b/Assets/Pacore/Shared/Attributes/CreateInstanceOnStart.cs.meta new file mode 100644 index 0000000..77206c8 --- /dev/null +++ b/Assets/Pacore/Shared/Attributes/CreateInstanceOnStart.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 76e2a7cd34f24a17be15685d89706cd3 +timeCreated: 1769274185 \ No newline at end of file diff --git a/Assets/TestMonoBehaviour.cs b/Assets/TestMonoBehaviour.cs index 896eeda..6a51798 100644 --- a/Assets/TestMonoBehaviour.cs +++ b/Assets/TestMonoBehaviour.cs @@ -1,7 +1,7 @@ using PashaBibko.Pacore.Shared.Attributes; using UnityEngine; -public class TestMonoBehaviour : MonoBehaviour +[CreateInstanceOnStart] public class TestMonoBehaviour : MonoBehaviour { [InspectorReadOnly, SerializeField] private GameObject go;