ARFoundation: ARCameraBackground CommandBuffer freezes Editor

The CommandBuffer in ARCameraBackground freezes the Editor after a reset (leaving and re-entering playmode).

The exact method is

internal static void AddBeforeBackgroundRenderHandler()
{
    commandBuffer.IssuePluginEvent(s_BeforeBackgroundRenderHandlerFuncPtr, 0);
}

If this method runs, and you leave and try to re-enter play mode, the editor remains stuck at ‘Application.Reload’ indefinitely.

1 Like

I stumbled on this issue too in the development of my plugin. I ended up writing a script that will automatically apply this fix:

// calling commandBuffer.IssuePluginEvent is crashing Unity Editor 2019.2 and freezing newer versions of Unity
#if !UNITY_EDITOR
    commandBuffer.IssuePluginEvent(s_BeforeBackgroundRenderHandlerFuncPtr, 0);
#endif
1 Like

Here is a script to apply the fix automatically. This is not the perfect piece of code, but you can use it as an example :slight_smile:
Please place it inside the Editor folder.

using System;
using System.IO;
using UnityEditor;


[InitializeOnLoad]
public static class ARCameraBackgroundFixer {
    static ARCameraBackgroundFixer() {
        if (ApplyFixIfNeeded()) {
            AssetDatabase.Refresh();
        }
    }
 
    static bool ApplyFixIfNeeded() {
        var path = "Packages/com.unity.xr.arfoundation/Runtime/AR/ARCameraBackground.cs";
        var script = AssetDatabase.LoadAssetAtPath<MonoScript>(path);
        var text = script.text;
        if (text.Contains("AR_FOUNDATION_EDITOR_REMOTE")) {
            return false;
        }

        var index = text.IndexOf("commandBuffer.IssuePluginEvent(", StringComparison.Ordinal);
        var withFix = text.Insert(index, @"// AR_FOUNDATION_EDITOR_REMOTE: calling commandBuffer.IssuePluginEvent is crashing Unity Editor 2019.2 and freezing newer versions of Unity
            " + @"#if !UNITY_EDITOR
            ");
        withFix = withFix.Insert(withFix.IndexOf(";", index, StringComparison.Ordinal) + 1, @"
            " + "#endif");
        File.WriteAllText(AssetDatabase.GetAssetPath(script), withFix);
        return true;
    }
}

Thanks @KyryloKuzyk
Your plugin is actually what led me to finding the bug.
My own code would just freeze the editor whenever I did return a texture, and not freeze if I didn’t.

I’m currently creating a similar plugin to yours (but without the ‘Remote’-part), and would like to not have to implement the same ‘hacky’ fixes you did (especially when looking at future compatibility), which is why I made this thread.

1 Like

Ok, so after looking at ARCameraBackground.cs a bit more, three things stood out to me:

This creates a pointer to a function in the native subsystem for the render-thread to call just before rendering.
As neither mine nor @KyryloKuzyk 's XRCameraSubsystem exists in unmanaged code when Unity reloads before entering Play Mode the call to the static constructor of ARCameraBackground will attempt to re-marshall the pointer.
This somehow puts the Editor into an infinite loop or await.

Adding a #if !UNITY_EDITOR around the [MonoPInvokeCallback(typeof(Action))]-Attribute allows for our managed XRSubsystems to work just fine, keeping the commandbuffer while gaining a call to subsystem.OnBeforeBackgroundRender in the process.
Example:

#if !UNITY_EDITOR
[MonoPInvokeCallback(typeof(Action<int>))]
#endif
static void BeforeBackgroundRenderHandler(int eventId)
{
    if (s_CameraSubsystem != null)
        s_CameraSubsystem.OnBeforeBackgroundRender(eventId);
}

Of course this is still a ‘hacky’ fix. Any ‘natively’ supported editor-attached XRDevice (as in native to the Unity team) would not run from within the Editor.
A better fix would be for ARFoundation to provide built-in defines for managed vs unmanaged subsystems.
Is there any way to file bugreports specifically to ARFoundation devs?

1 Like

I think this repo is the best place to file bug reports:
https://github.com/Unity-Technologies/arfoundation-samples

1 Like

Hi i just downloaded the new version of ARF remote and I get

AR Foundation Remote. ARCameraBackground calls CommandBuffer.IssuePluginEvent which crashes/freezes some versions of Unity.

Im using Unity 2022.2.1f
and AR foundation 5.0
any idea’s how to fix this?

Neverminded embeding the project fixed issue.
however when i run it it’s just black image on the phone and it’s not tracking

@baha95 are you using URP or the built-in render pipeline? There is a bug in URP that causes a black screen on device that will be fixed in Unity 2022.2.2f1. As a temporary workaround you can set your ARCameraManager’s Render Mode to After Opaques.

1 Like