Added CreateInstanceOnStart attribute

This commit is contained in:
2026-01-24 18:41:41 +00:00
parent 17e86b26a6
commit b6cfd020ba
6 changed files with 50 additions and 3 deletions

View File

@@ -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");
}

View 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);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cc80f2f5d2d545e2a5776a2c7d86a813
timeCreated: 1769274499

View File

@@ -0,0 +1,8 @@
using UnityEngine.Scripting;
using System;
namespace PashaBibko.Pacore.Shared.Attributes
{
[Preserve, AttributeUsage(AttributeTargets.Class)]
public class CreateInstanceOnStartAttribute : Attribute { }
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 76e2a7cd34f24a17be15685d89706cd3
timeCreated: 1769274185

View File

@@ -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;