Added CreateInstanceOnStart attribute
This commit is contained in:
@@ -13,8 +13,8 @@ namespace PashaBibko.Pacore.Runtime
|
||||
|
||||
public static ReadOnlyCollection<Type> GetAttributesOf<T>()
|
||||
{
|
||||
return AttributeCache.TryGetValue(typeof(T), out List<Type> attributes) ?
|
||||
attributes.AsReadOnly() :
|
||||
return AttributeCache.TryGetValue(typeof(T), out List<Type> classes) ?
|
||||
classes.AsReadOnly() :
|
||||
throw new ArgumentException($"Attribute [{nameof(T)}] is not used by any classes");
|
||||
}
|
||||
|
||||
|
||||
33
Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs
Normal file
33
Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs
Normal file
@@ -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<Type> types =
|
||||
ClassAttributeCache.GetAttributesOf<CreateInstanceOnStartAttribute>();
|
||||
|
||||
/* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc80f2f5d2d545e2a5776a2c7d86a813
|
||||
timeCreated: 1769274499
|
||||
8
Assets/Pacore/Shared/Attributes/CreateInstanceOnStart.cs
Normal file
8
Assets/Pacore/Shared/Attributes/CreateInstanceOnStart.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using UnityEngine.Scripting;
|
||||
using System;
|
||||
|
||||
namespace PashaBibko.Pacore.Shared.Attributes
|
||||
{
|
||||
[Preserve, AttributeUsage(AttributeTargets.Class)]
|
||||
public class CreateInstanceOnStartAttribute : Attribute { }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76e2a7cd34f24a17be15685d89706cd3
|
||||
timeCreated: 1769274185
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user