Log in or register to post comments

Switching MainCamera at runtime on scene load

November 5, 2014 - 7:44am #9

I have asked this on the Unity forums too, but not sure if it is a Unity or a Vuforia thing?

I have a main menu that is the first scene in my app and has a camera set as MainCamera. On my second scene I have an ARCamera also set as MainCamera that is ignored as a main camera as the first scene has bagged it.

I'm trying to figure out how to switch them as I segue from scene 1 to scene 2 and am having trouble finding the solution.

I have tried loading by tag, setting enabled and setting active but cannot get scene 2 to get a main character....

Is there a special technique for this?

Thanks in advance

Switching MainCamera at runtime on scene load

November 5, 2014 - 2:31pm #8

OK I have checked this out and think there "may" be a Vuforia glitche ...please note the use of the word "may"...

I now undertsand that when loading a new scene the preceeding scene is unloaded, unless specified not to.

In my setup the menu scene has a main camera named camera and is tagged MainCamera, the AR scene has a camera named ARCamera and is tagged MainCamera.

When menu scene loads I Debug.Log the name of the set MainCamera and I get "camera" displayed.

When I segue to the AR scene I again Debug.Log the name of the set MainCamera and I get "ARCamera" displayed.

But none of my AR  works, basically I just get the camera view displayed.

 

If I then build so that the menu scene is omitted and it starts straight into the ARScene all my AR works perfectly

So from the menu scene calling Application.LoadLevel("ARScene"); does load the AR scene, the camera name being displayed confirms that....but no AR functionality?

 

Help???

Switching MainCamera at runtime on scene load

November 6, 2014 - 5:21am #7

I have looked t the debug output and finding many differences as detailed below, where loading the app direct to AR scene works fine but comeing from an Application.LoadLevel() call I get a completely different log output?

 

Direct to AR scene starts out like this where it finds the trackables:

dataSetGetNumTrackableType

imageTargetSetSize

Found Trackable named CardTwo with id 4

 (Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)

imageTargetSetSize

OTT: startExtendedTracking

Found Trackable named CardFour with id 3

 (Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)

imageTargetSetSize

OTT: startExtendedTracking

Found Trackable named CardOne with id 1

 (Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)

imageTargetSetSize

OTT: startExtendedTracking

Found Trackable named CardFive with id 5

 (Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)

imageTargetSetSize

Found Trackable named CardThree with id 2

 (Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)

imageTrackerActivateDataSet

StartQCAR

 

But from Application.LoadLevel() I get this as AR scene loads:

dataSetGetNumTrackableType
Creating Image Target with values: 
 ID:           1
 Name:         CardOne
 Path:         
 Size:         100x142.037
 
(Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)
 
imageTargetSetSize
Creating Image Target with values: 
 ID:           2
 Name:         CardThree
 Path:         
 Size:         100x143.097
 
(Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)
 
imageTargetSetSize
Creating Image Target with values: 
 ID:           3
 Name:         CardFour
 Path:         
 Size:         100x142.9098
 
(Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)
 
imageTargetSetSize
Creating Image Target with values: 
 ID:           4
 Name:         CardTwo
 Path:         
 Size:         100x143.5514
 
(Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)
 
imageTargetSetSize
Creating Image Target with values: 
 ID:           5
 Name:         CardFive
 Path:         
 Size:         100x143.5185
 
(Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)
 
imageTargetSetSize
imageTrackerActivateDataSet
StartQCAR
 
(Filename: /Applications/buildAgent/work/d63dfc6385190b60/artifacts/iPhonePlayer-armv7Generated/UnityEngineDebug.cpp Line: 49)
 
Would really appreciate a nod in the right direction if this is down to me....
 
Thanks

 

 

Switching MainCamera at runtime on scene load

November 7, 2014 - 5:11am #6

*bump* .... please...

Is this in same way an init thing that when the AR scene loads directly without a menu scene some kind of init takes place but if I load a menu scene first which in turn load the AR scene is the init omitted?

 

Completely stuck ... help?

 

Thanks

Switching MainCamera at runtime on scene load

November 11, 2014 - 5:32am #5

anyone......getting desperate....:-(

 

 

Switching MainCamera at runtime on scene load

November 11, 2014 - 5:46am #4

 

elpuerco63:

Is this in same way an init thing that when the AR scene loads directly without a menu scene some kind of init takes place but if I load a menu scene first which in turn load the AR scene is the init omitted?

 

No, if you have a Scene with an ARcamera, the ARCamera should get initialized as soon as you load that Scene. It should not matter whether the Scene containing the ARCamera is the first or the second scene in your App.

You may want to check the "Books" sample; this contains multiple Scenes, and the Vuforia "stuff" (i.e. ARCamera and other Vuforia prefabs) are only loaded in Scene #4. 

 

 

 

Switching MainCamera at runtime on scene load

November 11, 2014 - 6:40am #3

Thanks, that is an interesting app and I can see the way you segue from the About scene to Loading Scene to the actual AR scene which will be very helpful to me, really appreciate the pointer....

 

Switching MainCamera at runtime on scene load

November 24, 2017 - 5:10am #2

Hello,

I realize this is a very old post, but I am having a similar issue with Unity 5.6.4 and integrated Vuforia and so far this is my only promising lead.

In my project I have a Main scene with no AR features. Then I have 2 other scenes, one that detects a VuMark and another one that detects Image Targets. These scenes are loaded additively from the Main one, in a way that I will always have 2 scenes loaded at most: Main + AR_Vumark or Main + AR_Images. In other words, once an AR scene is loaded I can't load any other scene on top. The only possibility is to go back to Main (unload AR scene) and then I can load the other one.

Each of the AR scenes has its own ARCamera. Nothing is shared besides the Vuforia configuration, which is global. here I have two datasets active, one for the Vumark and another one for the Image Targets.

Finally, a peculiarity of my AR_Images scene is that on Start I disable all Image Targets but one. I do this because depending on where I clicked on the Main scene I want to detect only a specific one. My Start method does something like this:

 

private void Start()
{
    m_ImageCode = SceneDataExchange.AR_IMAGE_CODE;    // This tells me which Image Marker I need to keep active

    VuforiaARController vuforia = VuforiaARController.Instance;
    vuforia.RegisterVuforiaStartedCallback(OnVuforiaStarted);

    // Enable marker detection for the current image only
    foreach (ImageTargetBehaviour target in m_MarkersHolder.GetComponentsInChildren<ImageTargetBehaviour>(true))
    {
        bool enabled = target.gameObject.name.CompareTo(m_ImageCode) == 0;
        target.enabled = enabled;
        target.gameObject.SetActive(enabled);
        if (enabled)
        {
            m_Target = target;
            target.RegisterTrackableEventHandler(this);
        }
    }
}

      

Everything works fine on the Editor, but on a build (I'm building for UWP) I'm detecting a strange behaviour:

- The scene AR_Vumark always works, no matter what I do.

- If I run the app and go straight from the scene Main to AR_Images, everything works as expected. And I can load it and unload it as many times as I want and it works.

- The scene AR_Images stops working (it doesn't detect the Image Target) the moment I load AR_Vumark. In other words:

    -- Main -> AR_Images (OK) -> Main -> AR_Images (OK)

    -- Main -> AR_Vumark (OK) -> Main -> AR_Vumark (OK)

    -- Main -> AR_Vumark (OK) -> Main -> AR_Images (BROKEN) -> Main -> AR_Vumark (OK)

    -- Main -> AR_Images (OK) -> Main -> AR_Vumark (OK) -> Main -> AR_Images (BROKEN) -> Main -> AR_Vumark (OK)

 

The only difference I have found when comparing the logs is this:

- When AR_Images works the only traces are of the type "Found Trackable X with id Y"

- When I load the scene AR_Images after having loaded AR_Vumark, I see extra traces of the type "Creating Image Target with values:  ID: ... Name: ... Path: ... Size: ...", just as the ones detected by user elpuerco63. I suspect this is what causes the Image Target not being detected.

I would appreciate any help. Thank you!

Switching MainCamera at runtime on scene load

November 28, 2017 - 9:47am #1

If anyone is interested, I found the issue and a way to solve it.

The problem was that even when I'm not using a dataset in a scene (it's not active and the scene doesn't have any of its targets), Vuforia is auto-creating them. So when I load my AR_Images scene, I enable only the ImageTargets dataset but Vuforia also creates a target for the Vumark included in the other dataset. And when loading the AR_Vumark scene, I only activate the Vumarks dataset but Vuforia creates targets for all of the ImageTargets in the other dataset.

These objects are not destroyed when unloading the scene, so when I loaded AR_Vumark and then AR_Images, there was a sort of conflict between the ImageTargets in the scene (the ones I created) and the ones auto-created by Vuforia.

My workaround has been to look for all of the DataSetTrackableBehaviour in the scene and destroy the game objects manually in the method OnDestroy of my scene controller. To know if they have been auto-created I check if the name of the trackable is "New Game Object".

Hope this helps someone.

Log in or register to post comments