- Home /
IL2CPP does not support marshaling delegates that point to instance methods to native code.
I'm encountering an issue when I try to call RegisterDidChangeTagsSetCallback from my game. I get the following error in xCode: "IL2CPP does not support marshaling delegates that point to instance methods to native code." This game is also only for iOS, so I haven't tested it on any other platform.
I found a similar issue on Unity Answers, but the solution seemed to be to make the callback method static, which mine already was. They also seemed to have the [MonoPInvokeCallback(typeof(TagsChangedDelegate))] line above the method, which I added, but to no avail.
I'm not experienced with anything in this realm, so any help would be greatly appreciated!
C++ Code:
extern "C" {
typedef void (*TagsChangedDelegateCallback)(const char *oldTagsDate, char *oldTags[], const char *newTagsDate, char *newTags[]);
void registerDidChangeTagsSetCallback(TagsChangedDelegateCallback callback);
}
C# Code:
[DllImport("__Internal")]
private static extern void registerDidChangeTagsSetCallback(TagsChangedDelegate callback);
public delegate void TagsChangedDelegate(string oldTagsDate, string[] oldTags, string newTagsDate, string[] newTags);
[MonoPInvokeCallback(typeof(TagsChangedDelegate))]
public static void RegisterDidChangeTagsSetCallback(TagsChangedDelegate callback)
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
registerDidChangeTagsSetCallback(callback);
}
}
Where I'm calling the code:
public void ListenForTags()
{
if (!isRegistered)
{
isRegistered = true;
LXProxSeeSDKBridge.TagsChangedDelegate tagsChangedDelegate = Something;
LXProxSeeSDKBridge.RegisterDidChangeTagsSetCallback(tagsChangedDelegate);
}
}
void Something(string oldTagsDate, string[] oldTags, string newTagsDate, string[] newTags)
{
if (newTags.Length > 0)
proxSeeTMP.text = proxSeeTMP.text + "\n" + newTags[0];
}
Answer by JoshPeterson · Jan 28, 2019 at 01:59 PM
I think the other issue you are missing here is that the method Something
in the third code snippet needs to be a static method. Also, Something
needs to have the [MonoPInvokeCallback(typeof(TagsChangedDelegate))]
attribute. I don't believe that RegisterDidChangeTagsSetCallback
needs to have that attribute.
Unfortunately the error message that IL2CPP reports is not great here, since it does not mention the name of the method which needs to be static and have the attribute. We've improved the error message to include that information in newer versions of Unity - so that will help in the future.
Also note that marshaling of string arrays from native to managed code is not trivial - there be dragons. I'd recommend investigating the arguments passed to Something
to make sure they are correct in all cases.
Your answer
Follow this Question
Related Questions
Can't call GameCenter in my Unity game for iOS 0 Answers
Button as a child of a button 0 Answers
My Script Wont work? 2 Answers
If statement being ignored (Apparently) 2 Answers