Skip to content

Latest commit

 

History

History
52 lines (42 loc) · 1.44 KB

README.md

File metadata and controls

52 lines (42 loc) · 1.44 KB

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.