Skip to content

Yggdrasills/InjectableServiceLocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Service Locator for Unity

Utility that adds code generation to the standard service locator. This repository offers to get acquainted with the basics of working with code generation (Mono.Cecil). Allows you to automatically add services to the service locator using only an attribute to shorten the code.

Getting started

  • Before patch disassembly

Before patch disassembly

  • After patch disassembly

After patch disassembly

Usage

  1. Add [Service(typeof(T))] attribute for class that should be injected;
  2. Add [Inject] attribute for field in the other classes where service should be injected.
[Service(typeof(DemoService))]
public class DemoService : MonoBehaviour
{
  public void Display()
  {
      Debug.Log("Demo service");
  }
}
public class DemoClass : MonoBehaviour
{
    [Inject]
    private DemoService _service;

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            _service.Display();
        }
    }
}
  1. And don't forget to add ServiceLocator component to the scene

Notes

  1. 🔻 This repository is for educational purposes only and is not recommended for commercial use, because contains a lot of uncovered injection cases.

About

Small repository for showing work with Mono.Cecil

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages