I too found this problem while trying to spawn several instances of a VuMark.
You do not have to track this every frame, but only when the VuMark identifies a mark. After trying different solutions to this problem, I found a suitable one for my project:
Upon identifying a mark, a conditional check in the OnTrackingFound() function in DefaultTrackableEventHandler was triggered, in which the id/code of the identified mark was parsed and suitable actions took place.
In my case, I have several child GameObjects with the same name as the id/code for the VuMarks, and i just SetActive(true) the child which name that correspond to the identified mark. All the other are SetAcive(false)'d.
Example and information:
*1 If VuMarkCode "child0", "child1", or "child2" is identified, show it (only one of them per instance) and hide everything else for this instance. If vuforia is configured to handle multiple marks, all of these childs can be triggered simultaneusly.
*2 If finding "other0" or "other1", childs does not get spawned, but these are recognised and can be used for things such as scene transitions and other useful thunks.
*3 All children will automagically be deactivated whenever the mark is lost, but the state of the object will be retained, since Vuforia does not delete any instances that are deactivated. However! The state if only valid for the object which instance was active. If the state should be stored over all object of that kind, the information will have to be transferred to some other object.
*4 All children must be present in the Unity Hierarchy tree view when application is executed. But only those which are set to display are displayed at runtime when the correct VuMark is shown.
Code:
// Called from OnTrackingFound()
public void ActivateChildByVuMarkId()
// Check if a mark has been identified, returns null if there are no marks active in this instance. Might not strictly be necessary since this is only called when a mark is found, but useful if called from Update (Not recommended).
if(GetComponent<VuMarkBehaviour>().VuMarkTarget == null)
return;
// Get the code/id of the identified VuMarkInstance
string vuMarkCode = GetComponent<VuMarkBehaviour>().VuMarkTarget.InstanceId.ToString();
switch(vuMarkCode) {
case "child0":
case "child1":
case "child2":
foreach(Transform child in transform) {
if(child.gameObject.name.Equals(vuMarkCode)) {
child.gameObject.SetActive(true);
} else {
child.gameObject.SetActive(false);
}
}
break;
case "other0":
// Do something else
break;
case "other1":
// Yet something else to do
break;
default:
break;
}
//Sorry for the code formatting, seems the code-block tags did disturbing things with the code..
Best regards Erik
I too found this problem while trying to spawn several instances of a VuMark.
You do not have to track this every frame, but only when the VuMark identifies a mark. After trying different solutions to this problem, I found a suitable one for my project:
Upon identifying a mark, a conditional check in the OnTrackingFound() function in DefaultTrackableEventHandler was triggered, in which the id/code of the identified mark was parsed and suitable actions took place.
In my case, I have several child GameObjects with the same name as the id/code for the VuMarks, and i just SetActive(true) the child which name that correspond to the identified mark. All the other are SetAcive(false)'d.
Example and information:
*1 If VuMarkCode "child0", "child1", or "child2" is identified, show it (only one of them per instance) and hide everything else for this instance. If vuforia is configured to handle multiple marks, all of these childs can be triggered simultaneusly.
*2 If finding "other0" or "other1", childs does not get spawned, but these are recognised and can be used for things such as scene transitions and other useful thunks.
*3 All children will automagically be deactivated whenever the mark is lost, but the state of the object will be retained, since Vuforia does not delete any instances that are deactivated. However! The state if only valid for the object which instance was active. If the state should be stored over all object of that kind, the information will have to be transferred to some other object.
*4 All children must be present in the Unity Hierarchy tree view when application is executed. But only those which are set to display are displayed at runtime when the correct VuMark is shown.
Code:
// Called from OnTrackingFound()
public void ActivateChildByVuMarkId()
// Check if a mark has been identified, returns null if there are no marks active in this instance. Might not strictly be necessary since this is only called when a mark is found, but useful if called from Update (Not recommended).
if(GetComponent<VuMarkBehaviour>().VuMarkTarget == null)
return;
// Get the code/id of the identified VuMarkInstance
string vuMarkCode = GetComponent<VuMarkBehaviour>().VuMarkTarget.InstanceId.ToString();
switch(vuMarkCode) {
case "child0":
case "child1":
case "child2":
foreach(Transform child in transform) {
if(child.gameObject.name.Equals(vuMarkCode)) {
child.gameObject.SetActive(true);
} else {
child.gameObject.SetActive(false);
}
}
break;
case "other0":
// Do something else
break;
case "other1":
// Yet something else to do
break;
default:
break;
}
//Sorry for the code formatting, seems the code-block tags did disturbing things with the code..
Best regards Erik