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
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.
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:
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?
@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.