Log in or register to post comments

Enabling/disabling tracking within a single Unity scene on iOS

December 17, 2013 - 12:10pm #1

I have a single Unity scene where user is able to view both AR and pure 3D content. The scene has the Vuforia ARCamera prefab added to it. In addition, there is another camera in the scene for rendering pure 3D content (in which there is no video background and the camera is user rotatable). By enabling/disabling the Camera component of the ARCamera prefab and the 3D camera, the user can switch between the AR and 3D view respectively. This works great.

In order to save battery while in pure 3D mode I set QCARBehaviour.enabled = false for the QCARComponent that is inside the ARCamera prefab. 

Question1: Is this right way to disable tracking? Are there any other things to do/consider when I want to pause the tracking when it is not needed to extend the battery life?

Another question relates to tracking behavior when the app goes to background (is suspended) when user presses either home key or power key. I have observed that when user returns to app that has been suspended the tracking is also resumed regardless of the state of the QCARBehaviour.enabled.

Question2: Can you confirm if above is true and is there way to prevent tracking being resumed if it was paused already when app is suspended?

Enabling/disabling tracking within a single Unity scene on iOS

December 17, 2013 - 2:08pm #2

Hi,

disabling QCARBehaviour is a correct way of disabling ARCamera and Trackers.

However, a more "selective" way of stopping the trackers (and optionally also the ARcamera) is via this API:

ImageTracker imgTracker = TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER) as ImageTracker;
			imgTracker.Stop();
			MarkerTracker mkTracker = TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER) as MarkerTracker;
			mkTracker.Stop();
			CameraDevice.Instance.Stop();
			CameraDevice.Instance.Deinit();

Then if you want to prevent QCAR from restarting the Trackers when the app is resumed, you could use some logic like in this example (you can attach the script to the ARCamera):

public class StopTracker : MonoBehaviour {

	private bool mTrackerON = true;
	private bool mWasPaused = false;

	void OnApplicationPause(bool b) {
		Debug.Log ("====== App was Paused ======");
		mWasPaused = true;
	}

	void Update() {
		if (mWasPaused) {
			// updating after it was paused
			// this means we have just resumed
			Debug.Log("====== App was just resumed ===== ");

			if (!mTrackerON) {
				
				//counter-act default starting of tracker
				ImageTracker imgTracker = TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER) as ImageTracker;
				imgTracker.Stop();
				MarkerTracker mkTracker = TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER) as MarkerTracker;
				mkTracker.Stop();
				CameraDevice.Instance.Stop();
				CameraDevice.Instance.Deinit();
				Debug.Log("======  Force Stop ARCamera and Trackers ======");
			}

			mWasPaused = false;
		}
	}

	
	void OnGUI() {
		if (GUI.Button(new Rect(50,50,100,30), "Stop Tracker")) {
			// Stop the Trackers and the ARCamera
			ImageTracker imgTracker = TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER) as ImageTracker;
			imgTracker.Stop();
			MarkerTracker mkTracker = TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER) as MarkerTracker;
			mkTracker.Stop();
			CameraDevice.Instance.Stop();
			CameraDevice.Instance.Deinit();

			mTrackerON = false;
		}
		
		if (GUI.Button(new Rect(150,50,100,30), "Start Tracker")) {
			// Start the Trackers and ARCamera
			CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_DEFAULT);
			CameraDevice.Instance.Start();
			ImageTracker imgTracker = TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER) as ImageTracker;
			imgTracker.Start();
			MarkerTracker mkTracker = TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER) as MarkerTracker;
			mkTracker.Start();

			mTrackerON = true;
		}
	}
}

The basic idea is to detect when the app is paused, and save this event in the mWasPaused boolean; then, when the app is resumeid, as soon as Update() is called you can check if the mWasPaused is true, which means that app was previously paused and now it has been resmed (since Update() is being called);

under this condition, you can then enforce stopping the trackers and the camera.

I've tried this script on Android, but hopefully this should also work on iOS devices.

 

 

Log in or register to post comments