Added Pacore
This commit is contained in:
33
Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs
Normal file
33
Assets/Pacore/Runtime/CreateInstanceOnStartLoader.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using PashaBibko.Pacore.Attributes;
|
||||
using System.Collections.ObjectModel;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace PashaBibko.Pacore
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user