In this page of the documentation, there’s a line that says “Function pointers don’t support generic delegates.” This is partially correct. The truth is that generic FunctionPointer
s run in a compiled Job DO work; however, if the FunctionPointer
is run outside of the job (via mono), they DO NOT work.
In Burst version 1.5.0, there’s a nifty feature where you can assume booleans to be true or false. Using that, you can make your code do different things if it’s compiled by Burst or not.
Here’s how you use the Assume
method to create a FunctionPointer
implementation that supports generics in all cases:
You have 2 cases – When the code being run is compiled by Burst, then you use the generic FunctionPointer
. When the code being run isn’t compiled by burst, then you use a stored pointer of the C# delegate. All you have to do is keep a map from the function pointer to the C# delegate. You can do this when you compile the FunctionPointer
.
There ya go, now it’s much easier to define FunctionPointer
s in your generic methods and structs.