Version: 2023.1
LanguageEnglish
  • C#

JNINativeMethod.fnPtr

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public IntPtr fnPtr;

Description

The function pointer.

This should be obtained using Marshal.GetFunctionPointerForDelegate, and delegate should refer to a method annotated with MonoPInvokeCallback attribute and having such signature: static ReturnType MyMethod(IntPtr jniEnv, IntPtr objectOrClass, ...);

// Delegate difinition
delegate void JavaToCs(IntPtr jniEnv, IntPtr klass, int x);

// Method definition [MonoPInvokeCallback(typeof(JavaToCs))] static void CsMethod(IntPtr jniEnv, IntPtr klass, int x) { Debug.Log("From Java: " + x); }

// Array to be passed to AndroidJNI.RegisterNative var methods = new JNINativeMethod[] { new JNINativeMethod { name = "csMethod", signature = "(I)V", fnPtr = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(new JavaToCs(CsMethod)), } };

// Register method for Java class AndroidJNI.RegisterNatives(clazz, methods);