Fix "Handle is not initialized" for Unity 2021.2.x games #232
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a fix proposal for #210 - I was trying to work with Neptunia Sisters Vs Sisters which is affected by the same issue the user reported there.
When the mono object is a System.Action object it seems
GetGcHandlePtrFromIl2CppObject(pointer)returns a null pointer for the Garbage Collector Handle (or well IntPtr.Zero). This causesGCHandle.FromIntPtr(gcHandle)to throw an excepction.One solution after a lot of back and forth was to essentially check if the object class is a Delegate type class (which includes System.Action) and if so, get the garbage collector handle on the mTarget of the delegate object. This way in my testing the code will not interfere with other Unity versions - yet it allows to hook onto my test game - which happens to be a 2021.2.x game compiled with IL2CPP.
In other Unity versions
if (gcHandle == IntPtr.Zero)should be false so I doubt this would interfere in any way (it worked fine in other tested games for me).