Attribute Injector is an attribute-based framework for creating and injecting inline codes into your projects.
A code injection built for use in Unity3D. Implemented using Mono.Cecil.
Attribute Injector is a library that applies code injection to methods with additional attributes without the need to extend and create a new methods for each functionality. You can ensure project integrity with very little attributes.
[Injection]
public class SimpleLog : Attribute
{
[Rise(When.OnEntry)]
public void SomeMethod([Argument(Method.Name)] string name)
{
Debug.Log($"Entering method {name}");
}
}
[SimpleLog]
public void DoSomething()
{
Debug.Log("body of DoSomething method");
}
DoSomething(); //call the method anywhere...
Entering method 'DoSomething'
body of DoSomething method
[Injection]
public class ComplexLog : Attribute
{
[Rise(When.OnEntry)]
public void Begin([Argument(Method.Name)] string name)
{
Debug.Log($"Entering method {name}");
}
[Rise(When.OnExit)]
public void End([Argument(Method.Name)] string name, [Argument(Method.ReturnValue)] int value)
{
//you can reach return value of method.
Debug.Log($"Exiting method {name} with return value: {value.ToString()}");
}
}
[ComplexLog]
private int DoSomething() //notice return type
{
var x = Random.Range(1, 1000);
Debug.Log("DoSomething body");
return x;
}
Entering method 'DoSomething'
DoSomething body
Exiting method 'DoSomething' with return value: 126
Before install the addon, you should check the requirements.
- Supports only projects that can reference netstandard3.5+ libraries. Make sure PlayerSettings -> Api Compatibility Level is set to .NET 4.X
- In order for the addon to work, you need to add Mono Cecil package.
- Works with Unity 2017+
For Mono Cecil installation:
-
In Unity, open Window -> Package Manager.
-
Press the + button, choose "Add package from git URL..."
-
Enter "com.unity.nuget.mono-cecil@1.10" and press Add.
Project supports Unity Package Manager. To install the project as a Git package do the following:
- In Unity, open Window -> Package Manager.
- Press the + button, choose "Add package from git URL..."
- Enter "https://github.com/muveso/AttributeInjector.git#upm" and press Add.
Alternatively, you can add the code directly to the project:
- Clone the repo or download the latest release.
- Add the AttributeInjector folder to your Unity project or import the .unitypackage
On the Project Window, right-click and create Attribute Injector -> Settings.
This scriptable object holds all assemblies as cache for performance optimization. If you remove it, all injection process will be canceled.
Contact : sefa@muveso.com