arcore-unity-sdk
arcore-unity-sdk copied to clipboard
Random deactivation of gameobject during loss of image tracking
What is going on: Eventually... when tracking is lost... sometimes(!) for just one frame, your anchored game-object might go inactive.
This was tricky to pinpoin but it gave me a 6 hour madness as I had a OnDisable() method executing without anything calling it until I've build a test script and confirmed it.
Here is how to reproduce:
- Have a cube deactivated in the hierarchy
- Use my sloppy script that will test the scene just fine, drag that deativated cube in
debugGameobject
- Run on device having the device report to the console (development / script debugging)
- It doesn't always fail but I found that by doing this trick 2 or 3 times it does fail
- I printed a photo so it could be scanned and I leave that paper on a desk
- I scan it, the cube get's in the middle of the photo
- I landed the phone on the printer untill it goes 100% black
- cube gets lost
- I raise phone a bit but I do this in a hurry so that cube does not reappear
- I land the phone again
- I raise it even higher now so that ARCore does figure out it's looking in the photo
- Right before the cube re-appears it deactivates for one frame (at the time)
I found ways to overcome this and optimize the hell out of my AR app but this shouldn't have happened in the first place.
I forgot to paste this
//-----------------------------------------------------------------------
// <copyright file="AugmentedImageExampleController.cs" company="Google">
//
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
//-----------------------------------------------------------------------
namespace GoogleARCore.Examples.AugmentedImage
{
using System.Collections.Generic;
using System.Runtime.InteropServices;
using GoogleARCore;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Controller for AugmentedImage example.
/// </summary>
/// <remarks>
/// In this sample, we assume all images are static or moving slowly with
/// a large occupation of the screen. If the target is actively moving,
/// we recommend to check <see cref="AugmentedImage.TrackingMethod"/> and
/// render only when the tracking method equals to
/// <see cref="AugmentedImageTrackingMethod"/>.<c>FullTracking</c>.
/// See details in <a href="https://developers.google.com/ar/develop/c/augmented-images/">
/// Recognize and Augment Images</a>
/// </remarks>
public class AugmentedImageExampleControllerDEBUG : MonoBehaviour
{
//DEBUGGER
private bool m_KeepRunning = true;
public GameObject debugGameobject;
private List<AugmentedImage> m_TempAugmentedImages = new List<AugmentedImage>();
/// <summary>
/// The Unity Awake() method.
/// </summary>
public void Awake()
{
// Enable ARCore to target 60fps camera capture frame rate on supported devices.
// Note, Application.targetFrameRate is ignored when QualitySettings.vSyncCount != 0.
Application.targetFrameRate = 60;
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
/// <summary>
/// The Unity Update method.
/// </summary>
public void Update()
{
Debug.Log(
$"*debuger activeSelf = {debugGameobject.activeSelf} & " +
$"activeInHierarchy = {debugGameobject.activeInHierarchy}");
//Debug.LogWarning("gameO parent = " + debugGameobject.transform.parent.name);
// Exit the app when the 'back' button is pressed.
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
if (m_KeepRunning)
{
print("in keep");
// Get updated augmented images for this frame.
Session.GetTrackables<AugmentedImage>(
m_TempAugmentedImages, TrackableQueryFilter.Updated);
if (m_TempAugmentedImages.Count > 0)
{
if (m_TempAugmentedImages[0].TrackingState == TrackingState.Tracking)
{
print("Assining");
// Create an anchor to ensure that ARCore keeps tracking this augmented image.
Anchor anchor = m_TempAugmentedImages[0].CreateAnchor(m_TempAugmentedImages[0].CenterPose);
debugGameobject.transform.position = anchor.transform.position;
debugGameobject.transform.parent = anchor.transform;
debugGameobject.SetActive(true);
m_KeepRunning = false;
}
}
}
}
}
}