Unity IL2CPP-compliant reflection acceleration library
- Unity 2022.2 or newer (2023.3 is not tested) This library highly depends on the il2cpp runtime. Be cautious about version differences or this will cause crash.
- Open the Package Manager by going to Window > Package Manager.
- Click on the "+" button and select "Add package from git URL".
- Enter the following URL:
https://github.com/Akeit0/UniReflection.git?path=/Assets/UniReflection
Open Packages/manifest.json
and add the following in the dependencies
block:
"com.akeit0.uni-reflection": "https://github.com/Akeit0/UniReflection.git?path=/Assets/UniReflection"
No boxing fast new
class MyClass{
private int a=0;
public MyClass(){
}
public MyClass(int a){
this.a=a;
}
}
//Equivalent to Activator.CreateInstance, but faster
var myClass=NewCache<MyClass>.CreateInstance();
myClass=NewCache<MyClass,int>.CreateInstance(1);
class MyStruct{
private int a;
public MyStruct(int a){
this.a=a;
}
}
var myStruct=NewCache<MyStruct>.CreateInstance();
myStruct=NewCache<MyStruct,int>.CreateInstance(1);
static int a;
Action<int> setter =fieldInfo.CreateStaticFieldSetter<int>();
Func<int> getter =fieldInfo.CreateStaticFieldGetter<int>();
class MyClass{
private int a=0;
}
var offset=fieldInfo.GetOffset();
var myclass=new MyClass();
ref var a=GetFieldReferenceFromObject<int>(myclass,offset);
InstanceAction or InstanceFunc can be called in burst compiled function.
[BurstCompile]
public class Foo
{
[BurstCompile]
public static void Bar(in InstanceAction<int> a, int i)
{
a.Invoke(i);
}
}
using (var instanceAction=new InstanceAction<int>(i => Debug.Log(i))
{
Foo.Bar(instanceAction,1);
}
Static function delegates and those containing multiple delegates are not supported.
static void Log(int i)=> Debug.Log(i);
Action<int> action=Log;
// Throw Exception
using (var instanceAction=new InstanceAction<int>(action)
{
Foo.Bar(instanceAction,1);
}
Action<int> action=i=>_=i;
action +=i=>_=i;
// Throw Exception
using (var instanceAction=new InstanceAction<int>(action)
{
Foo.Bar(instanceAction,1);
}
namespace UniReflection.IL2CPP will allow you to access il2cpp runtime internal.
Il2CppApi class provides many low level apis such as il2cpp_resolve_icall
which can get internal extern methods pointer (unmanaged[Cdecl]).