NivekNK / NKRuntimeSetSystem

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NK RuntimeSet System

This RuntimeSet system allows you to define classes of type "Singleton", which you can find and use in other classes. It comes with an example of the Manager class type.

To use the NK RuntimeSet System, you need to clone the repository to the "Assets/Plugins/NKTools" folder path and create the "Assets/Resources/RuntimeSets" folder, there you need to create the RuntimeSet.

NKRuntimeSetSystem

NKManager Class

using UnityEngine;

namespace NK.RuntimeSetSystem
{
    public abstract class NKManager : MonoBehaviour
    {
        public NKManagerRuntimeSet RuntimeSet;

        protected virtual void OnEnable()
        {
            if (RuntimeSet == null)
                RuntimeSet = NKRuntimeSet.GetRuntimeSet<NKManagerRuntimeSet>();
            RuntimeSet?.Add(this);
        }

        protected virtual void OnDisable()
        {
            RuntimeSet?.Remove(this);
        }
    }
}

What you can do now is inherit the class NKManager and use search for the created class in the "RuntimeSet".

using NK.RuntimeSetSystem;

public class ClassA : NKManager
{
    public string Name = "ClassA";
}
using NK.RuntimeSetSystem;

public class ClassB : MonoBehaviour
{
    private void Start()
    {
        var runtimeSet = NKRuntimeSet.GetRuntimeSet<NKManagerRuntimeSet>();

        if (runtimeSet.GetTypeOfRuntimeSet(out ClassA classA))
            Debug.Log(classA.Name);
    }
}

About

License:MIT License


Languages

Language:C# 100.0%