How to get ARKit Session pointer

Hey guys,

I want to get the native ARKit session pointer through the ARfoundation package, and pass it back to a native iOS plugin. Is that possible? Basically I want to get the camera feed data, as well as the pose, and the point cloud elements, and do some processing through native swift code. Is it possible to pass the ARSession handle?

Please see Extending AR Foundation | AR Foundation | 4.0.12

Thanks, so the doc. Just checking, does this look like I’m on the right track to get at that data in currentFrame?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using System;
using System.Runtime.InteropServices;


//using UnityEngine.XR.

public class TestScript : MonoBehaviour
{
    #if UNITY_IOS && !UNITY_EDITOR
    [DllImport("__Internal")]
    public static extern void HelloWorld(int x);
    #else
    public static void HelloWorld(int x) {  Debug.Log(x); }
    #endif
    #if UNITY_IOS && !UNITY_EDITOR
    [DllImport("__Internal")]
    public static extern void DefinePointer(IntPtr x);
    #else
    public static void DefinePointer(IntPtr x) {  Debug.Log(x); }
    #endif
    // private static extern int DefinePointer(IntPtr pointer);
     ARSession arSession;
    // // Start is called before the first frame update
    void Start()
    {
        Invoke("DoWork", 2.0f);
    }

    void DoWork() {
        HelloWorld(5);
        arSession = GetComponent<ARSession>();
        System.IntPtr x = (arSession.subsystem.nativePtr);
        DefinePointer(x);
    }
}

//Test.mm
#import <Foundation/Foundation.h>
#import <ARKit/ARKit.h>
extern “C” {
void HelloWorld(int x)

{
NSLog(@“Hello, World!”);
}

void DefinePointer(void* x)
{
NSLog(@“%p”, x);
ARSession *arSession = (__bridge ARSession *)x;
NSLog(@“%p”, arSession);
NSLog(@“%@”, arSession.currentFrame.description);
}
}

The pointer in arSession.subsystem.nativePtr points to a struct that contains the ARSession pointer; it does not point directly to the pointer. So your Test.mm should look more like this:

struct UnityXRNativeSessionPtr
{
   int version;
   void* session;
};

void DefinePointer(void* x)
{
    auto data = static_cast<UnityXRNativeSessionPtr*>(x);
    ARSession* session = (__bridge ARSession*)data->session;
    ...
}

Ahh, gotcha. Thanks so much for your help, it seems to work now!