Giter Club home page Giter Club logo

attribute-injector's Introduction

Attribute Injector For Unity

Unity 2017+ License: MIT

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.

Simple Usage

Create an attribute with injection :

[Injection]
public class SimpleLog : Attribute
{
    [Rise(When.OnEntry)]
    public void SomeMethod([Argument(Method.Name)] string name)
    {
        Debug.Log($"Entering method {name}");
    }
}

Use it :

[SimpleLog]
public void DoSomething() 
{ 
    Debug.Log("body of DoSomething method");
}

DoSomething(); //call the method anywhere...

Result :

Entering method 'DoSomething'
body of DoSomething method

More Complex Scenerios

Complex Attribute :

[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()}");
    }
}

Usage :

[ComplexLog]
private int DoSomething() //notice return type
{
    var x = Random.Range(1, 1000);
    Debug.Log("DoSomething body");
    return x;
}

Result :

Entering method 'DoSomething'
DoSomething body
Exiting method 'DoSomething' with return value: 126

Requirements

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:

  1. In Unity, open Window -> Package Manager.

  2. Press the + button, choose "Add package from git URL..."

  3. Enter "[email protected]" and press Add.

Download

Git URL

Project supports Unity Package Manager. To install the project as a Git package do the following:

  1. In Unity, open Window -> Package Manager.
  2. Press the + button, choose "Add package from git URL..."
  3. Enter "https://github.com/muveso/AttributeInjector.git#upm" and press Add.

Unity Package

Alternatively, you can add the code directly to the project:

  1. Clone the repo or download the latest release.
  2. Add the AttributeInjector folder to your Unity project or import the .unitypackage

Installation

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 : [email protected]

attribute-injector's People

Contributors

muveso avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.