- Home /
Vuforia - Disable/Enable Game Object Mesh Renderer On Button Touch Event
Is there any way to disable or enable the game object Mesh Renderer on button press in Vuforia? I knew it is easier to do in unity.
In Vuforia, OnTrackingLost() function handles this. But how do I reference the particular game object and turn on the renderer on and off using button click?
Any help would be appreciated. Thanks.
I realise you posted two questions slightly different but getting the same answer.
Answer by fafase · Apr 02, 2015 at 05:53 AM
Just like you would do for a normal object. As long as you detect the event (OnTrackingLost) you can spread the event to any listener and in your case you would have your GameObject that waits for the event to be triggered:
public class TargetTracker : MonoBehaviour, ITrackingEventHandler
{
public event Action OnTrackingFound = ()=>{};
public event Action OnTrackingLost = ()=>{};
public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
{
if(newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED){
OnTrackingFound();
} else{
OnTrackingLost();
}
}
}
Then your object listens like this:
public class ObjectTrackListener:MonoBehaviour{
private TargetTracker tt = null;
void Start(){
tt = GameObect.Find("Tracker").GetComponent<TargetTracker>();
tt.OnTrackingFound += TrackingFound;
tt.OnTrackingLost += TrackingLost;
}
void OnDestroy(){
tt.OnTrackingFound -= TrackingFOund;
tt.OnTrackingLost -= TrackingLost;
}
private void TrackingFound(){
gameObject.SetActive(true);
}
private void TrackingFound(){
gameObject.SetActive(false);
}
}
In this case, it will deactivate the object but you get teh idea you can do whatever you wish in there, like disabling renderer.
Thanks a lot.
If I add public class TargetTracker : $$anonymous$$onoBehaviour, ITrackingEvent
it flags an error. Is this ITrackingEventis in the Vuforia script? I have included the name space vuforia, All I can see is ITrackableEventHandler and ITrackerEventHandler. Am I missing something?
Also you have used public event Action OnTrackingFound = ()=>{}; It couldn't locate Action in the event. Am I doing something wrong?
Oh ok my bad the ITrackingEvent is my own interface I use to spread the events so the vuforia is the one you mentioned. I Edited the answer
Thanks. Still I get error in this line public event Action OnTrackingFound = ()=>{};
Am I missing something?
Thanks. Ah! $$anonymous$$y bad, I used System.IO, now changed it to System. Works fine. But am not quiet sure how to make the object appear only when I press the button. $$anonymous$$eaning, the model should be deactivated from the beginning, only when the user click the button it should be visible. I have pasted below the code.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
namespace Vuforia
{
public class targetTrackListener : $$anonymous$$onoBehaviour
{
public bool visiToggle;
private targetTracker targetObject = null;
void Start()
{
targetObject = GameObject.Find("ImageTarget").GetComponent<targetTracker>();
targetObject.OnTrackingFound += trackingFound;
//targetObject.OnTrackingLost+= trackingLost;
}
void onDestroy()
{
targetObject.OnTrackingFound -= trackingFound;
//targetObject.OnTrackingLost-= trackingLost;
}
public void trackingFound()
{
if(visiToggle)
{
gameObject.SetActive(false);
visiToggle = false;
}
else
{
gameObject.SetActive(true);
visiToggle = true;
}
}
}
}
Your answer
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
Hi. I can't track my Vuforia 3D Scan Object successfully. Can't someone help me please. , 0 Answers
Disable Game object, Enable other Game objects after 5 seconds to show progress 2 Answers
How can I keep objects invisible when vuforia ImageTarget is lost and found again? 1 Answer