Skip to content

A set of utility classes to make it easier to access hidden functionality

License

Notifications You must be signed in to change notification settings

Thundernerd/Unity3D-Reflectives

Repository files navigation

Reflectives

GitHub package.json version GitHub issues GitHub pull requests GitHub license GitHub last commit

A set of utility classes to make it easier to access hidden functionality.

Installation

  1. The package is available on the openupm registry. You can install it via openupm-cli.
openupm add net.tnrd.reflectives
  1. Installing through a Unity Package created by the Package Installer Creator from Needle

Usage

Let's assume we have the class below and we want to access some of the hidden functionality

public class Foo
{
    public int SomeField;

    private int HiddenProperty => SomeField * 10;

    public void NormalPublicMethod()
    {
        [...]
    }
    
    private void SomeHiddenMethod()
    {
        [...]
    }
}

There best way to get access to the hidden functionality with the use of Reflectives is like so

public class ReflectiveFoo : ReflectiveClass
{
    private readonly ReflectiveProperty<int> hiddenProperty;
    private readonly ReflectiveMethod someHiddenMethod;

    public ReflectiveFoo(object instance) : base(instance)
    {
        hiddenProperty = CreateProperty<int>("HiddenProperty");
        someHiddenMethod = CreateMethod("SomeHiddenMethod");
    }

    public ReflectiveFoo(Type type) : base(type)
    {
    }

    public int HiddenProperty
    {
        get => hiddenProperty.GetValue();
        set => hiddenProperty.SetValue(value);
    }

    public void SomeHiddenMethod()
    {
        someHiddenMethod.Invoke();
    }
}

Now whenever you want to access the functionality you can easily do so like this

public void SomeFunction()
{
    Foo foo = GetFoo();

    ReflectiveFoo reflectiveFoo = new ReflectiveFoo(foo);

    int value = reflectiveFoo.HiddenProperty;
    reflectiveFoo.HiddenProperty = 123;

    reflectiveFoo.SomeHiddenMethod();
}

The downside of this is that it does require you to write some boilerplate code, however, if you have Odin Inspector you can the Reflective Generator to generate reflective classes for you

Reflective Generator Window

By clicking the + button on the right side of the window you'll be able to search through all types in the project through the search window

Search Window

Check the types that you want and click the green checkmark to confirm your selection. Afterwards you simply press the export button and your classes will be generated.

Keep in mind that reflection can be tricky, and the generated code might contain some compile errors from time to time. It is expected that you'll be able to solve those yourself with the given error messages that you'll receive.

Support

Reflectives is a small and open-source utility that I hope helps other people. It is by no means necessary but if you feel generous you can support me by donating.

ko-fi

Contributions

Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.

About

A set of utility classes to make it easier to access hidden functionality

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages