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.
- Before patch disassembly
- After patch disassembly
- Add
[Service(typeof(T))]
attribute for class that should be injected; - 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();
}
}
}
- And don't forget to add ServiceLocator component to the scene
- 🔻 This repository is for educational purposes only and is not recommended for commercial use, because contains a lot of uncovered injection cases.