App shows black screen at first, works after minimizing and reopening

Hi everyone,

I have a question related to my AR app.
I’m making a simple AR app that detects a mark and places a video on top.
I’m using ARCore to build so I can install apk on my Galaxy S23 phone.
I have this script under the XR Origin object that allows me to place multiple videos with my app.
But when I turn on this app, it shows a black screen at first, but it works perfectly after minimizing and reopening. This still happens after I give all the access to my camera and fully turn it off, and turn it on again. Does anyone know how to make this work without going back to main screen?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
  
public class ImageTracker : MonoBehaviour
{
    private ARTrackedImageManager trackedImages;
    public GameObject[] ArPrefabs; //Quarry the game object and claim it as 'ArPrefabs'


    List<GameObject> ARObjects = new List<GameObject>();


    void Awake() //called when the script object is initialised, regardless of whether or not the script is enabled
    {
        trackedImages = GetComponent<ARTrackedImageManager>();
    }

    void OnEnable() 
    {
        trackedImages.trackedImagesChanged += OnTrackedImagesChanged;
    }

    void OnDisable()
    {
        trackedImages.trackedImagesChanged -= OnTrackedImagesChanged;
    }


    
    private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs) //Main event function.
    {
        //Create object based on image tracked
        foreach (var trackedImage in eventArgs.added)
        {
            foreach (var arPrefab in ArPrefabs)
            {
                if (trackedImage.referenceImage.name == arPrefab.name)
                {
                    var newPrefab = Instantiate(arPrefab, trackedImage.transform);
                    ARObjects.Add(newPrefab);
                }
            }
        }

        //Update tracking position
        foreach (var trackedImage in eventArgs.updated)
        {
            foreach (var gameObject in ARObjects)
            {
                if (gameObject.name == trackedImage.name)
                {
                    gameObject.SetActive(trackedImage.trackingState == TrackingState.Tracking);
                }
            }
        }

    }
}

To solve the Black Screen issue in all my AR Assets, I use Native Camera asset.
It operates camera permission request. At the same time, the camera feed is operated by AR Foundation.

The methods that Unity provides to operate with permission are not flexible.

The trick is that to block user experience until permission was granted, that allowed by Native Camera for iOS/Android:

  1. Ask the permission.
  2. Open the scene with main gameplay or block user experience with the next screen (yes, I also use this trick in apps without AR Foundation but with custom Camera Feed (WebCamTexture)):

Do you have the Magic Leap XR Plug-in in your project? This sounds like an old issue: Issue with Android: Skipped rendering frame because GfxDevice is in invalid state (device lost) - #35 by andyb-unity

Unfortunately, I don’t have the Magic Leap XR Plug-in in my project. I think it’s a different issue :frowning:

Well I can only give you generic advise here. This issue is definitely not in the script you attached.

  1. Make sure your project configuration is valid: Project configuration | Google ARCore XR Plugin | 6.0.5
  2. You don’t say what version of Unity you are on, so if it’s not the most recent patch of your Editor version, I would try most recent patch versions to get any recent bugfixes.
  3. You could try bringing your code into our sample app instead, which is a known working project with no reports of this issue: GitHub - Unity-Technologies/arfoundation-samples: Example content for Unity projects based on AR Foundation
  4. Please file a bug if you are unable to resolve this case: Unity QA: Building quality with passion