LaRocca
LaRocca

Reputation: 55

How to stop Vuforia Behaviour

I have three scenes in my project.

First one is just a Main Menu.

Second one is a Virtual Reality Scene using GoogleVR

Third one is a AR Scene using vuforia.

Not sure why, but when I started VR Scene (Not AR), vuforia behaviour is loaded too and it changed my field of view.

There is a way to prevent vuforia to start when app is loaded and just start when I load AR Scene?

I tried this but not worked. This stopped the camera instance, but not vuforia behaviour.

public void stopAR(){
    if (Vuforia.CameraDevice.Instance.IsActive()) {
        Vuforia.CameraDevice.Instance.Deinit ();
        Vuforia.CameraDevice.Instance.Stop ();
        Debug.Log ("AR Stopped");
    }
}

Upvotes: 1

Views: 5662

Answers (4)

Tiny Brain
Tiny Brain

Reputation: 640

[SOLVED] You can just enable/disable the Vuforia camera using the VuforiaBehavior component.

void StartCamera () {
    vuforiaBehavior.enabled = true;
}

void StopCamera () {
    vuforiaBehavior.enabled = false;
}

Upvotes: 0

Joeri Van den Eynden
Joeri Van den Eynden

Reputation: 11

For anyone interested, I found a way to disable this behaviour entirely following this example.

You just add this to a gameobject in your FIRST scene and make sure it executes before anything else (e.g. through the script execution order). It unsubscribes the event method call from the SceneManager to the VuforiaRuntime singleton that adds the behaviour(s) to every main camera in every scene. Once this is done, it should never do this again.

Tested version for Unity 2017.3.1p4

using UnityEngine;
using System.Reflection;
using UnityEngine.SceneManagement;
using System;

public class StopAutoAddVuforia : MonoBehaviour
{
     private void Awake() {
         // https://forum.unity.com/threads/use-ar-camera-vuforia-core-in-individual-scene-not-entire-project.498489/
         try {
             EventInfo evSceneLoaded = typeof(SceneManager).GetEvent("sceneLoaded");
             Type tDelegate = evSceneLoaded.EventHandlerType;

             MethodInfo attachHandler = typeof(Vuforia.VuforiaRuntime).GetMethod("AttachVuforiaToMainCamera", BindingFlags.NonPublic | BindingFlags.Static);

             Delegate d = Delegate.CreateDelegate(tDelegate, attachHandler);
             SceneManager.sceneLoaded -= d as UnityEngine.Events.UnityAction<Scene, LoadSceneMode>;
         } catch (Exception e) {
             Debug.LogWarning("Cant remove the AttachVuforiaToMainCamera action. Execption:" + e.Message);
         }

         Destroy(this);
     }
 }

Tested version for Unity 2018.2.4f1

using UnityEngine;
using System.Reflection;
using UnityEngine.SceneManagement;
using System;

 public class StopAutoAddVuforia : MonoBehaviour
 {
     private void Awake() {
         // https://forum.unity.com/threads/use-ar-camera-vuforia-core-in-individual-scene-not-entire-project.498489/
         try {
             EventInfo evSceneLoaded = typeof(SceneManager).GetEvent("sceneLoaded");
             Type tDelegate = evSceneLoaded.EventHandlerType;

             MethodInfo attachHandler = typeof(Vuforia.VuforiaRuntime).GetMethod("AttachVuforiaToMainCamera", BindingFlags.NonPublic | BindingFlags.Instance);

             Delegate d = Delegate.CreateDelegate(typeof(UnityEngine.Events.UnityAction<Scene, LoadSceneMode>), Vuforia.VuforiaRuntime.Instance, attachHandler);
             SceneManager.sceneLoaded -= d as UnityEngine.Events.UnityAction<Scene, LoadSceneMode>;
         } catch (Exception e) {
             Debug.LogError("Cant remove the AttachVuforiaToMainCamera action. Exception " + e.Message);
         }

         Destroy(this);
     }
 }

Upvotes: 1

Anh Huynh
Anh Huynh

Reputation: 109

using UnityEngine;
using Vuforia;

public class NoAR : MonoBehaviour
{
    public Camera camera;

    // Use this for initialization
    void Start()
    {
        if(camera.enabled)
            if (camera.GetComponent<VuforiaBehaviour>() != null)
                camera.GetComponent<VuforiaBehaviour>().enabled = false;
    }
}

Upvotes: 0

LaRocca
LaRocca

Reputation: 55

[SOLVED] I followed this to solve this problem:

On Main Camera where I do not want Vuforia to start, add VuforiaBehavious script

Uncheck it

Done.

Upvotes: 2

Related Questions