类似与之前的文章JNI中用long传递指针到java,我们在Android平台的Unity中同样存在这样的问题。java中我们可以直接返回一个long类型,但在C#中我们找到了一个叫做IntPtr的东西。
什么是IntPtr : 先来看看MSDN上说的:用于表示指针或句柄的平台特定类型。这个其实说出了这样两个事实,IntPtr 可以用来表示指针或句柄、它是一个平台特定类型。对于它的解释,这个哥们写的比较好:It's a class that wraps a pointer that is used when calling Windows API functions. The underlying pointer may be 32 bit or 64 bit, depending on the platform.
那其实是java中使用long是一样的使用方法跟原理了。 在C#中我们这样传递过去:
#if UNITY_ANDROID
public static Vector3[][] get47MeshVertices(){
...
IntPtr p = System.Runtime.InteropServices.Marshal.AllocHGlobal(8);
Vector3[] vector3s = new Vector3[columns];
getVertices(vector3s , p);
...
}
[DllImport("yoyoav_ffmpeg")]
public static extern void getVertices([In, Out] Vector3[] vector3s, IntPtr ptr);
#endif
C++中我们可以直接用long来接,其实用float或者double*接是一样的,我们只是把它当做一个指针来对待就好了。
//我们只关心指针变量的传递
JNIEXPORT void
getVertices(Vector3 *vector3, long *p1) {
...
std::vector<std::vector<float>> *vectors = new std::vector<std::vector<float>>();
*p1 = (long) vectors;
...
}
C++这里使用的时候直接指针类型强转,然后做赋值或者push_back都可以
std::vector<std::vector<float>> *vs = (std::vector<std::vector<float>> *) *p1;
大功告成。